Google Cloud Functions
Google Cloud 函数适配器使 Spring Cloud 功能应用程序能够运行在 Google Cloud Functions无服务器平台上。您可以使用开源 Google Functions Framework for Java在本地运行功能,也可以在 GCP 上运行。
The Google Cloud Functions adapter enables Spring Cloud Function apps to run on the Google Cloud Functions serverless platform. You can either run the function locally using the open source Google Functions Framework for Java or on GCP.
Project Dependencies
首先,将 spring-cloud-function-adapter-gcp
依赖项添加到您的项目。
Start by adding the spring-cloud-function-adapter-gcp
dependency to your project.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
</dependency>
...
</dependencies>
此外,添加 spring-boot-maven-plugin
,它将构建函数的 JAR 以进行部署。
In addition, add the spring-boot-maven-plugin
which will build the JAR of the function to deploy.
请注意,我们还引用 |
Notice that we also reference |
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<outputDirectory>target/deploy</outputDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
</dependency>
</dependencies>
</plugin>
最后,添加作为 Google Functions Framework for Java 的一部分提供的 Maven 插件。这允许您通过 mvn function:run
在本地测试您的函数。
Finally, add the Maven plugin provided as part of the Google Functions Framework for Java.
This allows you to test your functions locally via mvn function:run
.
函数目标应始终设置为 |
The function target should always be set to |
<plugin>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.1</version>
<configuration>
<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
<port>8080</port>
</configuration>
</plugin>
您可以在 Spring Cloud Functions GCP sample中找到一个正在运行的 `pom.xml`及其完整示例。
A full example of a working pom.xml
can be found in the Spring Cloud Functions GCP sample.
HTTP Functions
Google Cloud 函数支持部署 HTTP Functions,这些函数由 HTTP 请求调用。以下部分描述了如何将 Spring Cloud 函数部署为 HTTP 函数的说明。
Google Cloud Functions supports deploying HTTP Functions, which are functions that are invoked by HTTP request. The sections below describe instructions for deploying a Spring Cloud Function as an HTTP Function.
Getting Started
让我们从一个简单的 Spring Cloud Function 示例开始:
Let’s start with a simple Spring Cloud Function example:
@SpringBootApplication
public class CloudFunctionMain {
public static void main(String[] args) {
SpringApplication.run(CloudFunctionMain.class, args);
}
@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
}
在 resources/META-INF/MANIFEST.MF
中指定您的配置主类。
Specify your configuration main class in resources/META-INF/MANIFEST.MF
.
Main-Class: com.example.CloudFunctionMain
然后在本地运行函数。这是通过项目依赖关系部分中所述的 Google Cloud Functions function-maven-plugin
提供的。
Then run the function locally.
This is provided by the Google Cloud Functions function-maven-plugin
described in the project dependencies section.
mvn function:run
调用 HTTP 函数:
Invoke the HTTP function:
curl http://localhost:8080/ -d "hello"
Buikd & Deploy to GCP
首先打包您的应用程序。
Start by packaging your application.
mvn package
如果您添加了上面定义的自定义 spring-boot-maven-plugin
插件,您应该在 target/deploy
目录中看到结果 JAR。此 JAR 经正确格式化,可部署到 Google Cloud Functions。
If you added the custom spring-boot-maven-plugin
plugin defined above, you should see the resulting JAR in target/deploy
directory.
This JAR is correctly formatted for deployment to Google Cloud Functions.
接下来,请确保已安装 Cloud SDK CLI。
Next, make sure that you have the Cloud SDK CLI installed.
从项目基本目录运行以下命令进行部署。
From the project base directory run the following command to deploy.
gcloud functions deploy function-sample-gcp-http \ --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \ --runtime java11 \ --trigger-http \ --source target/deploy \ --memory 512MB
调用 HTTP 函数:
Invoke the HTTP function:
curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"
设置自定义 HTTP statusCode:
Setting custom HTTP statusCode:
Functions can specify a custom HTTP response code by setting the `FunctionInvoker.HTTP_STATUS_CODE` header.
@Bean
public Function<String, Message<String>> function() {
String payload = "hello";
Message<String> message = MessageBuilder.withPayload(payload).setHeader(FunctionInvoker.HTTP_STATUS_CODE, 404).build();
return input -> message;
};
Background Functions
Google Cloud 函数还支持部署 Background Functions,这些函数是响应某个事件进行间接调用的,例如 Cloud Pub/Sub主题上的消息、 Cloud Storage存储桶中的更改或 Firebase事件。
Google Cloud Functions also supports deploying Background Functions which are invoked indirectly in response to an event, such as a message on a Cloud Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.
spring-cloud-function-adapter-gcp
还允许将函数部署为后台函数。
The spring-cloud-function-adapter-gcp
allows for functions to be deployed as background functions as well.
以下部分描述了编写 Cloud Pub/Sub 主题后台函数的过程。但是,有许多不同的事件类型可以触发后台函数的执行,这里未对其进行讨论;这些内容在 Background Function triggers documentation中进行了描述。
The sections below describe the process for writing a Cloud Pub/Sub topic background function. However, there are a number of different event types that can trigger a background function to execute which are not discussed here; these are described in the Background Function triggers documentation.
GCP Getting Started
我们从一个简单的 Spring Cloud 函数开始,它将作为 GCF 后台函数运行:
Let’s start with a simple Spring Cloud Function which will run as a GCF background function:
@SpringBootApplication
public class BackgroundFunctionMain {
public static void main(String[] args) {
SpringApplication.run(BackgroundFunctionMain.class, args);
}
@Bean
public Consumer<PubSubMessage> pubSubFunction() {
return message -> System.out.println("The Pub/Sub message data: " + message.getData());
}
}
此外,请使用以下定义在项目中创建 `PubSubMessage`类。此类表示 Pub/Sub event structure,该 Pub/Sub event structure会在 Pub/Sub 主题事件中传递给您的函数。
In addition, create PubSubMessage
class in the project with the below definition.
This class represents the Pub/Sub event structure which gets passed to your function on a Pub/Sub topic event.
public class PubSubMessage {
private String data;
private Map<String, String> attributes;
private String messageId;
private String publishTime;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
}
在 resources/META-INF/MANIFEST.MF
中指定您的配置主类。
Specify your configuration main class in resources/META-INF/MANIFEST.MF
.
Main-Class: com.example.BackgroundFunctionMain
然后在本地运行函数。这是通过项目依赖关系部分中所述的 Google Cloud Functions function-maven-plugin
提供的。
Then run the function locally.
This is provided by the Google Cloud Functions function-maven-plugin
described in the project dependencies section.
mvn function:run
调用 HTTP 函数:
Invoke the HTTP function:
curl localhost:8080 -H "Content-Type: application/json" -d '{"data":"hello"}'
通过查看日志确认函数是否已被调用。
Verify that the function was invoked by viewing the logs.
Deploy to GCP
若要将后台函数部署到 GCP,请首先打包应用程序。
In order to deploy your background function to GCP, first package your application.
mvn package
如果您添加了上面定义的自定义 spring-boot-maven-plugin
插件,您应该在 target/deploy
目录中看到结果 JAR。此 JAR 经正确格式化,可部署到 Google Cloud Functions。
If you added the custom spring-boot-maven-plugin
plugin defined above, you should see the resulting JAR in target/deploy
directory.
This JAR is correctly formatted for deployment to Google Cloud Functions.
接下来,请确保已安装 Cloud SDK CLI。
Next, make sure that you have the Cloud SDK CLI installed.
从项目基本目录运行以下命令进行部署。
From the project base directory run the following command to deploy.
gcloud functions deploy function-sample-gcp-background \ --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \ --runtime java11 \ --trigger-topic my-functions-topic \ --source target/deploy \ --memory 512MB
每次向 --trigger-topic
指定的主题发布消息时,Google Cloud Function 现在都会调用该函数。
Google Cloud Function will now invoke the function every time a message is published to the topic specified by --trigger-topic
.
有关测试和验证后台函数的演练,请参阅运行 GCF Background Function sample 的说明。
For a walkthrough on testing and verifying your background function, see the instructions for running the GCF Background Function sample.
Sample Functions
该项目提供以下示例函数供参考:
The project provides the following sample functions as reference:
-
The function-sample-gcp-http is an HTTP Function which you can test locally and try deploying.
-
The function-sample-gcp-background shows an example of a background function that is triggered by a message being published to a specified Pub/Sub topic.