返回

Spring Cloud GCP 适配器参考

发布时间:2022-12-08 04:35:23 351
# java# json# 服务器# 服务器# 工具

Spring Cloud GCP 适配器参考_maven

谷歌云函数

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>

此外,添加 which,这将构建要部署的函数的 JAR。​​spring-boot-maven-plugin​

请注意,我们还引用了 的依赖项。这是必要的,因为它会修改插件,以正确的JAR格式打包您的函数,以便在Google Cloud Functions上部署。​​spring-cloud-function-adapter-gcp​​​​spring-boot-maven-plugin​

<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​

函数目标应始终设置为;这是一个适配器类,充当从 Google Cloud Functions 平台进入 Spring Cloud Function 的入口点。​​org.springframework.cloud.function.adapter.gcp.GcfJarLauncher​

<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示例中找到工作的完整示例。pom.xml

HTTP 函数

Google Cloud Functions 支持部署 HTTPFunctions,即由 HTTP 请求调用的函数。以下部分描述了将 Spring 云函数部署为 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"
部署到基仕伯

首先打包应用程序。

mvn package

如果您添加了上面定义的自定义插件,您应该会看到生成的 JAR 目录。 此 JAR 的格式正确,可以部署到 Google Cloud Functions。​​spring-boot-maven-plugin​​​​target/deploy​

接下来,请确保您已安装云开发工具包 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"

后台函数

Google Cloud Functions 还支持部署后台函数,这些后台函数是间接调用的,以响应事件,例如有关 CloudPub/Sub主题的消息、Cloud Storage存储桶中的更改或Firebase事件。

还允许将函数部署为后台函数。spring-cloud-function-adapter-gcp

以下部分介绍了编写云发布/订阅主题后台函数的过程。 但是,有许多不同的事件类型可以触发后台函数执行,此处不讨论;这些在后台函数触发器文档中进行了描述。

开始

让我们从一个简单的 Spring Cloud 函数开始,它将作为 GCF 后台函数运行:

@SpringBootApplication
public class BackgroundFunctionMain {

public static void main(String[] args) {
SpringApplication.run(BackgroundFunctionMain.class, args);
}

@Bean
public Consumer pubSubFunction() {
return message -> System.out.println("The Pub/Sub message data: " + message.getData());
}
}

此外,在项目中创建类具有以下定义。 此类表示 Pub/Sub 事件结构,该结构在 Pub/Sub 主题事件上传递给函数。​​PubSubMessage​

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,请先打包应用程序。

mvn package

如果您添加了上面定义的自定义插件,您应该会看到生成的 JAR 目录。 此 JAR 的格式正确,可以部署到 Google Cloud Functions。​​spring-boot-maven-plugin​​​​target/deploy​

接下来,请确保您已安装云开发工具包 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 后台函数示例的说明。

示例函数

该项目提供以下示例函数作为参考:

  • 函数-sample-gcp-http是一个HTTP函数,您可以在本地测试并尝试部署。
  • 函数样本 gcp-background显示了一个后台函数的示例,该函数由发布到指定 Pub/Sub 主题的消息触发。
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Spring Cloud Azure 适配器参考 2022-12-08 04:01:44