|
此版本仍在开发中,目前尚不被视为稳定版本。如需最新稳定版本,请使用 spring-cloud-function 5.0.1! |
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 http://localhost:8080/ -d "hello"
构建和部署到 GCP
首先打包你的应用程序。
mvn package
如果你添加了上面定义的自定义spring-boot-maven-plugin插件,你应该会看到JAR文件位于target/deploy目录中。
此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 事件。
The 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插件,你应该会看到JAR文件位于target/deploy目录中。
此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 Functions 将现在对指定的主题调用函数。
有关测试和验证后台功能的演练,请参阅运行
样例功能
该项目提供了以下示例功能作为参考:
-
The function-sample-gcp-http is an HTTP Function which you can test locally and try deploying.
-
The function-sample-gcp-background 展示了一个当消息被发布到指定的 Pub/Sub 主题时触发的后台函数示例。