简体   繁体   English

RabbitMQ - 主题交流和竞争消费者

[英]RabbitMQ - Topic Exchanges and Competing Consumers

I successfully setup a Topic Exchange, and I'm able to deliver messages to several consumers at once. 我成功设置了一个主题交换,我能够一次向几个消费者传递消息。

I'd also like to deliver messages to competing consumers and keep using topic exchanges. 我还想向竞争消费者传递信息并继续使用主题交换。 I read that using the same queue name enables consumers to compete for messages. 我读到使用相同的队列名称使消费者能够竞争消息。 I might be mistaken, however, since I cannot make it work. 然而,我可能会弄错,因为我无法使其发挥作用。

Setup for several listeners to the same topic: 针对同一主题的多个侦听器的设置:

  • Declare topic exchange 声明主题交换
  • For each listener, declare a new queue with an autogenerated name 对于每个侦听器,声明具有自动生成名称的新队列
  • Bind this queue to the above exchange with a given topic routing key 使用给定的主题路由键将此队列绑定到上述交换

How to setup competing consumers to the same topic? 如何将竞争消费者设置为同一主题?

Is it even possible with Topic Exchanges? 甚至可以使用Topic Exchanges吗?

Thanks. 谢谢。

Let's review a couple of points first. 我们首先回顾几点。

First, remember that in RabbitMQ you always consume from queues. 首先,请记住,在RabbitMQ中,您总是从队列中消耗。 Exchanges are just your portals and you cannot directly consume from them. 交换只是您的门户,您无法直接使用它们。

Second, Topic exchanges allow for binding the queues with routing key "patterns". 其次, 主题交换允许使用路由键“模式”绑定队列。 Therefore, the term topic is valid in the context of "Topic Exchanges". 因此,术语主题在“主题交换”的上下文中有效。

Now this is what I understand from your question: 现在这是我从你的问题中理解的:

Multiple consumers/same routing key : This is where you want multiple consumers to all consume the messages with the same routing key (or same routing key patterns in the case of Topic Exchanges). 多个使用者/相同的路由密钥 :这是您希望多个使用者都使用相同路由密钥(或主题交换中的相同路由密钥模式)消费的地方。 This is in fact doable. 这实际上是可行的。 Just do this: 这样做:

  1. Declare your Topic Exchange 声明您的主题交换
  2. Declare a queue with some name 声明具有某个名称的队列
  3. Bind that queue to your topic with your desired routing key pattern 使用所需的路由密钥模式将该队列绑定到您的主题
  4. Create multiple consumers and have them listen to that same queue. 创建多个使用者并让他们监听同一个队列。

What will happen is that RabbitMQ is going to load balance to your consumers in a round robin matter. 会发生什么是RabbitMQ将在循环问题中为您的消费者带来负载平衡。 This means all consumers will consume from the same queue. 这意味着所有消费者将使用相同的队列。 But remember that in this scenario it is possible that a single message is delivered more than once in theory. 但请记住,在这种情况下,理论上可能会多次传递单个消息。

What you were doing was to create multiple queues and have one consumer per queue. 你正在做的是创建多个队列,每个队列有一个消费者。 This means that every message coming to the exchange would be duplicated across all queues. 这意味着进入交换的每条消息都将在所有队列中重复。 The end result would be that a message gets processed multiple time. 最终结果是消息被多次处理。

I solved this by using exchange-to-exchange bindings. 我通过使用交换到交换绑定解决了这个问题。

  1. The outer exchange is a topic exchange. 外部交换是一个主题交换。
  2. The inner exchange is a fanout exchange bound to a client-named queue. 内部交换是绑定到客户端命名队列的扇出交换。
  3. The outer exchange is bound to the inner exchange with a routing key that includes a wildcard. 外部交换机使用包含通配符的路由密钥绑定到内部交换机。

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

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