[英]Can I configure max tries for Spring Cloud Stream with RabbitMQ using DLQ
I'm working with Spring Cloud Stream and Rabbit, and I used the config defined here to set up a dead-letter queue (DLQ) and it works very nicely.我正在使用 Spring Cloud Stream 和 Rabbit,我使用此处定义的配置来设置死信队列 (DLQ),它工作得非常好。
What I'd like to do is set a maximum amount of times a message goes to the DLQ before being discarded - is is possible to set this via config?我想做的是设置消息在被丢弃之前进入 DLQ 的最大次数 - 可以通过配置设置吗? If so, how?
如果是这样,怎么做? If not, what should I do to achieve this behaviour?
如果没有,我应该怎么做才能实现这种行为?
I'm looking for a code sample for the best answer, preferably in Kotlin (if relevant)我正在寻找最佳答案的代码示例,最好是在 Kotlin 中(如果相关)
That depends whether you're using qurorum queues or not.这取决于您是否使用仲裁队列。 I don't believe there's a default config for that without quorum queues.
我不相信没有仲裁队列的默认配置。 However, you should be able to store the redelivery count within a custom header or in the message itself.
但是,您应该能够将重新投递计数存储在自定义标头或消息本身中。
Then if you set a MAX_REDELIVERY_COUNT
constant in your application, you can check if the message exceeds the maximum number of redeliveries.然后如果你在你的应用程序中设置了一个
MAX_REDELIVERY_COUNT
常量,你可以检查消息是否超过了最大重新传递次数。
If you're not using quorum queues, I'd take a look at this answer: How do I set a number of retry attempts in RabbitMQ?如果您不使用仲裁队列,我会看看这个答案: 如何在 RabbitMQ 中设置重试次数? .
. This answer has quite some good options.
这个答案有一些不错的选择。
However, when using quorum queues, you can set the delivery-limit
option.但是,在使用仲裁队列时,您可以设置
delivery-limit
选项。 More info on that can be found here: https://www.rabbitmq.com/quorum-queues.html#feature-matrix .更多信息可以在这里找到: https ://www.rabbitmq.com/quorum-queues.html#feature-matrix。
In order to publish a message with custom headers:为了发布带有自定义标头的消息:
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("latitude", 51.5252949);
headers.put("longitude", -0.0905493);
channel.basicPublish(exchangeName, routingKey,
new AMQP.BasicProperties.Builder()
.headers(headers)
.build(),
messageBodyBytes);
As found on https://www.rabbitmq.com/api-guide.html#publishing .如在https://www.rabbitmq.com/api-guide.html#publishing上找到的。
The problem is that the headers can't be simply updated.问题是标题不能简单地更新。 However, you could do this with a workaround.
但是,您可以使用解决方法来做到这一点。 Let's say you want a maximum of 5 retries per message.
假设您希望每条消息最多重试 5 次。 If the message can't be processed, send it to a DLX.
如果无法处理消息,请将其发送到 DLX。 If the message doesn't exceed the maximum retries, read the original headers of the message, update the custom retry count header and resend it to the original queue.
如果消息没有超过最大重试次数,则读取消息的原始标头,更新自定义重试计数标头并将其重新发送到原始队列。
If the message gets in de DLX and does exceed the maximum retry count, send the message as is to the DLX with a different routing key, which is bound to a queue for the "definitive" dead messages.如果消息进入 de DLX 并且确实超过了最大重试次数,则使用不同的路由键将消息按原样发送到 DLX,该路由键绑定到“最终”死消息的队列。
That'd mean that you would get something like this in a simplified diagram:这意味着你会在一个简化的图表中得到类似的东西:
This is just an idea, I don't know if it'll work for sure, but it's the best that I can think of in your situation.这只是一个想法,我不知道它是否肯定会起作用,但这是我能想到的在你的情况下最好的。
It seems like the Spring Cloud Stream Binder for RabbitMQ has this option.似乎 RabbitMQ 的 Spring Cloud Stream Binder 有这个选项。 In the docs as found on https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit , it says the following:
在https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit上的文档中,它说明了以下内容:
By using the optional
autoBindDlq
option, you can configure the binder to create and configure dead-letter queues (DLQs) (and a dead-letter exchangeDLX
, as well as routing infrastructure).通过使用可选的
autoBindDlq
选项,您可以配置绑定器以创建和配置死信队列 (DLQ)(以及死信交换DLX
以及路由基础设施)。 By default, the dead letter queue has the name of the destination, appended with.dlq
.默认情况下,死信队列具有目的地名称,附加
.dlq
。 If retry is enabled (maxAttempts > 1
), failed messages are delivered to the DLQ after retries are exhausted.如果启用了重试(
maxAttempts > 1
),则在重试用尽后,失败的消息将传递到 DLQ。 If retry is disabled (maxAttempts = 1
), you should setrequeueRejected
tofalse
(the default) so that failed messages are routed to the DLQ, instead of being re-queued.如果禁用重试 (
maxAttempts = 1
),则应将requeueRejected
设置为false
(默认值),以便将失败的消息路由到 DLQ,而不是重新排队。 In addition,republishToDlq
causes the binder to publish a failed message to the DLQ (instead of rejecting it).此外,
republishToDlq
会导致 binder 将失败的消息发布到 DLQ(而不是拒绝它)。 This feature lets additional information (such as the stack trace in thex-exception-stacktrace
header) be added to the message in headers.此功能允许将附加信息(例如
x-exception-stacktrace
标头中的堆栈跟踪)添加到标头中的消息中。 See theframeMaxHeadroom
property for information about truncated stack traces.有关截断堆栈跟踪的信息,请参阅
frameMaxHeadroom
属性。 This option does not need retry enabled.此选项不需要启用重试。 You can republish a failed message after just one attempt.
您可以在一次尝试后重新发布失败的消息。 Starting with version 1.2, you can configure the delivery mode of republished messages.
从版本 1.2 开始,您可以配置重新发布消息的传递模式。 See the
republishDeliveryMode
property.请参阅
republishDeliveryMode
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.