Google Cloud Functions (Serverless) with RESTEasy Reactive, Undertow, or Reactive Routes
The quarkus-google-cloud-functions-http
extension allows you to write microservices with RESTEasy Reactive (JAX-RS), Undertow (Servlet), Reactive Routes, or Funqy HTTP, and make these microservices deployable to the Google Cloud Functions runtime.
One Google Cloud Functions deployment can represent any number of JAX-RS, Servlet, Reactive Routes, or Funqy HTTP endpoints.
この技術は、previewと考えられています。 preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリスト や GitHub の課題管理 で受け付けています。 For a full list of possible statuses, check our FAQ entry. |
前提条件
このガイドを完成させるには、以下が必要です:
-
約15分
-
IDE
-
JDK 11+ がインストールされ、
JAVA_HOME
が適切に設定されていること -
Apache Maven 3.8.1+
-
使用したい場合、 Quarkus CLI
-
A Google Cloud Account. Free accounts work.
ソリューション
This guide walks you through generating a sample project followed by creating three HTTP endpoints written with JAX-RS APIs, Servlet APIs, Reactive Routes, or Funqy HTTP APIs. Once built, you will be able to deploy the project to Google Cloud.
これらの手順を順にすべて実行しない場合、完成したサンプルを以下で確認できます。
Gitレポジトリをクローンするか git clone https://github.com/quarkusio/quarkus-quickstarts.git
、 アーカイブ をダウンロードします。
The solution is located in the google-cloud-functions-http-quickstart
directory.
Maven デプロイメントプロジェクトの作成
Create an application with the quarkus-google-cloud-functions-http
extension. You can use the following Maven command to create it:
Google Cloudにログイン
Login to Google Cloud is necessary for deploying the application. It can be done as follows:
gcloud auth login
エンドポイントの作成
For this example project, we will create four endpoints, one for RESTEasy (JAX-RS), one for Undertow (Servlet), one for Reactive routes and one for Funqy HTTP.
These various endpoints are for demonstration purposes. For real life applications, you should choose one of this technology and stick to it. |
If you don’t need endpoints of each type, you can remove the corresponding extensions from your pom.xml
.
Quarkus supports Cloud Functions gen 1 and gen 2. For an overview of Cloud Functions gen 2 see this page on the Google Cloud Functions documentation. To use gen 2 you must use gcloud beta command and add the --gen2 parameter.
|
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へのデプロイ
Quarkus forces a packaging of type uber-jar for your function as Google Cloud Function deployment requires a single JAR.
|
Package your application using the standard mvn clean package
command. The result of the previous command is a single JAR file inside the target/deployment
directory that contains the classes and the dependencies of the project.
Then you will be able to use gcloud
to deploy your function to Google Cloud.
gcloud functions deploy quarkus-example-http \
--entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \
--runtime=java11 --trigger-http --source=target/deployment
The entry point must always be set to |
You can also use the new Java 17 runtime by using |
このコマンドを初めて起動したときには、以下のようなエラーメッセージが表示されることがあります。
これは、Cloud Buildがまだ有効化されていないことを意味します。このエラーを解決するには、エラーに表示されているURLを開き指示に従った後、数分待ってからコマンドを再試行してください。 |
このコマンドは、作成したCloud Functions 関数をトリガーするための httpsTrigger.url
を出力します。
You can then call your endpoints via:
-
For JAX-RS: {httpsTrigger.url}/hello
-
For servlet: {httpsTrigger.url}/servlet/hello
-
For Reactive Routes: {httpsTrigger.url}/vertx/hello
-
For Funqy: {httpsTrigger.url}/funqy
ローカルでのテスト
関数をローカルでテストする最も簡単な方法は、Cloud Function invoker JAR を使用することです。
以下のコマンドでMaven経由でダウンロードできます。
mvn dependency:copy \
-Dartifact='com.google.cloud.functions.invoker:java-function-invoker:1.1.0' \
-DoutputDirectory=.
invokerを使用する前に、mvn package
で関数をビルドする必要があります。
Then you can use it to launch your function locally.
java -jar java-function-invoker-1.1.0.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 で利用できます。
次のステップ
You can use our Google Cloud Functions Funqy binding to use Funqy, a provider-agnostic function as a service framework, that allow to deploy HTTP function or Background function to Google Cloud.