rabbitmq 队列上有多个消费者-只有一个消费者收到消息 [英] rabbitmq multiple consumers on a queue- only one get the message

查看:126
本文介绍了rabbitmq 队列上有多个消费者-只有一个消费者收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了多个消费者,他们从单个队列中获取消息,我使用类似于 这个例子,除了我在一个无限循环中执行 basic.get 以进行轮询.

I implemented multiple consumers, who are fetching messages from a single queue, I am doing this using something similar to this example, except that I'm doing basic.get on an infinite loop for polling.

知道如何防止所有消费者之间发生竞争,因为只有一个消费者会收到消息,而另一个消费者将继续进行轮询,直到另一条消息到来吗?
我尝试实现一个逻辑,在该逻辑中,一旦我收到消息,我就确认它以删除消息,但似乎其他一些队列在第一个队列确认并删除它之前设法获取了该消息.所以每个消费者都收到了消息.

Any idea how do I prevent racing between all consumers, in that only one consumer will get the message and the other will continue to do polling until another message comes?
I tried implementing a logic in which as soon as I get the message I ack it for the message to be removed, but it seems that some other queue managed to get the message before the first queue has ack and removed it. So each consumer got the message.

提前致谢

推荐答案

知道如何防止所有消费者之间发生竞争,因为只有一个消费者会收到消息,而另一个消费者将继续进行轮询,直到另一条消息到来吗?

Any idea how do I prevent racing between all consumers, in that only one consumer will get the message and the other will continue to do polling until another message comes?

你不能,你设置的方式.RabbitMQ 会将消息轮询给消费者,但只有一个消费者会从队列中接收消息.这是在 RabbitMQ 中设计的,当您在单个队列上有多个使用者时.

you can't, the way you have things set up. RabbitMQ will round-robin the messages to the consumers, but only one consumer will receive the message from the queue. This is by design in RabbitMQ, when you have multiple consumers on a single queue.

如果您需要所有消费者接收所有消息,那么您需要更改您的配置,以便每个消费者都有自己的队列.然后,您需要通过一个交换器发布您的消息,该交换器会将消息传送到所有消费者的所有队列.

If you need all consumers to receive all messages, then you need to change your configuration so that each consumer has it's own queue. Then you need to publish your message through an exchange that will deliver the message to all of the queues for all of the consumers.

最简单的方法是使用扇出交换类型.

The easiest way to do this is with a Fanout exchange type.

这篇关于rabbitmq 队列上有多个消费者-只有一个消费者收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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