155. Spring 云流

Spring Cloud GCP 为 Google Cloud Pub/Sub 提供了Spring 云流Binder。

提供的 Binder 依赖于适用于 Google Cloud Pub/Sub 的 Spring Integration Channel 适配器

使用 Spring Cloud GCP BOM 进行 Maven 坐标:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-pubsub-stream-binder</artifactId>
</dependency>

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-pubsub-stream-binder'
}

155.1 Overview

该资料夹将生产者绑定到 Google Cloud Pub/Sub 主题,将 Consumer 绑定到订阅。

Note

该绑定器当前不支持分区。

155.2 Configuration

您可以为 Google Cloud Pub/Sub 配置 Spring Cloud Stream Binder 以自动生成基础资源,例如针对生产者和 Consumer 的 Google Cloud Pub/Sub 主题和订阅。为此,您可以使用spring.cloud.stream.gcp.pubsub.bindings.<channelName>.<consumer|producer>.auto-create-resources属性,该属性默认情况下处于打开状态。

从版本 1.1 开始,可以为所有绑定全局配置这些和其他绑定程序属性,例如spring.cloud.stream.gcp.pubsub.default.consumer.auto-create-resources

如果您正在使用 Spring Cloud GCP Pub/Sub Starter 中的发布/订阅自动配置,则应参考configuration部分以获取其他发布/订阅参数。

Note

要将此绑定器与running emulator一起使用,请通过spring.cloud.gcp.pubsub.emulator-host配置其主机和端口。

155.2.1 生产者目标配置

如果打开自动资源创建功能,并且与目标名称对应的主题不存在,则会创建该资源。

例如,对于以下配置,将创建一个名为myEvents的主题。

application.properties.

spring.cloud.stream.bindings.events.destination=myEvents
spring.cloud.stream.gcp.pubsub.bindings.events.producer.auto-create-resources=true

155.2.2Consumer 目标配置

如果打开自动资源创建功能,并且对于 Consumer 而言不存在订阅和/或主题,则将创建订阅和潜在的主题。主题名称将与目标名称相同,订阅名称将是目标名称,后跟使用者组名称。

无论auto-create-resources设置如何,如果未指定使用者组,将创建一个名称为anonymous.<destinationName>.<randomUUID>的匿名用户组。然后,当 Binder 关闭时,将自动清除为匿名使用者组创建的所有发布/订阅订阅。

例如,对于以下配置,将创建名为myEvents的主题和名为myEvents.counsumerGroup1的订阅。如果未指定使用者组,则将创建一个名为anonymous.myEvents.a6d83782-c5a3-4861-ac38-e6e2af15a7be的订阅,并随后对其进行清理。

Tip

如果要手动为使用者创建发布/订阅,请确保它们遵循<destinationName>.<consumerGroup>的命名约定。

application.properties.

spring.cloud.stream.bindings.events.destination=myEvents
spring.cloud.stream.gcp.pubsub.bindings.events.consumer.auto-create-resources=true

# specify consumer group, and avoid anonymous consumer group generation
spring.cloud.stream.bindings.events.group=consumerGroup1

155.3 Sample

sample application可用。