Azure Functions

quarkus-azure-functions 扩展是一个 Azure Functions 和 Quarkus 之间的简单集成点。它与 Azure Functions 运行时交互,以引导 Quarkus 并将您编写的任何 Azure Functions 类转换为 CDI/Arc Bean。

The quarkus-azure-functions extension is a simple integration point between Azure Functions and Quarkus. It interacts with Azure Functions runtime to bootstrap quarkus and turns any Azure Functions class you write into a CDI/Arc bean.

这使您可以将由 Quarkus 初始化的任何服务或组件直接注入到您函数类中。如果要将函数类设为单例类,还可以将函数类的生命周期从请求作用域(默认)更改为应用程序作用域。

This allows you to inject any service or component initialized by quarkus directly into your function classes. You can also change the lifecycle of your function class from request scoped (the default) to application scope too if you want your function class to be a singleton.

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import jakarta.inject.Inject;
import java.util.Optional;

public class Function {
    @Inject
    GreetingService service;

    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        // Parse query parameter
        final String query = request.getQueryParameters().get("name");
        final String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
        } else {
            return request.createResponseBuilder(HttpStatus.OK).body(service.greeting(name)).build();
        }
    }
}

Unresolved directive in azure-functions.adoc - include::{includes}/extension-status.adoc[]

Prerequisites

Unresolved directive in azure-functions.adoc - include::{includes}/prerequisites.adoc[]* An Azure Account。免费帐户有效。* Azure Functions Core Tools 版本 4.x* Azure CLI Installed

Unresolved directive in azure-functions.adoc - include::{includes}/prerequisites.adoc[] * An Azure Account. Free accounts work. * Azure Functions Core Tools version 4.x * Azure CLI Installed

Solution

本指南引导您运行可部署 HTTP 触发器 Azure 函数类的 Maven 项目。此函数类注入 CDI Bean 服务,该服务生成传递给客户端的问候消息。

This guide walks you through running a maven project that can deploy an Http Trigger Azure Function class. This function class injects a CDI bean service that generates a greeting message that is passed back to the client.

Creating the Maven/Gradle Project

您可以在 this link 上从 Quarkus 的在线应用程序生成器生成示例代码。

You can generate the example code from Quarkus’s online application generator at this link.

你还可以使用 Quarkus CLI 生成此示例:

You can also generate this example with the Quarkus CLI:

quarkus create app --extension=quarkus-azure-functions

如果你想生成 gradle 项目,请添加 @“1” 开关。

Add the --gradle switch if you want to generate a gradle project.

Examining the project

如果你打开生成的项目中的 @“2” 或 @“3” 构建文件,你将看到该项目与任何其他 Quarkus 项目相似。@“4” 扩展是 Quarkus 和 Azure Functions 之间的集成点。它用 Azure Functions 运行时注册回调以引导 Quarkus 并将 Quarkus/Arc 设置为功能类的功能工厂。

If you open the pom.xml or build.gradle build file of the generated project you’ll see that the project is similar to any other Quarkus project. The quarkus-azure-functions extension is the integration point between Quarkus and Azure Functions. It registers callback with the Azure Functions runtime to bootstrap Quarkus and to set up Quarkus/Arc as the function factory for your function classes.

@“5” 扩展的当前实现不再需要 @“6” 或 gradle 等效项。本地开发和 Azure Functions 打包和部署现在都由 Quarkus 完成。

The current implementation of the quarkus-azure-functions extension no longer requires the azure-functions-maven-plugin or gradle equivalent. Local development and Azure Functions packaging and deployment is now all done by Quarkus.

构建配置现在都在 @“7” 内。唯一必需的配置开关是 @“8”。

Build configuration is now all within application.properties. The only required configuration switch is quarkus.azure-functions.app-name.

Azure Deployment Descriptors

Azure Functions @“9” 部署描述符自动生成,但如果你需要覆盖它,请在项目的根目录中声明它并在准备就绪时重新运行构建。

The Azure Functions host.json deployment descriptor is automatically generated, but if you need to override it, declare it in the root directory of the project and rerun the build when you are ready.

Quarkus dev mode

Quarkus 开发模式当前不适用于 Azure Functions。

Quarkus dev mode does not work currently with Azure Functions.

Run locally in Azure Functions local environment

如果你想使用本地 Azure Functions 环境试用你的应用,你可以使用此命令

If you want to try your app with a local Azure Functions environment, you can use this command

./mvnw quarkus:run

or

./gradlew --info --no-daemon quarkusRun

Gradle 在进程管理方面有点怪异,所以你需要 @“10” 开关或 ctrl-c 不会干净地销毁进程,你将会有打开的端口。

Gradle is a bit quirky with process management, so you need the --no-daemon switch or control-c will not destroy the process cleanly and you’ll have open ports.

请注意,你必须安装 @“11” 才能使此项工作!

Note that you must have the Azure Functions Core Tools installed for this to work!

访问该示例的 URL 为:

The URL to access the example would be:

[role="bare"]@“12”

Quarkus Integration Testing

你可以使用 @“13” 功能实现集成测试。当这些集成测试运行时,本地 Azure Functions 环境将为集成测试期间启动。

You can implement integration tests using @QuarkusIntegrationTest functionality. When these integration tests run, the local Azure Functions environment will be spun up for the duration of integration testing.

对于 maven:

For maven:

./mvnw -DskipITs=false verify

确保你使用 maven 执行的任何集成测试都使用 @“14” 文件模式,以便常规构建不执行测试。

Make sure any integration tests you execute with maven use the *IT.java file pattern so that regular builds do not execute the test.

对于 Gradle:

For Gradle:

./gradlew --info quarkusIntTest

确保你使用 Gradle 执行的任何集成测试都位于 @“15” 中。存在于 @“16” 中的集成测试将随常规构建一起运行并失败。

Make sure any integration tests you execute with Gradle are located within src/integrationTest/java. Integration tests that exist in src/test will run with normal build and fail.

Login to Azure

如果你不登录到 Azure,你将无法部署。

If you don’t log in to Azure you won’t be able to deploy.

az login

Deploy to Azure

@“17” 扩展处理所有部署到 Azure 的工作。默认情况下,Quarkus 将在后台使用 Azure CLI 来验证和部署到 Azure。如果你有多个订阅与你的帐户相关联,你必须将 @“19” 文件中的 @“18” 属性设置为你要使用的订阅。有关其他身份验证机制和部署选项,请参阅我们的配置文件 @“20”。

The quarkus-azure-functions extension handles all the work to deploy to Azure. By default, Quarkus will use the Azure CLI in the background to authenticate and deploy to Azure. If you have multiple subscriptions associated with your account, you must set the quarkus.azure-functions.subscription-id property in your application.properties file to the subscription you want to use. For other authentication mechanisms and deployment options see our config properties here.

在构建好项目后,执行以下命令可运行该部署:

To run the deploy, after you build your project execute:

./mvnw quarkus:deploy

or

./gradlew --info deploy

如果部署成功,Quarkus 会将示例函数的端点 URL 输出到控制台。对于 Gradle,您必须使用 --info 开关才能看到此输出!

If deployment is a success, Quarkus will output the endpoint URL of the example function to the console For Gradle, you must use the --info switch to see this output!

i.e.

[INFO] HTTP Trigger Urls:
[INFO] 	 HttpExample : https://{appName}.azurewebsites.net/api/httpexample

访问该服务的 URL 应为

The URL to access the service would be