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中文網其他相關文章!

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文使用lambda表達式,流API,方法參考和可選探索將功能編程集成到Java中。 它突出顯示了通過簡潔性和不變性改善代碼可讀性和可維護性等好處

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用選擇器和頻道使用單個線程有效地處理多個連接的Java的NIO API,用於非阻滯I/O。 它詳細介紹了過程,好處(可伸縮性,性能)和潛在的陷阱(複雜性,

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文詳細介紹了用於網絡通信的Java的套接字API,涵蓋了客戶服務器設置,數據處理和關鍵考慮因素,例如資源管理,錯誤處理和安全性。 它還探索了性能優化技術,我


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。