gpt4 book ai didi

java - RabbitMQ 主题交换消息排序

转载 作者:行者123 更新时间:2023-12-01 14:20:44 25 4
gpt4 key购买 nike

RabbitMQ specification可以找到:

Section 4.7 of the AMQP 0-9-1 core specification explains the conditions under which ordering is guaranteed: messages published in one channel, passing through one exchange and one queue and one outgoing channel will be received in the same order that they were sent. RabbitMQ offers stronger guarantees since release 2.7.0.

但是如果有像 Exchange 1 -> Exchange 2 -> Queue 1 这样的绑定(bind)呢?

仍然保证顺序吗?

我们假设是这样,但我们在我们的应用程序中发现情况可能并非如此。我们使用 spring-rabbit-2.1.6-RELEASE(它使用 amqp-client-5.4.3)。

发布者、绑定(bind)和消费者如下:

Client 1 publishes to Exchange 1 -> Exchange 2 -> Queue 1 - consumed by Client 2
-> Queue 2 - consumed by Client 3

我们可以看到 Client 1 按以下顺序发布了 3 条消息:

  • 消息 1
  • 消息 2
  • 消息 3

但是Client 2和Client 3都按以下顺序接收消息:

  • 消息 3
  • 消息 1
  • 消息 2

编辑 1( Spring 配置)

对于发布者(Client 1),使用了以下 XML 配置(没有在 rabbit 的 ConnectionFactory 上设置额外的属性):

<rabbit:connection-factory channel-cache-size="1" cache-mode="CHANNEL" id="respConnFactory" addresses="..." virtual-host="..." username="..." password="..." executor="connExec"/>
<!-- the executor has no meaning for such usingas mentioned by Gary -->

发布是通过:

AmqpTemplate::send(String exchange, String routingKey, Message message)

专用线程中。

客户端 2 使用带有 SimpleMessageListenerContainer 的默认 spring 配置。

Client 3 实际上不是我们的应用程序,所以我不知道真正的设置。正是他们向我们报告了一个消息未正确排序的错误。

当然,我们记录的消息发布仍有可能存在一些错误。但我对它进行了三次检查 - 它来自单个线程,并且每条消息的自定义 header 中都有序列号,该序列号在 Client 1 上正确递增。

编辑2

我做了进一步分析,以了解错误邮件排序发生的频率。这是结果:

我在事件前后 +-2 小时(总共 4 小时)记录了日志和数据,发送了 42706 条消息,其中只有 3 条排序错误在 客户端 2 上。所有 3 条消息都在 7 毫秒的间隔内发送。

然后我随机取另一个长度为14小时的时间窗发送了 531904 条消息,所有这些消息都客户端 2正确的顺序接收。平均消息速率约为每秒 11 条消息。

消息分布不均,因此 7 毫秒内的 3 条消息没什么特别的 - 恰恰相反。通常在 3-5 毫秒内发送多条消息。

根据这个分析,我假设 rabbit 集群上发生了一些奇怪的事情。不幸的是,我没有它的日志了。

在我看来,某种竞争条件的可能性非常低

谢谢,

弗兰克

最佳答案

Spring AMQP 使用 channel 缓存;在多线程环境中,不能保证同一个线程总是使用同一个 channel ;因此不能保证订购。

对于当前版本,解决方案是使用 scoped operations这将保证一系列出版物将在同一 channel 和保证顺序上发生。

在下一个版本(2.3,今年晚些时候可用)中,我们还添加了 ThreadChannelConnectionFactory它做同样的事情。

关于java - RabbitMQ 主题交换消息排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com