问题描述
我正在阅读 RabbitMQ in Action 一书,仍在第 2 章中,但作者说的一件事让我感到困惑.您设置了一个交换并发送了一条消息,两个订阅者正在侦听队列.当第一条消息进来时,第一个订阅者得到它,一旦确认消息就会被删除.当下一条消息到达时,它以循环方式进入下一个侦听器.我想,如果我要发送消息,我希望所有订阅者都能收到.我的理解有误吗?
I am reading RabbitMQ in Action book, stil in the chapter 2, but one thing authors says puzzling me. You setup a exchange and send a message, two subscribers are listening to the queue. When the first message comes in, the first subscriber gets it and the message is removed once it is acknowledged. When the next messages arrives it goes to the next listener in round robin way. I thought, if I am sending a message, I want all the subscribers to get it. Is my understanding wrong?
推荐答案
这很简单.如果您希望所有订阅者都获得消息的副本,请使用通配符绑定创建多个队列.
This is simple. If you want all subscribers to get a copy of the message, then create multiple queues with a wildcard binding.
假设您有一个主题交换,并且您发布所有消息都带有 fred.interesting 或 fred.boring 这样的路由键,那么如果每个订阅者都使用 fred.* 的绑定键声明了一个队列,那么每个队列都会得到每条消息的副本.唯一的问题是如何命名队列,尽管 RabbitMQ 可以根据需要为您生成唯一的名称.
Assuming that you have a topic exchange, and you publish all messages with a routing key like fred.interesting or fred.boring, then if each subscriber declares a queue with the binding key of fred.*, then each queue will get a copy of every message. The only issue is how to name the queues, although RabbitMQ can generate unique names for you if you wish.
如果我这样做,我将有一个主管进程来启动和监视消息消费者进程.主管会为每个消费者进程分配一个队列名称,如 fred0001、fred0002,并跟踪哪些名称在起作用.像这样使用指定的名称可以更轻松地使用管理工具或编写管理和监控脚本.
If I were doing this I would have a supervisor process that starts and monitors the message consumer processes. The supervisor would assign each consumer process a queue name like fred0001, fred0002 and keep track of which names are in play. Using specified names like this makes it easier to use management tools or write management and monitoring scripts.
这篇关于如何以所有订阅者获取消息的方式设置队列 - Rabbit MQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!