如何使用两个不同的输入主题启用函数路由

时间:2021-03-30 12:21:06

标签: spring-cloud-stream spring-cloud-stream-binder-kafka

我正在将应用程序迁移到基于新函数的 Spring Cloud Stream 编程模型,但在事件路由上受阻。

我必须路由来自两个不同 kafka 主题的事件,我不知道如何将 functionRouter-in-0 绑定到两个不同的目的地。

通过向生产者端的每条消息添加一个 spring.cloud.function.definition 标头来完成路由。

假设我有

  • 我的服务中有 4 个消费者函数:consumerA、consumerB、consumerC、consumerD
  • 2 kafka 主题:
    • topic1 包含带有值为 consumerA 或 consumerB 的 spring.cloud.function.definition 标头的消息
    • topic2 的消息头具有值为 consumerC 或 consumerD

如何在配置中表达 RoutingFunction.FUNCTION_NAME 应该监听 topic1 AND topic2 ?

spring:
  cloud:
    function.definition: consumerA;consumerB;consumerC;consumerD
    stream:
      function.routing.enabled: true
      bindings:
        functionRouter-in-0:
          destination: topic1
          group: myService
          consumer:
            concurrency: 1
            partitioned: false
            maxAttempts: 1
        functionRouter-in-0:   # <== this oviously does not work because it's already defined above
          destination: topic2
          group: myService
          consumer:
            concurrency: 3
            maxAttempts: 1

注意:我使用的是 3.0.11.RELEASE 版本

1 个答案:

答案 0 :(得分:2)

关于绑定,基于注解的模型没有任何变化,正如它所解释的那样 here - "如果绑定代表一个消费者绑定(输入),它可以绑定到多个目的地,并且目标名称可以指定为逗号分隔的字符串值。" 所以。 . .

. . .
functionRouter-in-0:
          destination: topic1, topic2
          group: myService
. . .

我认为其余的内容是 here,它讨论了 ROUTING TO 和 FROM 以及您可以使用的不同机制 - 例如应用程序属性、消息头等。

此外,对于出站,您还有多种选择。 一种是 StreamBridge...sendto... 标头。

随时跟进。