首页 Java java教程 在 Azure 容器应用程序上部署 Java Azure Function

在 Azure 容器应用程序上部署 Java Azure Function

Sep 09, 2024 pm 10:30 PM

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

公司安全软件导致应用无法运行?如何排查和解决? 公司安全软件导致应用无法运行?如何排查和解决? Apr 19, 2025 pm 04:51 PM

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

如何使用MapStruct简化系统对接中的字段映射问题? 如何使用MapStruct简化系统对接中的字段映射问题? Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

如何优雅地获取实体类变量名构建数据库查询条件? 如何优雅地获取实体类变量名构建数据库查询条件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

如何将姓名转换为数字以实现排序并保持群组中的一致性? 如何将姓名转换为数字以实现排序并保持群组中的一致性? Apr 19, 2025 pm 11:30 PM

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象如何安全地转换为数组? Java对象如何安全地转换为数组? Apr 19, 2025 pm 11:33 PM

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? 电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? Apr 19, 2025 pm 11:27 PM

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名构建查询条件? 使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名构建查询条件? Apr 19, 2025 pm 09:51 PM

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...

See all articles