在 Azure 容器應用程式上部署 Java Azure Function
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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
