Azure Functions 為在 Azure 容器應用程式上開發、部署和管理容器化函數應用程式提供整合支援。與在 Azure Kubernetes 服務 (AKS) 等容器環境中獨立執行 Azure Functions 相比,使用整合的 Azure 管理入口網站可以更輕鬆地執行和管理 Azure Functions。此外,透過利用 Azure 容器應用程式提供的功能,您可以輕鬆利用 Azure Functions 的 KEDA、Dapr、Envoy、擴充功能、監控、安全性和存取控制等功能。
[參考]
託管 Azure Functions 的 Azure 容器應用程式
在 Azure 容器應用程式上建立您的第一個容器化函數
設定環境變數
以下是與建立 Azure 容器應用程式資源相關的環境變數。在這裡,您為將建立的資源指定各種名稱和安裝位置,以及容器映像名稱和標籤。
# Azure Container Apps resource names export LOCATION=eastus export RESOURCE_GROUP_NAME=yoshio-rg export CONTAINER_REGISTRY_NAME=cajava2411 export CONTAINER_ENVIRONMENT=YoshioContainerEnvironment export STORAGE_NAME=yoshiojavastorage export AZURE_FUNCTION_NAME=yoshiojavafunc # Container image name and tag export C_IMAGE_NAME=tyoshio2002/java-function-on-aca export C_IMAGE_TAG=1.0
建立並測試 Java Azure Function 項目
1. 建立 Azure Functions for Java Maven 項目
首先,為 Azure Functions for Java 建立一個 Maven 專案。此 Maven 專案旨在使用 Java 21 建立 Azure Functions。使用 mvn archetype:generate 指令建立項目,根據需要修改參數。
mvn archetype:generate \ -DinteractiveMode=false \ -DarchetypeGroupId=com.microsoft.azure \ -DarchetypeArtifactId=azure-functions-archetype \ -DgroupId=com.yoshio3 \ -Dpackage=com.yoshio3 \ -DartifactId=yoshiojavafunc \ -DappName=Java-Azure-Functions \ -DappRegion=$LOCATION \ -DjavaVersion=21 \ -Dversion=1.0-SNAPSHOT \ -Ddocker
執行上述指令將自動建立目錄結構,Function.java 將包含帶有 HTTP 觸發器的 Azure Function 的範例程式碼。也會建立一個 Dockerfile,其中包含在 Docker 容器環境中執行 Azure Functions 的配置。
├── Dockerfile ├── host.json ├── local.settings.json ├── pom.xml └── src ├── main │ └── java │ └── com │ └── yoshio3 │ └── Function.java └── test └── java └── com └── yoshio3 ├── FunctionTest.java └── HttpResponseMessageMock.java
2.本地運行Azure函數
建置 Maven 專案並在本機執行 Azure Functions。執行下列命令以使用 HTTP 觸發器啟動 Azure Functions。
mvn clean package mvn azure-functions:run
Azure Functions 運行後,打開另一個終端機並執行以下命令以向 HTTP 觸發器發送請求。您應該會收到一封回覆「Hello, World」。
curl "http://localhost:7071/api/HttpExample?name=World" # Output: Hello, World
建立和測試 Azure Functions 容器映像
1. 建置並測試 Docker 映像
使用自動產生的 Dockerfile 建構 Azure Functions 容器映像。執行以下命令來建立鏡像。
docker build -t $C_IMAGE_NAME:$C_IMAGE_TAG -f ./Dockerfile .
建置完成後,執行以下命令檢查鏡像是否已建立。
docker images | grep $C_IMAGE_NAME # Output: tyoshio2002/java-function-on-aca 1.0 bcf471e6f774 9 hours ago 1.46GB
建立映像後,執行下列指令在本機測試 Azure Functions 容器映像。 Azure Functions 容器內部使用 HTTP 連接埠 80,因此您將其對應至連接埠 8080 以進行本機存取。容器啟動後,執行curl指令向Azure Functions HTTP觸發器發送請求。如果一切正常,您應該會收到“Hello, World”。
docker run -p 8080:80 -it $C_IMAGE_NAME:$C_IMAGE_TAG curl "http://localhost:8080/api/HttpExample?name=World" # Output: Hello, World
將容器映像推送到 Azure 容器註冊表
1.登入Azure CLI
首先,使用 Azure CLI 登入 Azure。執行以下命令登入。
az login
2. 建立資源組
在 Azure 中建立資源組。此資源組將用於將與 Azure 容器註冊表和 Azure 容器應用程式相關的資源進行分組。
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
3. 建立並登入Azure容器註冊表
建立 Azure 容器註冊表並登入。 Azure 容器登錄檔是用於推送容器映像的私人容器註冊表。
az acr create --resource-group $RESOURCE_GROUP_NAME --name $CONTAINER_REGISTRY_NAME --sku Basic az acr login --name $CONTAINER_REGISTRY_NAME
4. 檢索 Azure 容器登錄伺服器名稱
檢索已建立的 Azure 容器註冊表的伺服器名稱。伺服器名稱的格式為 $CONTAINER_REGISTRY_NAME.azurecr.io。
CONTAINER_REGISTRY_SERVER=$(az acr show --name $CONTAINER_REGISTRY_NAME --query loginServer --output tsv)
5. 將鏡像推送到Azure容器登錄檔
要將本機建立的容器映像推送到 Azure 容器註冊表,請使用 tag 指令標記該映像。打標籤後,使用push指令推送鏡像。
docker tag $C_IMAGE_NAME:$C_IMAGE_TAG $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG docker push $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG
建立 Azure 容器應用程式並部署 Java Azure Function
1. 在 Azure CLI 中註冊擴充功能和資源提供者
要從 Azure CLI 建立和管理 Azure 容器應用程序,請註冊必要的擴充功能和資源提供者。
az upgrade az extension add --name containerapp --upgrade -y az provider register --namespace Microsoft.Web az provider register --namespace Microsoft.App az provider register --namespace Microsoft.OperationalInsights
2.建立Azure容器應用程式環境
為Azure容器應用程式建立環境。此命令設定託管 Azure 容器應用程式所需的配置。
az containerapp env create --name $CONTAINER_ENVIRONMENT --enable-workload-profiles --resource-group $RESOURCE_GROUP_NAME --location $LOCATION
3. Create Storage Account for Azure Functions
Azure Functions requires a storage account when creating a Function App instance. Therefore, create a general-purpose storage account for Azure Functions.
az storage account create --name $STORAGE_NAME --location $LOCATION --resource-group $RESOURCE_GROUP_NAME --sku Standard_LRS
4. Create an Instance of Java Azure Function in Azure Container Apps
Create an instance of the Java Azure Function in Azure Container Apps. Execute the following command to create the instance. Since the Azure Function is created using Java 21, specify --runtime java.
az functionapp create --name $AZURE_FUNCTION_NAME \ --resource-group $RESOURCE_GROUP_NAME \ --environment $CONTAINER_ENVIRONMENT \ --storage-account $STORAGE_NAME \ --workload-profile-name "Consumption" \ --max-replicas 15 \ --min-replicas 1 \ --functions-version 4 \ --runtime java \ --image $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG \ --assign-identity
5. Assign Role for Azure Function to Access Azure Container Registry
Finally, configure secure access for Azure Functions to Azure Container Registry. Enable the system-managed identity for Azure Functions and assign the ACRPull role for access.
FUNCTION_APP_ID=$(az functionapp identity assign --name $AZURE_FUNCTION_NAME --resource-group $RESOURCE_GROUP_NAME --query principalId --output tsv) ACR_ID=$(az acr show --name $CONTAINER_REGISTRY_NAME --query id --output tsv) az role assignment create --assignee $FUNCTION_APP_ID --role AcrPull --scope $ACR_ID
6. Retrieve the URL of the Azure Function
Finally, retrieve the HTTP trigger function URL of the deployed Azure Function. Use the az functionapp function show command to get the details of the Azure Functions function.
az functionapp function show --resource-group $RESOURCE_GROUP_NAME --name $AZURE_FUNCTION_NAME --function-name HttpExample --query invokeUrlTemplate # Output: "https://yoshiojavafunc.niceocean-********.eastus.azurecontainerapps.io/api/httpexample"
You can then send a request to the retrieved URL using curl command to confirm that the Azure Functions is working correctly.
curl "https://yoshiojavafunc.niceocean-********.eastus.azurecontainerapps.io/api/httpexample?name=World" # Expected Output: Hello, World
If everything is set up correctly, you should receive a response saying "Hello, World", confirming that your Azure Function is functioning as expected.
Summary
In this guide, you learned how to:
- Set up environment variables for your Azure resources.
- Create a Java Azure Function project using Maven.
- Run the Azure Function locally for testing.
- Build a Docker image for the Azure Function.
- Push the Docker image to Azure Container Registry.
- Create an Azure Container Apps environment and deploy the Java Azure Function.
- Assign necessary roles for secure access to Azure Container Registry.
- Retrieve and test the URL of the deployed Azure Function.
By following these steps, you can successfully deploy a Java Azure Function on Azure Container Apps, leveraging the benefits of containerization and Azure's integrated management capabilities. If you have any further questions or need assistance with specific steps, feel free to ask!
以上是在 Azure 容器應用程式上部署 Java Azure Function的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java'splatFormIndependecemeansDeveloperScanWriteCeandeCeandOnanyDeviceWithouTrecompOlding.thisAcachivedThroughThroughTheroughThejavavirtualmachine(JVM),WhaterslatesbyTecodeDecodeOdeIntComenthendions,允許univerniverSaliversalComplatibilityAcrossplatss.allospplats.s.howevss.howev

要設置JVM,需按以下步驟進行:1)下載並安裝JDK,2)設置環境變量,3)驗證安裝,4)設置IDE,5)測試運行程序。設置JVM不僅僅是讓其工作,還包括優化內存分配、垃圾收集、性能調優和錯誤處理,以確保最佳運行效果。

toensurejavaplatFormIntence,lofterTheSeSteps:1)compileAndRunyOpplicationOnmultPlatFormSusiseDifferenToSandjvmversions.2)upureizeci/cdppipipelinelikeinkinslikejenkinsorgithikejenkinsorgithikejenkinsorgithikejenkinsorgithike forautomatecross-plateftestesteftestesting.3)

javastandsoutsoutinmoderndevelopmentduetoitsrobustfeatureslikelambdaexpressions,streams,andenhanced concurrencysupport.1)lambdaexpressionssimplifyfunctional promprogientsmangional programmanging,makencodemoreconciseandable.2)

Java的核心特點包括平台獨立性、面向對象設計和豐富的標準庫。 1)面向對象設計通過多態等特性使得代碼更加靈活和可維護。 2)垃圾回收機制解放了開發者的內存管理負擔,但需要優化以避免性能問題。 3)標準庫提供了從集合到網絡的強大工具,但應謹慎選擇數據結構以保持代碼簡潔。

Yes,Javacanruneverywhereduetoits"WriteOnce,RunAnywhere"philosophy.1)Javacodeiscompiledintoplatform-independentbytecode.2)TheJavaVirtualMachine(JVM)interpretsorcompilesthisbytecodeintomachine-specificinstructionsatruntime,allowingthesameJava

jdkincludestoolsfordEveloping and compilingjavacode,whilejvmrunsthecompiledbytecode.1)jdkcontainsjre,編譯器,andutilities.2)

Java的關鍵特性包括:1)面向對象設計,2)平台獨立性,3)垃圾回收機制,4)豐富的庫和框架,5)並發支持,6)異常處理,7)持續演進。 Java的這些特性使其成為開發高效、可維護軟件的強大工具。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用