流应用程序 DSL

上一节中描述的流管道 DSL 会自动设置每个 Spring Cloud Stream 应用程序的输入和输出绑定属性。您可以这样做,因为 Spring Cloud Stream 应用程序中只有一个输入或输出目标使用 SourceProcessorSink 提供的绑定接口。但是,Spring Cloud Stream 应用程序可以定义多个输入/输出。例如,考虑一个应用程序,它接收饮料订单的输入,准备饮料,最后将准备好的饮料发送到“热饮”或“冷饮”输出。

在具有多个输入和输出绑定的情况下,Data Flow 无法对从一个应用程序到另一个应用程序的数据流做出任何假设。因此,您需要设置绑定属性来“连接”应用程序。_流应用程序 DSL_ 使用“双竖线”(而不是“管道符号”)来指示 Data Flow 不应配置应用程序的绑定属性。将 || 视为“并行”的意思。

以下示例使用双竖线符号来指示四个流应该并行运行

stream create --definition "orderGeneratorApp || baristaApp || hotDrinkDeliveryApp || coldDrinkDeliveryApp" --name myCafeStream

此流的图形表示类似于下图

Stream Application DSL

此流中有四个应用程序。baristaApp 有两个输出目的地(hotDrinkscoldDrinks),分别供 hotDrinkDeliveryAppcoldDrinkDeliveryApp 使用。部署此流时,您需要设置绑定属性,以便 baristaApp 将热饮消息发送到 hotDrinkDeliveryApp 目的地,并将冷饮消息发送到 coldDrinkDeliveryApp 目的地。

以下示例展示了如何执行此操作

app.baristaApp.spring.cloud.stream.bindings.hotDrinks.destination=hotDrinksDest
app.baristaApp.spring.cloud.stream.bindings.coldDrinks.destination=coldDrinksDest
app.hotDrinkDeliveryApp.spring.cloud.stream.bindings.input.destination=hotDrinksDest
app.coldDrinkDeliveryApp.spring.cloud.stream.bindings.input.destination=coldDrinksDest

下图说明了如何使用绑定属性连接应用程序和通道目的地

Multiple Input/Output channels

与绑定属性一样,您可以为生产者和消费者配置其余的 Spring Cloud Stream 属性。例如,如果要使用消费者组,则需要分别在生产者和消费者应用程序上设置 spring.cloud.stream.bindings.<channelName>.producer.requiredGroupsspring.cloud.stream.bindings.<channelName>.group Spring Cloud Stream 应用程序属性。

Multiple Input/Output channels Bindings

Stream 应用程序 DSL 的另一个常见用例是部署一个 HTTP 网关应用程序,该应用程序向 Kafka 或 RabbitMQ 应用程序发送同步请求或回复消息。在这种情况下,HTTP 网关应用程序和 Kafka 或 RabbitMQ 应用程序都可以是不使用 Spring Cloud Stream 库的 Spring 集成应用程序。

您也可以使用 Stream 应用程序 DSL 只部署一个应用程序。