简体   繁体   中英

Programmatically declare bindings in Spring Cloud Stream (RabbitMQ)?

I have a Spring Cloud Stream application what using the RabbitMQ binder to consume messages (it doesn't produce any). The application.yaml file looks like this:

spring:
  cloud:
    stream:
      rabbit:
        bindings:
          x:
            consumer:
              bindingRoutingKey: x.z.#
              queueNameGroupOnly: true
          y:
            consumer:
              bindingRoutingKey: y.z.#
              queueNameGroupOnly: true
      bindings:
        x:
          binder: rabbit
          group: q1
          destination: x
        y:
          binder: rabbit
          group: q2
          destination: y

This will create two queues in RabbitMQ:

  1. q1 that is bound to exchange x with routing key xz#
  2. q2 that is bound to exchange y with routing key yz#

I'd like to create a single queue that consumes from multiple exchanges and routing keys . I know that I can't bind an exchange to multiple routing keys from application.yaml (see this SO question) and thus I suspect that I can't configure Spring Cloud Stream to use multiple destinations (exchanges) for a single binding.

So my question is, can I programmatically declare so that one binding consumes from multiple exchanges? Is there anything that is required to be retained in the application.yaml file if doing this?

How should I go about?

You can use exchange-to-exchange binding to satisfy this requirement.

x -> z
y -> z

Then consume from a single queue on z that is bound with # .

You can define the exchange to exchange bindings in your boot application as @Bean s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM