简体   繁体   English

在Spring Cloud Stream(RabbitMQ)中以编程方式声明绑定?

[英]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). 我有一个Spring Cloud Stream应用程序,它使用RabbitMQ活页夹来消耗消息(它不产生任何消息)。 The application.yaml file looks like this: application.yaml文件如下所示:

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: 这将在RabbitMQ中创建两个队列:

  1. q1 that is bound to exchange x with routing key xz# 必定要与路由密钥xz#交换x q1
  2. q2 that is bound to exchange y with routing key yz# 必定要与路由密钥yz#交换y q2 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. 我知道我无法将交换绑定到来自application.yaml多个路由键(请参阅 SO问题),因此我怀疑无法将Spring Cloud Stream配置为对单个绑定使用多个目的地(交换)。

So my question is, can I programmatically declare so that one binding consumes from multiple exchanges? 所以我的问题是,我可以以编程方式声明一个binding消耗多个交换吗? Is there anything that is required to be retained in the application.yaml file if doing this? 如果这样做,有什么需要保留在application.yaml文件中的吗?

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 # . 然后从与#绑定的z上的单个队列中消费。

You can define the exchange to exchange bindings in your boot application as @Bean s. 您可以定义交换,以在启动应用程序@Bean绑定交换为@Bean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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