Google Cloud Functions
Google Cloud Functions 适配器允许 Spring Cloud Function 应用在 Google Cloud Functions 无服务器平台上运行。您可以使用开源 Google Functions Framework for Java 在本地运行函数,或在 GCP 上运行。
项目依赖
首先,将 spring-cloud-function-adapter-gcp 依赖项添加到您的项目中。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
</dependency>
...
</dependencies>
此外,添加 spring-boot-maven-plugin,它将构建要部署的函数的 JAR 包。
请注意,我们还将 spring-cloud-function-adapter-gcp 作为 spring-boot-maven-plugin 的依赖项。这是必要的,因为它会修改插件,以正确的 JAR 格式打包您的函数,以便部署到 Google Cloud Functions。 |
<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 在本地测试您的函数。
函数目标应始终设置为 org.springframework.cloud.function.adapter.gcp.GcfJarLauncher;这是一个适配器类,作为 Spring Cloud Function 从 Google Cloud Functions 平台的入口点。 |
<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>
一个完整的工作 pom.xml 示例可以在 Spring Cloud Functions GCP 示例中找到。
HTTP 函数
Google Cloud Functions 支持部署 HTTP 函数,这些函数通过 HTTP 请求调用。以下部分描述了将 Spring Cloud Function 部署为 HTTP 函数的说明。
入门
让我们从一个简单的 Spring Cloud Function 示例开始
@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 中指定您的配置主类。
Main-Class: com.example.CloudFunctionMain
然后在本地运行函数。这由项目依赖项部分中描述的 Google Cloud Functions function-maven-plugin 提供。
mvn function:run
调用 HTTP 函数
curl https://:8080/ -d "hello"
构建并部署到 GCP
首先打包您的应用程序。
mvn package
如果您添加了上面定义的自定义 spring-boot-maven-plugin 插件,您应该会在 target/deploy 目录中看到生成的 JAR 包。此 JAR 包已正确格式化,可部署到 Google Cloud Functions。
接下来,确保您已安装 Cloud SDK CLI。
从项目根目录运行以下命令进行部署。
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 函数
curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"
设置自定义 HTTP 状态码
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;
};
后台函数
Google Cloud Functions 还支持部署 后台函数,这些函数通过事件间接调用,例如 Cloud Pub/Sub 主题上的消息、Cloud Storage 存储桶中的更改或 Firebase 事件。
spring-cloud-function-adapter-gcp 也允许将函数部署为后台函数。
以下部分描述了编写 Cloud Pub/Sub 主题后台函数的过程。但是,还有许多不同类型的事件可以触发后台函数执行,这里不讨论这些事件;这些在 后台函数触发器文档 中进行了描述。
GCP 入门
让我们从一个简单的 Spring Cloud Function 开始,它将作为 GCF 后台函数运行
@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 事件结构,该结构在 Pub/Sub 主题事件发生时传递给您的函数。
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 中指定您的配置主类。
Main-Class: com.example.BackgroundFunctionMain
然后在本地运行函数。这由项目依赖项部分中描述的 Google Cloud Functions function-maven-plugin 提供。
mvn function:run
调用 HTTP 函数
curl localhost:8080 -H "Content-Type: application/json" -d '{"data":"hello"}'
通过查看日志验证函数是否已调用。
部署到 GCP
为了将您的后台函数部署到 GCP,首先打包您的应用程序。
mvn package
如果您添加了上面定义的自定义 spring-boot-maven-plugin 插件,您应该会在 target/deploy 目录中看到生成的 JAR 包。此 JAR 包已正确格式化,可部署到 Google Cloud Functions。
接下来,确保您已安装 Cloud SDK CLI。
从项目根目录运行以下命令进行部署。
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
Google Cloud Function 现在将在每次消息发布到 --trigger-topic 指定的主题时调用该函数。
有关测试和验证后台函数的详细说明,请参阅运行 GCF 后台函数示例的说明。
示例函数
该项目提供以下示例函数作为参考
-
function-sample-gcp-http 是一个 HTTP 函数,您可以在本地测试并尝试部署。
-
function-sample-gcp-background 展示了一个后台函数的示例,该函数由发布到指定 Pub/Sub 主题的消息触发。