RabbitMQ 向每个消费者发送相同的消息 [英] RabbitMQ same message to each consumer

查看:68
本文介绍了RabbitMQ 向每个消费者发送相同的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 RabbitMQ 网站上的示例:RabbitMQ 示例

I have implemented the example from the RabbitMQ website: RabbitMQ Example

我已将其扩展为具有用于发送消息的按钮的应用程序.现在我在两台不同的计算机上启动了两个消费者.当我发送消息时,第一条消息发送到计算机 1,然后第二条消息发送到计算机 2,第三条消息发送到计算机 1,依此类推.

I have expanded it to have an application with a button to send a message. Now I started two consumer on two different computers. When I send the message the first message is sent to computer1, then the second message is sent to computer2, the thrid to computer1 and so on.

这是为什么,我该如何更改将每条消息发送给每个消费者的行为?

Why is this, and how can I change the behavior to send each message to each consumer?

推荐答案

这是为什么

正如 Yazan 所指出的,消息以循环方式从单个队列中使用.您看到的行为是设计使然,可以轻松扩展给定队列的消费者数量.

As noted by Yazan, messages are consumed from a single queue in a round-robin manner. The behavior your are seeing is by design, making it easy to scale up the number of consumers for a given queue.

如何更改将每条消息发送给每个消费者的行为?

how can I change the behavior to send each message to each consumer?

要让每个消费者收到相同的消息,您需要为每个消费者创建一个队列,并将相同的消息传递到每个队列.

To have each consumer receive the same message, you need to create a queue for each consumer and deliver the same message to each queue.

最简单的方法是使用 fanout 交换.这会将每条消息发送到绑定到交换的每个队列,完全忽略路由键.

The easiest way to do this is to use a fanout exchange. This will send every message to every queue that is bound to the exchange, completely ignoring the routing key.

如果您需要对路由进行更多控制,可以使用 topicdirect 交换和管理路由密钥.

If you need more control over the routing, you can use a topic or direct exchange and manage the routing keys.

无论您选择哪种类型的交换,您都需要为每个消费者创建一个队列,并将每条消息路由到每个队列.

Whatever type of exchange you choose, though, you will need to have a queue per consumer and have each message routed to each queue.

这篇关于RabbitMQ 向每个消费者发送相同的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆