首頁 > Java > java教程 > 主體

在 Azure 容器應用程式上部署 Java Azure Function

WBOY
發布: 2024-09-09 22:30:47
原創
371 人瀏覽過

Deploying a Java Azure Function on Azure Container Apps

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:

  1. Set up environment variables for your Azure resources.
  2. Create a Java Azure Function project using Maven.
  3. Run the Azure Function locally for testing.
  4. Build a Docker image for the Azure Function.
  5. Push the Docker image to Azure Container Registry.
  6. Create an Azure Container Apps environment and deploy the Java Azure Function.
  7. Assign necessary roles for secure access to Azure Container Registry.
  8. 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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!