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脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。