Spring boot stream bind queue with multiple routin

2019-06-01 02:06发布

问题:

I need to bind single queue with multiple routing keys.

I have configuration in application.properties:

spring.cloud.stream.bindings.some-channel1.destination=exch
spring.cloud.stream.bindings.some-channel1.group=a-queue
spring.cloud.stream.rabbit.bindings.some-channel1.consumer.binding-routing-key=event.domain1

spring.cloud.stream.bindings.some-channel2.destination=exch
spring.cloud.stream.bindings.some-channel2.group=a-queue
spring.cloud.stream.rabbit.bindings.some-channel2.consumer.binding-routing-key=event.domain2

This creates queue and bindings properly in rabbit, but finally after running application I got:

org.springframework.cloud.stream.binder.BinderException: Exception thrown while starting consumer: 

After all above configuration i still bad because I need single channel. But queue binded with list of routing keys.

Any Ideas how to configure it?

回答1:

You can't do it with stream properties, but you can always add extra bindings with normal Spring AMQP declarations...

@SpringBootApplication
@EnableBinding(Sink.class)
public class So50526298Application {

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

    @StreamListener(Sink.INPUT)
    public void listen(String in) {
        System.out.println(in);
    }

    // extra bindings...

    @Bean
    public TopicExchange exch() {
        return new TopicExchange("exch");
    }

    @Bean
    public Queue queue() {
        return new Queue("exch.a-queue");
    }

    @Bean
    public Binding extraBinding1() {
        return BindingBuilder.bind(queue()).to(exch()).with("event-domain2");
    }

}

There is also a third party "advanced" boot starter that allows you to add declarations in a yaml file. I haven't tried it, but it looks interesting.