且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 RabbitMQ 中设置重试次数?

更新时间:2022-05-10 22:07:49

在 RabbitMQ(以及 AMQP 协议)中没有像重试尝试这样的功能.

There are no such feature like retry attempts in RabbitMQ (as well as in AMQP protocol).

实现重试尝试限制行为的可能解决方案:

Possible solution to implement retry attempts limit behavior:

  1. 如果之前没有重新传递消息,则重新传递消息(检查 basic.deliver 方法上的 redelivered 参数 - 你的库应该有一些接口)并删除它然后在死信交换中捕获,然后以某种方式处理.

  1. Redeliver message if it was not previously redelivered (check redelivered parameter on basic.deliver method - your library should have some interface for this) and drop it and then catch in dead letter exchange, then process somehow.

每次无法处理消息时再次发布它,但设置或增加/减少标头字段,例如 x-redelivered-count(不过,您可以选择任何您喜欢的名称).要在这种情况下控制重新交付,您必须检查您设置的字段是否达到某个限制(顶部或底部 - 0 是我的选择,a-la ttl in ip header from tcp/ip).

Each time message cannot be processed publish it again but set or increment/decrement header field, say x-redelivered-count (you can chose any name you like, though). To get control over redeliveries in this case you have to check the field you set whether it reaches some limit (top or bottom - 0 is my choise, a-la ttl in ip header from tcp/ip).

将消息唯一键(比如 uuid,但您必须在发布消息时手动设置)存储在 Redis、memcache 或其他存储中,甚至在 mysql 中与重新传递计数一起存储,然后在每次重新传递时递增/递减这个值直到达到极限.

Store message unique key (say uuid, but you have to set it manually when you publish message) in Redis, memcache or other storage, even in mysql alongside with redeliveries count and then on each redelivery increment/decrement this value until it reach the limit.

(对于真正的极客)编写将实现您想要的此类行为的插件.

(for real geeks) write plugin that will implement such behavior like you want.

#3 的优点是重新传递的消息保留在队列头中.如果您排长队或消息顺序对您很重要,这一点很重要(请注意,重新发送会破坏严格的消息顺序,有关详细信息,请参阅官方文档或 this question on SO).

The pro of #3 is that redelivered message stay in queue head. This is important if you have long queue or if message order is important for you (note, that redeliveries will break strict messages order, see official docs for details or this question on SO).

附注:

此主题中有类似答案,但在php中.看一遍,也许对你有一点帮助(从有多种技术可以处理循环重新传递问题"开始阅读..

There is similar answer in this topic, but in php. Look through it, maybe it helps you a bit (start reading it from words "There are multiple techniques to deal with cycle redeliver problem".