RabbitMQ:带有主题交换的持久消息
我对 RabbitMQ 很陌生。
我已经建立了一个“主题”交换。消费者可以在发布者之后启动。我希望消费者能够接收在启动之前发送且尚未消耗的消息。
交换器使用以下参数进行设置:
exchange_type => 'topic'
durable => 1
auto_delete => 0
passive => 0
使用此参数发布消息:
delivery_mode => 2
消费者使用 get() 从交换器检索消息。
不幸的是,在任何客户端启动之前发布的任何消息都会丢失。我使用了不同的组合。
我想我的问题是交换不保存消息。也许我需要在发布者和消费者之间有一个队列。但这似乎不适用于通过密钥路由消息的“主题”交换。
我应该如何进行?我使用 Perl
绑定 Net::RabbitMQ
(应该没关系)和 RabbitMQ 2.2.0
。
I am very new to RabbitMQ.
I have set up a 'topic' exchange. The consumers may be started after the publisher. I'd like the consumers to be able to receive messages that have been sent before they were up, and that was not consumed yet.
The exchange is set up with the following parameters:
exchange_type => 'topic'
durable => 1
auto_delete => 0
passive => 0
The messages are published with this parameter:
delivery_mode => 2
Consumers use get() to retrieve the messages from the exchange.
Unfortunately, any message published before any client was up is lost. I have used different combinations.
I guess my problem is that the exchange does not hold messages. Maybe I need to have a queue between the publisher and the consumer. But this does not seem to work with a 'topic' exchange where messages are routed by a key.
How should I proceed? I use the Perl
binding Net::RabbitMQ
(shouldn't matter) and RabbitMQ 2.2.0
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果在发布消息时没有连接的使用者可用于处理消息,则需要一个持久队列来存储消息。
交换器不存储消息,但队列可以。令人困惑的部分是,交换可以被标记为“持久”,但这真正意味着,如果您重新启动代理,交换本身仍将存在,但它确实不存在。 > 意味着发送到该交换器的任何消息都会自动保留。
鉴于此,这里有两个选项:
我会选择#1。需要执行的步骤可能并不多,您始终可以编写所需步骤的脚本,以便可以重复这些步骤。另外,如果所有消费者都将从同一个队列中提取数据(而不是每个都有一个专用队列),那么这实际上是最小的管理开销。
队列是需要适当管理和控制的东西。否则,您最终可能会遇到流氓消费者声明持久队列,使用它们几分钟,但再也不会。不久之后,您将拥有一个永久增长的队列,并且没有任何东西可以减少其大小,以及即将到来的经纪人灾难。
You need a durable queue to store messages if there are no connected consumers available to process the messages at the time they are published.
An exchange doesn't store messages, but a queue can. The confusing part is that exchanges can be marked as "durable" but all that really means is that the exchange itself will still be there if you restart your broker, but it does not mean that any messages sent to that exchange are automatically persisted.
Given that, here are two options:
I would go for #1. There may not be many steps to perform and you could always script the steps required so that they could be repeated. Plus if all your consumers are going to pull from the same single queue (rather than have a dedicated queue each) it's really a minimal piece of administrative overhead.
Queues are something to be managed and controlled properly. Otherwise you could end up with rogue consumers declaring durable queues, using them for a few minutes but never again. Soon after you'll have a permanently-growing queue with nothing reducing its size, and an impending broker apocalypse.
正如 Brian 所提到的,交换器不存储消息,主要负责将消息路由到另一个交换器或队列。如果交换器未绑定到队列,则发送到该交换器的所有消息都将“丢失”。
您不需要在发布者脚本中声明固定的客户端队列,因为这可能不可扩展。队列可以由发布者动态创建,并使用交换器到交换器绑定在内部路由。
RabbitMQ 支持交换到交换的绑定,这将允许拓扑灵活性、解耦和其他好处。您可以在 RabbitMQ Exchange 到 Exchange 绑定处阅读更多信息[AMPQ]
RabbitMQ Exchange 到 Exchange 绑定
如果不存在使用队列的消费者,则用于创建具有持久性的交换到交换绑定的示例 Python 代码。
As mentioned by Brian an exchange does not store messages and is mainly responsible for routing messages to either another exchange/s or queue/s. If the exchange is not bound to a queue, then all messages sent to that exchange will be 'lost'.
You should not need to declare fixed client queues in the publisher script since this might not be scalable. Queues can be created dynamically by your publishers and routed internally using exchange-to-exchange binding.
RabbitMQ supports exchange-to-exchange bindings that will allow for topology flexibility, decoupling and other benefits. You can read more here at RabbitMQ Exchange to Exchange Bindings [AMPQ]
RabbitMQ Exchange To Exchange Binding
Example Python code to create exchange-to-exchange binding with persistence if no consumer is present using queue.
您的情况似乎是“消息持久性”。
从 RabbitMQ 教程文档 中,您需要标记两个
队列
和消息
作为持久的(下面的代码为C#版本。对于其他语言,您可以更喜欢此处)。队列
能够在RabbitMQ
节点重新启动后继续存在。为此,我们需要将其声明为持久的:IBasicProperties.SetPercient为真。
Your case seems to be "Message durability".
From RabbitMQ Tutorials docs, You need to mark both the
queue
andmessages
as durable (The code below as C# version. With other languages, you can prefer here).queue
will survive aRabbitMQ
node restart. In order to do so, we need to declare it as durable:IBasicProperties.SetPersistent
to true.