如何在@StreamListener

时间:2019-06-27 08:57:54

标签: spring-cloud-stream

我们正在使用具有Kakfa绑定的Spring Cloud Stream,并且在我们的项目中,我们设想了以下两个类来实现基本使用者:

MyConsumer ==> StreamConsumer

StreamConsumer:

    public class StreamConsumer {

        @StreamListener(EventSink.INPUT0)
        public void handleMessages(Message<Notification> message) {
            handle(message); // here we call to consumer registered functions
        }
    }
As of doc @StreamListener:

Annotation that marks a method to be a listener to inputs declared via 
EnableBinding (e.g. channels).

还有MyConsumer:

    @EnableBinding(EventSink.class)
    public class MyConsumer extends StreamConsumer {

        --we declare functional interfaces (to consume messages) to be register at StreamConsumer
          and managed by the handle function in order to be call back--
    }
As of doc, @EnableBinding:

Enables the binding of targets annotated with Input and Output to a broker,
according to the list of interfaces passed as value to the annotation.

EventSink界面:

    public interface EventSink {

        String INPUT0 = "input0";
        String INPUT1 = "input1";
        String INPUT2 = "input2";

        @Input(INPUT0)
        SubscribableChannel input0();

        @Input(INPUT1)
        SubscribableChannel input1();

        @Input(INPUT2)
        SubscribableChannel input2();   
    }

Application.yml:

      cloud:
        stream:
          bindings:
            input0:
              group: CommonConsumer
              destination: ${EXTERNALSERVICES_QUEUE:externalservices}
              binder: kafka
            input1:
              group: UpdaterConsumer
              destination: ${EXTERNALSERVICES_QUEUE:externalservices}
              binder: kafka
            input2:
              group: DeleterConsumer
              destination: ${EXTERNALSERVICES_QUEUE:externalservices}
              binder: kafka
          binders:
              kafka:
                type: kafka
                environment:
                  spring:
                    kafka:
                      brokers: ${KAFKA_HOST:127.0.0.1}

如您所见,这3个输入具有相同的目的地,因为我们希望有3个(动态数量)消费者 为了共享同一频道,但我们不希望他们竞争消息,因此我们希望拥有 不同的组(所有消息将分发给所有消费者)

是否可以在StreamConsumer基类中声明使用@StreamListener侦听消息的方法,但是 动态选择输入?据我所知,@ StreamListener或@Input始终需要使用文字作为参数 (它可能来自EvenSink界面,直接文字,应用程序属性,用于管理分区的SPEL)

或者是另一种可能性,我们是否能够在消费者中动态地改变群体?

我还检查了获取使用者的选项,如doc中所述:

https://cloud.spring.io/spring-cloud-static/spring-cloud-stream-binder-kafka/2.1.0.RC4/single/spring-cloud-stream-binder-kafka.html#pause-resume

但是Consumer无权访问组属性。我猜想@StreamListener中的绑定已经建立 然后经纪人进入一个已定义的(可能是不变的)组。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的是基于内容的路由,可以将消息委派给特定的侦听器。如果是,则可以在<style> body{ width:100%; overflow-x:hidden; overflow-y:hidden; } </style> 上使用condition属性。 Here is more details