首页 > 解决方案 > Azure 服务总线、AWS SNS、RabbitMQ -> 所有订阅者都收到消息?

问题描述

在查看 Pub/Sub 模式时,我遇到了这样的场景:

假设您有一个水平缩放的应用程序,它有 X 个实例。他们都订阅了一个主题,其中包含“将 10 美元从账户 A 转移到账户 B”之类的消息。当有人向该主题发布消息时,所有订阅者都会收到该消息吗?

在上述情况下,很明显,消息应该只被 1 个订阅者接收并且只处理一次。

如何处理这种情况?您是否放弃发布/订阅并开始合并?

标签: rabbitmqazureservicebusamazon-sns

解决方案


In RabbitMQ Normally, active consumers connected to the same queue receive messages from it in a round-robin fashion. So this insures that a message is processed exactly once.

So in your case you should design a queue where all the messages for

"Transfer $10 from account A to account B"

Are routed to and all the consumers register themselves on this queue itself , this insures that one message will go to only one subscriber.

Another point not related to your question but is important to know is that there is another concept called "Consumer Priorities" which allows you to ensure that high priority consumers receive messages while they are active, with messages only going to lower priority consumers when the high priority consumers block.

More info can be found here


推荐阅读