Azure Functions
プレビューquarkus-azure-functions エクステンションは、Azure Functions と Quarkus のシンプルな統合ポイントです。Azure Functions ランタイムと連携して Quarkus をブートストラップし、記述する Azure Functions クラスを CDI/ArC Bean に変換します。
これにより、Quarkus によって初期化された任意のサービスやコンポーネントを、関数クラスに直接注入できます。関数クラスをシングルトンにしたい場合は、関数クラスのライフサイクルをリクエストスコープ (デフォルト) からアプリケーションスコープに変更することも可能です。
|
この技術は、previewと考えられています。 preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリスト や GitHub の課題管理 で受け付けています。 とりうるステータスの完全なリストについては、 FAQの項目 を参照してください。 |
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();
}
}
}
要件
このガイドを完成させるには、以下が必要です:
-
約15分
-
IDE
-
JDK 17+がインストールされ、
JAVA_HOMEが適切に設定されていること -
Apache Maven 3.9.15
-
使用したい場合は、 Quarkus CLI
-
ネイティブ実行可能ファイルをビルドしたい場合、MandrelまたはGraalVM(あるいはネイティブなコンテナビルドを使用する場合はDocker)をインストールし、 適切に設定していること
-
Azureのアカウント 無料アカウントでも大丈夫です。
-
Azure Functions Core Tools バージョン4.x
ソリューション
このガイドでは、HTTP Trigger Azure Function クラスをデプロイできる Maven プロジェクトの実行方法を説明します。この関数クラスは、クライアントに返される挨拶メッセージを生成する CDI Bean サービスを注入します。
Maven/Gradleプロジェクトの作成
まず、新しいプロジェクトが必要です。以下のコマンドで新規プロジェクトを作成します:
Windowsユーザーの場合:
-
cmdを使用する場合、(バックスラッシュ
\を使用せず、すべてを同じ行に書かないでください)。 -
Powershellを使用する場合は、
-Dパラメータを二重引用符で囲んでください。例:"-DprojectArtifactId=azure-functions-quickstart"
このコマンドは、Quarkus Azure Functions HTTP エクステンションをインポートするプロジェクト構造を生成します。例も含まれています。
プロジェクトの確認
生成されたプロジェクトの pom.xml または build.gradle ビルドファイルを開くと、そのプロジェクトが他の Quarkus プロジェクトと同様であることがわかります。quarkus-azure-functions エクステンションは、Quarkus と Azure Functions の間の統合ポイントです。これは、Azure Functions ランタイムにコールバックを登録して Quarkus をブートストラップし、Quarkus/ArC を関数クラスの関数ファクトリーとして設定します。
quarkus-azure-functions エクステンションの現在の実装では、azure-functions-maven-plugin または Gradle の同等のものが不要になりました。ローカル開発と Azure Functions のパッケージングおよびデプロイは、すべて Quarkus によって行われます。
ビルド設定はすべて application.properties 内にあります。唯一必要な設定スイッチ
は quarkus.azure-functions.app-name です。
Azureデプロイメントディスクリプタ
Azure Functions の`host.json`デプロイメント記述子は自動的に 生成されますが、オーバーライドする必要がある場合は、プロジェクトの root ディレクトリーで宣言し、 準備ができたらビルドを再実行します。
Azure Functionsローカル環境での実行
ローカルの Azure Functions 環境でアプリケーションを試したい場合は、次のコマンドを使用できます。
quarkus run
./mvnw quarkus:run
./gradlew --console=plain --info --no-daemon quarkusRun
|
Gradle はプロセス管理に少し癖があるため、 |
|
これが機能するには、 Azure Functions Core Tools がインストールされている必要があります。 |
例題にアクセスするためのURLは次のようになります:
Quarkus統合テスト
@QuarkusIntegrationTest の機能を使用して統合テストを実装できます。これらの統合テストが実行されると、
統合テストの間、ローカルの Azure Functions 環境がスピンアップされます。
./mvnw verify
./gradlew --info quarkusIntTest
|
Maven で実行する統合テストが |
|
Gradle で実行する統合テストが |
Azureへのデプロイ
quarkus-azure-functions エクステンションは、Azure へのデプロイに関するすべての作業を処理します。デフォルトでは、Quarkus はバックグラウンドで Azure CLI を使用して Azure に対して認証を行い、デプロイします。アカウントに複数のサブスクリプションが関連付けられている場合、application.properties ファイルの quarkus.azure-functions.subscription-id プロパティーを、使用したいサブスクリプションに設定する必要があります。その他の認証メカニズムおよびデプロイオプションについては、設定プロパティー Azure Functions 設定リファレンス を参照してください。
アプリケーションをデプロイするには、プロジェクトをビルドした後、次を実行します:
./mvnw quarkus:deploy
./gradlew --info deploy
|
Gradle の場合、この出力を確認するには |
デプロイメントが成功すると、Quarkus はサンプル関数のエンドポイント URL をコンソールに出力します:
[INFO] HTTP Trigger Urls:
[INFO] HttpExample : https://{appName}.azurewebsites.net/api/httpexample
サービスにアクセスするための URL は次のようになります: https://{appName}.azurewebsites.net/api/HttpExample
Azure Functions 設定リファレンス
ビルド時に固定される設定プロパティ - 他のすべての設定プロパティは実行時にオーバーライド可能
Configuration property |
タイプ |
デフォルト |
|---|---|---|
App name for azure function project. This is required setting. Defaults to the base artifact name Environment variable: Show more |
string |
|
Azure Resource Group for your Azure Functions Environment variable: Show more |
string |
|
Specifies the region where your Azure Functions will be hosted; default value is westus. Valid values Environment variable: Show more |
string |
|
Specifies whether to disable application insights for your function app Environment variable: Show more |
boolean |
|
Specifies the instrumentation key of application insights which will bind to your function app Environment variable: Show more |
string |
|
Valid values are linux, windows, and docker Environment variable: Show more |
string |
|
Should be set to at least the minimum Quarkus compatible version Environment variable: Show more |
string |
|
URL of docker image if deploying via docker Environment variable: Show more |
string |
|
If using docker, url of registry Environment variable: Show more |
string |
|
Description of each type can be found here Valid values are
Defaults to "azure_cli" for authentication Environment variable: Show more |
string |
|
Filesystem path to properties file if using file type Environment variable: Show more |
string |
|
Client or App Id required if using managed_identity type Environment variable: Show more |
string |
|
Tenant ID required if using oauth2 or device_code type Environment variable: Show more |
string |
|
Specifies the name of the existing App Service Plan when you do not want to create a new one. Environment variable: Show more |
string |
|
The app service plan resource group. Environment variable: Show more |
string |
|
Azure subscription id. Required only if there are more than one subscription in your account Environment variable: Show more |
string |
|
The pricing tier. Environment variable: Show more |
string |
|
Port to run azure function in local runtime. Will default to quarkus.http.test-port or 8081 Environment variable: Show more |
int |
|
Config String for local debug Environment variable: Show more |
string |
|