AMQP 中的消息路由
我想用 AMQP 做一些路由魔法。我的设置是使用 Python,在消费者/生产者端使用 Pika,在 AMQP 服务器上使用 RabbitMQ。
我想要实现的目标:
- 向单个交换发送消息
- (此处插入魔法)
像这样消费消息:
- 一组订阅者应该能够根据路由键进行检索
一组订阅者应该只获取所有消息。
棘手的部分是,如果第二组中的任何服务器收到消息,第二组中的其他服务器将不会收到该消息。第一组中的所有服务器应该仍然能够使用此消息。
这是否可以通过单个 basic_publish
调用实现,或者我是否需要将消息发送到路由交换(对于第一组消费者)和到“全局”交换第二组消费者?
澄清:
我想要实现的是一个 致电发布消息并获取 由 2 组不同的组接收 消费者。
情况 1:仅根据路由密钥(即 带有路由键
foo
的消息将是 受到所有消费者的好评 目前对该主题感兴趣)案例 2:这基本上类似于 RabbitMQ 教程对于工人 队列。 有不少工人表示 将收到在 a 中发送的消息 循环方式。只有一名工作人员会收到消息
仍然是对某个特定内容感兴趣的消费者收到的消息 路由密钥应该与工作人员收到的消息完全相同,产生 通过单个 API 调用。
(希望我的问题有意义,我对 AMQP 术语不太熟悉)
I'd like to do some routing magic with AMQP. My setup is Python with Pika on the consumer/producer side and RabbitMQ for the AMQP server.
What I'd like to achieve:
- send a message to a single exchange
- (insert magic here)
consume messages like so:
- one set of subscribers should be able to retrieve based on a routing key
one set of subscribers should just get all messages.
The tricky part is that if the any server in the second set has received a message no other server from the second set will receive it. All the servers from the first set should still be able to consume this message.
Is this possible with a single basic_publish
call or do I need to send the message to a routing exchange (for the first set of consumers) and to a "global" exchange for the second set of consumers?
CLARIFICATION:
What I'd like to achieve is a single
call to publish a message and have it
received by 2 distinct sets of
consumers.Case 1: Just receive messages based on routing key (that is a
message with routing keyfoo
will be
received by all the consumers
currently interested in that topic)Case 2: This basically resembles the RabbitMQ Tutorial for Worker
Queues.
There are a number of workers that
will receive messages dispatched in a
round robin way. Only one worker will receive a messageStill the message that is received by the consumers that are interested in a certain
routing key should be exactly the same as the messages received by the workers, produced
by a single API call.
(Hope my question makes sense I'm not too familiar with AMQP terms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要使用
topic
交换并为每个队列使用不同的路由键发布消息。当消费者使用绑定键(或要匹配的模式)绑定队列时,奇迹就会发生。一些消费者只是使用路由密钥作为他们的绑定密钥。但第二组将使用通配符模式作为其绑定密钥。对于情况 1,您需要为每个消费者创建一个队列,并将每个队列与适当的路由键绑定。
对于情况 2,只需创建一个路由键为
#
的单个队列,然后让每个工作线程消费者从中进行消费。经纪人会以循环方式向工人派遣。这是 RabbitMQ 中的屏幕截图。在此示例中,“案例 1”中有两个消费者(Foo 和 Bar),并且有一个队列供所有工作人员满足“案例 2”的要求。
所有符合 AMQP 的代理都应支持此模型,并且不需要任何特定于供应商的增强功能。
To start with, you need to use a
topic
exchange and publish your messages with a different routing key for each queue. The magic happens when the consumer binds the queue with a binding key (or pattern to be matched). Some consumers just use the routing keys as their binding key. But the second set will use a wildcard pattern for their binding key.For Case 1, you need to create a queue per consumer, and bind each queue with an appropriate routing key.
For Case 2, just create a single queue with a routing key of
#
and have each of your worker consumers consume from that. The broker will dispatch in a round-robin manner to the workers.Here's a screenshot of what it would look like in RabbitMQ. In this example there are two consumers from your "case 1" (Foo and Bar) and one queue for all the workers to satisfy "case 2".
This model should be supported by all AMQP-compliant brokers and wouldn't require any vendor-specific enhancements.