update 增加重试机制及死信队列
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package com.m2pool.lease.config;
|
||||
|
||||
|
||||
import com.m2pool.lease.constant.RabbitmqConstant;
|
||||
import org.springframework.amqp.core.*;
|
||||
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -18,6 +20,8 @@ import static com.m2pool.lease.constant.RabbitmqConstant.*;
|
||||
@Configuration
|
||||
public class RabbitMQConfig {
|
||||
|
||||
|
||||
|
||||
@Bean
|
||||
public MessageConverter jackson2JsonMessageConverter() {
|
||||
//自动生成消息唯一id
|
||||
@@ -54,13 +58,31 @@ public class RabbitMQConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
|
||||
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory,
|
||||
RabbitTemplate rabbitTemplate) {
|
||||
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory);
|
||||
//消费者序列化
|
||||
factory.setMessageConverter(jackson2JsonMessageConverter());
|
||||
factory.setConcurrentConsumers(3); // 设置初始消费者数量
|
||||
factory.setMaxConcurrentConsumers(5); // 设置最大消费者数量
|
||||
|
||||
// 配置重试机制 - 使用死信队列恢复器
|
||||
factory.setAdviceChain(org.springframework.amqp.rabbit.config.RetryInterceptorBuilder
|
||||
.stateless()
|
||||
.maxAttempts(3) // 最大重试次数
|
||||
.backOffOptions(5000, 2.0, 60000) // 初始间隔5秒,倍率2.0,最大间隔60秒
|
||||
.recoverer(new org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer(rabbitTemplate,
|
||||
RabbitmqConstant.RETRY_DEAD_LETTER_EXCHANGE,
|
||||
RabbitmqConstant.RETRY_DEAD_LETTER_ROUTING_KEY)) // 重试失败后发送到死信队列
|
||||
.build());
|
||||
|
||||
// 使用自动确认模式,让重试机制正常工作
|
||||
factory.setAcknowledgeMode(org.springframework.amqp.core.AcknowledgeMode.AUTO);
|
||||
|
||||
// 设置预取数量,防止消息积压
|
||||
factory.setPrefetchCount(1);
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
@@ -140,6 +162,34 @@ public class RabbitMQConfig {
|
||||
|
||||
//----------------定义订单延迟队列------------------------
|
||||
|
||||
//----------------定义重试死信队列------------------------
|
||||
|
||||
/**
|
||||
* 重试失败死信交换机
|
||||
*/
|
||||
@Bean
|
||||
public DirectExchange retryDeadLetterExchange() {
|
||||
return new DirectExchange(RabbitmqConstant.RETRY_DEAD_LETTER_EXCHANGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试失败死信队列
|
||||
*/
|
||||
@Bean
|
||||
public Queue retryDeadLetterQueue() {
|
||||
return new Queue(RabbitmqConstant.RETRY_DEAD_LETTER_QUEUE, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试失败死信队列绑定
|
||||
*/
|
||||
@Bean
|
||||
public Binding retryDeadLetterBinding() {
|
||||
return BindingBuilder.bind(retryDeadLetterQueue())
|
||||
.to(retryDeadLetterExchange())
|
||||
.with(RabbitmqConstant.RETRY_DEAD_LETTER_ROUTING_KEY);
|
||||
}
|
||||
|
||||
//----------------定义支付相关队列------------------------
|
||||
/**
|
||||
* 声明 Topic 类型的交换机
|
||||
|
||||
Reference in New Issue
Block a user