The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

Azure Functions

quarkus-azure-functions エクステンションは、Azure FunctionsとQuarkusの間のシンプルな結合ポイントです。Azure Functionsランタイムと連動してQuarkusをブートストラップし、書いたAzure FunctionsクラスをCDI/Arc Beanに変えることができます。

これにより、Quarkusで初期化されたサービスやコンポーネントを、関数クラスに直接注入することができます。また、関数クラスをシングルトンにしたい場合は、関数クラスのライフサイクルをリクエストスコープ(デフォルト)からアプリケーションスコープに変更することも可能です。

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();
        }
    }
}

この技術は、previewと考えられています。

preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリストGitHub の課題管理 で受け付けています。

とりうるステータスの完全なリストについては、 FAQの項目 を参照してください。

前提条件

このガイドを完成させるには、以下が必要です:

ソリューション

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.

Maven/Gradleプロジェクトの作成

Quarkusのオンラインアプリケーションジェネレーターから、この リンク でサンプルコードを生成できます。

この例は、Quarkus CLIで生成することもできます:

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

gradleプロジェクトを生成したい場合は、 --gradle スイッチを追加します。

プロジェクトの確認

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.

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.

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

Azureデプロイメントディスクリプタ

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開発モード

Quarkusの開発モードは、現在Azure Functionsでは動作しません。

Azure Functionsローカル環境での実行

ローカルの Azure Functions 環境でアプリを試したい場合は、次のコマンドを使用できます。

./mvnw quarkus:run

or

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

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.

なお、これを実行するためには、 Azure Functions Core Tools がインストールされている必要があります!

例題にアクセスするためのURLは次のようになります:

Quarkus統合テスト

@QuarkusIntegrationTest の機能を使用して統合テストを実装できます。これらの統合テストが実行されると、統合テストの間、ローカルの Azure Functions 環境がスピンアップされます。

Mavenの場合:

./mvnw -DskipITs=false verify

maven で実行する統合テストが *IT.java ファイルパターンを使用しているようにし、通常のビルドでテストが実行されないようにします。

Gradleの場合:

./gradlew --info quarkusIntTest

Gradle で実行する統合テストが src/integrationTest/java 内にあることを確認してください。 src/test に存在する統合テストは通常のビルドで実行され、失敗します。

Azureへのログイン

Azureにログインしないとデプロイできません。

az login

Azureにデプロイ

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.

デプロイを実行するには、プロジェクトをビルドした後、次のコマンドを実行してください:

./mvnw quarkus:deploy

or

./gradlew --info deploy

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!

例えば

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

サービスにアクセスするためのURLは次のようになります。

関連コンテンツ