在 RabbitMQ 中混合发布/订阅与工作队列

Mixing of Pub/Sub with workqueues in RabbitMQ(在 RabbitMQ 中混合发布/订阅与工作队列)

本文介绍了在 RabbitMQ 中混合发布/订阅与工作队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估使用 RabbitMQ 作为消息队列/消息总线,并一直在查看示例 教程 在 RabbitMQ 页面上.

I am evaluating using RabbitMQ as message queue/message bus and have been looking at the example tutorials on the RabbitMQ page.

我正在寻找教程未涵盖的特定场景,我不确定是否以及如何通过 RabbitMQ 进行操作.

I am looking for a specific scenario not covered by the tutorials and I am not sure if and how it would be possible to do via RabbitMQ.

设置:

  • 假设我有一项服务,我们称其为采购订单",而我必须提供其他服务,称为物流"和会计".

  • Let's assume I got a service, let's call it "purchase orders" and I have to other services called "logistics" and "accounting".

发送订单时,我想通过 RabbitMQ 将其​​作为消息发送.

When an order is sent, I want to send it as a message via RabbitMQ.

有 2 个账户"和 3 个物流"服务

There 2 "account" and 3 "logistic" services

确保帐户"和物流"只处理一次消息的正确方法是什么?如果我理解正确,使用 pub/sub 将导致消息被处理两次(帐户)或三次(物流).

What would be the correct way to ensure that "account" and "logistic" will process the message only once? Using pub/sub will cause the messages to be processed twice (account) or trice (logistics) if i understand it correctly.

使用工作队列和 prefetch=1 可以确保只有一个可以得到它,但是我有 2 项服务,并且希望每种类型的服务都能得到一个.

With work queues and prefetch=1 it would assure that only one gets it, but I have 2 services and want each type of service to get one.

有没有一种方法可以将两者结合起来并为每个服务设置一个工作队列,而无需将 2 个单独的事件/消息发送到两个不同的交换器?

Is there a way to combine both and have a work queues for each of the service, without sending 2 separate events/messages to two different exchanges?

推荐答案

如果我理解正确,使用 pub/sub 将导致消息被处理两次(帐户)或三次(物流).

Using pub/sub will cause the messages to be processed twice (account) or trice (logistics) if i understand it correctly.

根据您的描述,每个工作人员可能有 1 个队列,并且您正在将消息路由到所有工作人员队列.因此,每个工作人员都会获得消息的副本,因为您将消息路由到所有队列.

you probably have 1 queue per worker, based on your description, and you are routing the message to all worker queues. therefore, each worker gets a copy of the message, because you routed the message to all of the queues.

您想要的是一个帐户"队列和一个后勤"队列.您将从单个帐户队列中读取多个帐户服务;物流服务/队列也一样.

what you want is a single "account" queue and a single "logistic" queue. you will have multiple account services reading from the single account queue; same for the logistic service / queue.

设置 prefetch=1 也很重要.这可以防止您一次向单个工作人员读取太多消息.

setting prefetch=1 is important as well. this prevents you from reading too many messages in to a single worker, at once.

有没有一种方法可以将两者结合起来并为每个服务设置一个工作队列,而无需将 2 个单独的事件/消息发送到两个不同的交换器?

Is there a way to combine both and have a work queues for each of the service, without sending 2 separate events/messages to two different exchanges?

是的 - 不要使用扇出交换.使用主题或直接交换,并使用多个路由键将单个消息路由到帐户和物流队列.

yes - don't use a fanout exchange. use a topic or direct exchange, and use multiple routing keys to route a single message to both the account and logistics queues.

确保帐户"和物流"只处理一次消息的正确方法是什么?

What would be the correct way to ensure that "account" and "logistic" will process the message only once?

没有办法保证这一点,100%.在某些时候,即使像我描述的那样进行了正确的设置,您也会遇到网络故障或工作人员崩溃或其他一些问题,并且一条消息将被处理两次.你必须在你的设计中考虑到这一点,在你的消息处理中使用某种形式的 idempotence.

there is no way to guarantee this, 100%. at some point, even with a proper setup like I've described, you will have a network failure or a worker crash or some other problem and a message will get processed twice. you have to account for this in you design, using some form of idempotence in your message processing.

希望有帮助!

这篇关于在 RabbitMQ 中混合发布/订阅与工作队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 RabbitMQ 中混合发布/订阅与工作队列

上一篇: rabbitmq 的 REST API
下一篇: RabbitMQ 和 C#

基础教程推荐