Google Cloud Functions (Serverless) with Quarkus REST, Undertow, or Reactive Routes
利用 `quarkus-google-cloud-functions-http`扩展,您能够利用 Quarkus REST(Jakarta REST)、Undertow(Servlet)、反应式路由或 Funqy HTTP编写微服务,并且能够将这些微服务部署到 Google Cloud Functions 运行时。
The quarkus-google-cloud-functions-http
extension allows you to write microservices with Quarkus REST (Jakarta REST),
Undertow (Servlet), Reactive Routes, or Funqy HTTP, and make these microservices deployable to the Google Cloud Functions runtime.
一次 Google Cloud Functions 部署可以代表任意数量的 Jakarta REST、Servlet、反应式路由或 Funqy HTTP端点。
One Google Cloud Functions deployment can represent any number of Jakarta REST, Servlet, Reactive Routes, or Funqy HTTP endpoints. Unresolved directive in gcp-functions-http.adoc - include::{includes}/extension-status.adoc[]
Prerequisites
Unresolved directive in gcp-functions-http.adoc - include::{includes}/prerequisites.adoc[]* A Google Cloud Account。免费帐户可用。* Cloud SDK CLI Installed
Unresolved directive in gcp-functions-http.adoc - include::{includes}/prerequisites.adoc[] * A Google Cloud Account. Free accounts work. * Cloud SDK CLI Installed
Solution
本指南将引导您生成一个示例项目,然后创建三个使用 Jakarta REST API、Servlet API、反应式路由或 Funqy HTTPAPI 编写的 HTTP 端点。构建完成之后,您将能够将此项目部署到 Google Cloud。
This guide walks you through generating a sample project followed by creating three HTTP endpoints written with Jakarta REST APIs, Servlet APIs, Reactive Routes, or Funqy HTTP APIs. Once built, you will be able to deploy the project to Google Cloud.
如果您不想执行上述所有步骤,您可以直接转到完成的示例。
If you don’t want to follow all these steps, you can go right to the completed example.
克隆 Git 存储库: git clone {quickstarts-clone-url}
,或下载 {quickstarts-archive-url}[存档]。
Clone the Git repository: git clone {quickstarts-clone-url}
, or download an {quickstarts-archive-url}[archive].
解决方案位于 google-cloud-functions-http-quickstart
directory中。
The solution is located in the google-cloud-functions-http-quickstart
directory.
Creating the Maven Deployment Project
使用 `quarkus-google-cloud-functions-http`扩展创建一个应用程序。您可以使用以下 Maven 命令来创建它:
Create an application with the quarkus-google-cloud-functions-http
extension.
You can use the following Maven command to create it:
Unresolved directive in gcp-functions-http.adoc - include::{includes}/devtools/create-app.adoc[]
Login to Google Cloud
部署应用程序需要登录 Google Cloud。可以按以下方式进行操作:
Login to Google Cloud is necessary for deploying the application. It can be done as follows:
gcloud auth login
Creating the endpoints
对于这个示例项目,我们将创建四个端点:一个用于 Quarkus REST(Jakarta REST)、一个用于 Undertow(Servlet)、一个用于反应式路由以及一个用于 Funqy HTTP。
For this example project, we will create four endpoints, one for Quarkus REST (Jakarta REST), 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. |
如果您不需要每种类型的端点,则可以从 `pom.xml`中移除相应的扩展。
If you don’t need endpoints of each type, you can remove the corresponding extensions from your pom.xml
.
Quarkus 支持 Cloud Functions 第 1 代和第 2 代。有关 Cloud Functions 第 2 代的概述,请参阅 Google Cloud Functions 文档上的 this page。要使用第 2 代,您必须添加 `--gen2`参数。 |
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 and add the |
The Jakarta REST endpoint
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from Quarkus REST";
}
}
The Servlet endpoint
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.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);
}
}
The Reactive Routes endpoint
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");
}
}
Build and Deploy to Google Cloud
Quarkus 强制打包您的函数,其中类型为 |
Quarkus forces a packaging of type |
使用标准 `mvn clean package`命令打包您的应用程序。前一条命令的结果是 `target/deployment`目录中包含该项目的类和依赖项的单个 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.
然后您将能够使用 `gcloud`将您的函数部署到 Google Cloud。
Then you will be able to use gcloud
to deploy your function to Google Cloud.
我们将使用 Java 17 运行时,但你可以通过在部署命令中使用 |
We will use the Java 17 runtime but you can switch to the Java 21 runtime by using |
gcloud functions deploy quarkus-example-http \
--entry-point=io.quarkus.gcp.functions.http.QuarkusHttpFunction \
--runtime=java17 --trigger-http --allow-unauthenticated --source=target/deployment
入口点始终必须设置为 io.quarkus.gcp.functions.http.QuarkusHttpFunction
,因为这是一个将 Cloud Functions 和 Quarkus 集成的类。
The entry point must always be set to io.quarkus.gcp.functions.http.QuarkusHttpFunction
as this is the class that integrates Cloud Functions with Quarkus.
当你第一次启动此命令时,会出现以下错误信息:
The first time you launch this command, you can have the following error message:
ERROR: (gcloud.functions.deploy) OperationError: code=7, message=Build Failed: Cloud Build has not been used in project <project_name> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview?project=<my-project> then retry.
这意味着 Cloud Build 尚未激活。要解决此错误,请打开错误中显示的网址,按照说明操作,然后等待几分钟再重试该命令。
This means that Cloud Build is not activated yet. To overcome this error, open the URL shown in the error, follow the instructions and then wait a few minutes before retrying the command.
此命令将生成一个 httpsTrigger.url
,指向你的函数。
This command will give you as output a httpsTrigger.url
that points to your function.
您随后可以这样调用您的端点:
You can then call your endpoints via:
-
For Jakarta REST: {httpsTrigger.url}/hello
-
For servlet: {httpsTrigger.url}/servlet/hello
-
For Reactive Routes: {httpsTrigger.url}/vertx/hello
-
For Funqy: {httpsTrigger.url}/funqy
Testing locally
本地测试函数的最简单方法是使用 Cloud Function Invoker JAR。
The easiest way to locally test your function is using the Cloud Function invoker JAR.
你可以使用以下命令通过 Maven 下载它:
You can download it via Maven using the following command:
mvn dependency:copy \
-Dartifact='com.google.cloud.functions.invoker:java-function-invoker:{gcf-invoker-version}' \
-DoutputDirectory=.
在使用 invoker 前,您首先需要通过 `mvn package`构建您的函数。
Before using the invoker, you first need to build your function via mvn package
.
然后,您可以使用它在本地启动您的函数。
Then you can use it to launch your function locally.
java -jar java-function-invoker-{gcf-invoker-version}.jar \
--classpath target/deployment/google-cloud-functions-http-1.0.0-SNAPSHOT-runner.jar \
--target io.quarkus.gcp.functions.http.QuarkusHttpFunction
`--classpath`参数需要设置为先前打包的 JAR,其中包含您的函数类和所有 Quarkus 相关类。
The --classpath
parameter needs to be set to the previously packaged JAR that contains your function class and all Quarkus related classes.
您的端点将在 [role="bare"][role="bare"]http://localhost:8080上提供。
Your endpoints will be available on [role="bare"]http://localhost:8080.
What’s next?
您可以使用我们的 Google Cloud Functions Funqy binding来使用 Funqy(一个与提供商无关的函数即服务框架),该服务允许将 HTTP 函数或后台函数部署到 Google Cloud。
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.