Google Cloud Functions (Serverless) with RESTEasy, Undertow, or Reactive Routes
quarkus-google-cloud-functions-http
エクステンションは、RESTEasy(JAX-RS)、Undertow(Servlet)、Reactive Routes、 Funqy HTTP でマイクロサービスを記述し、これらのマイクロサービスをGoogle Cloud Functionsランタイムにデプロイ可能にします。
1つのGoogle Cloud Functionsのデプロイメントは、JAX-RS、Servlet、Reactive Routes、 FunqyのHTTP エンドポイントをいくつでも表すことができます。
この技術は、previewと考えられています。 preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリスト や GitHub の課題管理 で受け付けています。 とりうるステータスの完全なリストについては、 FAQの項目 を参照してください。 |
前提条件
このガイドを完成させるには、以下が必要です:
-
約15分
-
IDE
-
JDK 11+ がインストールされ、
JAVA_HOME
が適切に設定されていること -
Apache Maven 3.8.6
-
使用したい場合は、 Quarkus CLI
-
Google Cloudのアカウント 。無料アカウントが使えます。
ソリューション
このガイドでは、サンプルプロジェクトを生成し、JAX-RS API、Servlet API、Reactive Routes、または Funqy HTTP APIで記述された3つのHTTPエンドポイントを作成する手順を説明します。構築後は、プロジェクトをGoogle Cloudにデプロイします。
これらの手順を順にすべて実行しない場合、完成したサンプルを以下で確認できます。
Gitレポジトリをクローンするか git clone https://github.com/quarkusio/quarkus-quickstarts.git
、 アーカイブ をダウンロードします。
このソリューションは、 google-cloud-functions-http-quickstart
ディレクトリ にあります。
Maven デプロイメントプロジェクトの作成
quarkus-google-cloud-functions-http
のエクステンションを持つアプリケーションを作成します。以下のMavenコマンドで作成することができます:
エンドポイントの作成
このサンプルプロジェクトでは、RESTEasy(JAX-RS)用、Undertow(Servlet)用、Reactive routes用、 Funqy HTTP 用の4つのエンドポイントを作成します。
これらの様々なエンドポイントは、デモンストレーションのためのものです。実際のアプリケーションでは、これらの技術の中から1つを選び、それにこだわるべきです。 |
各タイプのエンドポイントが不要な場合は、対応するエクステンションを pom.xml
から削除することができます。
Quarkusは、Cloud Functionsの第1世代と第2世代をサポートしています。Cloud Functions gen 2の概要については、Google Cloud Functionsのドキュメントの このページを 参照してください。gen 2を使用するには、 --gen2 パラメータを追加する必要があります。
|
JAX-RS エンドポイント
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from RESTEasy Reactive";
}
}
サーブレットエンドポイント
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "ServletGreeting", urlPatterns = "/servlet/hello")
public class GreetingServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(200);
resp.addHeader("Content-Type", "text/plain");
resp.getWriter().write("hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getReader().readLine();
resp.setStatus(200);
resp.addHeader("Content-Type", "text/plain");
resp.getWriter().write("hello " + name);
}
}
Reactive Routesエンドポイント
import static io.quarkus.vertx.web.Route.HttpMethod.GET;
import io.quarkus.vertx.web.Route;
import io.vertx.ext.web.RoutingContext;
public class GreetingRoutes {
@Route(path = "/vertx/hello", methods = GET)
void hello(RoutingContext context) {
context.response().headers().set("Content-Type", "text/plain");
context.response().setStatusCode(200).end("hello");
}
}
ビルドとGoogle Cloudへのデプロイ
Google Cloud FunctionのデプロイにはシングルJARが必要なため、Quarkusでは強制的に uber-jar タイプのパッケージを作成しています。
|
標準の mvn clean package
コマンドを使用して、アプリケーションをパッケージ化します。前述のコマンドの結果、 target/deployment
ディレクトリ内に、プロジェクトのクラスと依存関係を含む単一の JAR ファイルが作成されます。
そして、 gcloud
を使って、機能を Google Cloud にデプロイすることができるようになります。
We will use the Java 17 runtime but you can switch to the Java 11 runtime by using --runtime=java11 instead of --runtime=java17 on the deploy commands.
|
gcloud functions deploy quarkus-example-http \
--entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \
--runtime=java17 --trigger-http --allow-unauthenticated --source=target/deployment
このクラスはCloud FunctionsとQuarkusを統合するクラスであるため、エントリーポイントは常に |
このコマンドを初めて起動したときには、以下のようなエラーメッセージが表示されることがあります:
これは、Cloud Buildがまだ有効化されていないことを意味します。このエラーを解決するには、エラーに表示されているURLを開き指示に従った後、数分待ってからコマンドを再試行してください。 |
このコマンドは、作成したCloud Functions 関数をトリガーするための httpsTrigger.url
を出力します。
そして、エンドポイントを経由して呼び出すことができます:
-
JAX-RSの場合: {httpsTrigger.url}/hello
-
サーブレットの場合: {httpsTrigger.url}/servlet/hello
-
リアクティブルートの場合: {httpsTrigger.url}/vertx/hello
-
Funqyの場合: {httpsTrigger.url}/funqy
ローカルでのテスト
関数をローカルでテストする最も簡単な方法は、Cloud Function invoker JAR を使用することです。
以下のコマンドでMaven経由でダウンロードできます:
mvn dependency:copy \
-Dartifact='com.google.cloud.functions.invoker:java-function-invoker:1.1.1' \
-DoutputDirectory=.
invokerを使用する前に、mvn package
で関数をビルドする必要があります。
そして、それを使ってローカルに機能を立ち上げることができます。
java -jar java-function-invoker-1.1.1.jar \
--classpath target/deployment/google-cloud-functions-http-1.0.0-SNAPSHOT-runner.jar \
--target io.quarkus.gcp.functions.http.QuarkusHttpFunction
--classpath パラメーターには、関数クラスとQuarkus関連のすべてのクラスを含む、先の手順でパッケージ化されたJARを指定する必要があります。
|
エンドポイントは http://localhost:8080 で利用できます。
次のステップ?
Google Cloud Functions Funqy バインディング を使用すると、プロバイダに依存しない Service as a Service フレームワークである Funqy を使用することができ、HTTP 関数や Background 関数を Google Cloud にデプロイすることができます。