From 0827302f4c1bd5c629da7b4abe24b81211772b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Fri, 4 Feb 2022 23:39:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=AE=A2=E5=8D=95=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E5=85=B3=E5=8D=95=E6=AD=BB=E4=BF=A1=E9=98=9F=E5=88=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=B3=E5=85=AC=E5=85=B1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall/oms/config/RabbitMQConfig.java | 76 ------------------- .../rabbitmq/config/RabbitMQConfig.java | 28 +++++++ .../rabbitmq/config/RabbitMqConfig.java | 17 ----- .../rabbitmq/queue/OrderCloseQueue.java | 76 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 5 files changed, 106 insertions(+), 94 deletions(-) delete mode 100644 mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/RabbitMQConfig.java create mode 100644 youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMQConfig.java delete mode 100644 youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMqConfig.java create mode 100644 youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/queue/OrderCloseQueue.java diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/RabbitMQConfig.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/RabbitMQConfig.java deleted file mode 100644 index ce9a9e918..000000000 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/config/RabbitMQConfig.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.youlai.mall.oms.config; - -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.amqp.core.*; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author huawei - * @email huawei_code@163.com - * @date 2021/1/17 - * @desc 业务描述 - * 1. 订单创建成功,发送消息到创建订单的路由 - * 2. 创建订单的路由转发消息给延时队列,延时队列的延时时间就是订单从创建到支付过程,允许的最大等待时间。延时队列不能有消费者(即消息不能被消费) - * 3. 延时时间一到,消息被转入DLX(死信路由) - * 4. 死信路由把死信消息转发给死信队列 - * 5. 订单系统监听死信队列,获取到死信消息后,执行关单解库存操作 - */ -@Configuration -@Slf4j -@AllArgsConstructor -public class RabbitMQConfig { - - /** - * 定义交换机,订单业务统一使用 order.exchange 交换机 - */ - @Bean - public Exchange exchange() { - return new TopicExchange("order.exchange", true, false); - } - - /** - * 延时队列(超时会通过交换机和路由key转发到死信队列),没有消费者 - */ - @Bean - public Queue delayQueue() { - // 延时队列的消息过期了,会自动触发消息的转发,根据routingKey发送到指定的exchange中,exchange路由到死信队列 - Map args = new HashMap<>(); - args.put("x-dead-letter-exchange", "order.exchange"); - args.put("x-dead-letter-routing-key", "order.close"); // 死信路由Key - args.put("x-message-ttl", 60000); // 单位:毫秒,1分钟测试使用 - return new Queue("order.delay.queue", true, false, false, args); - } - - - /** - * 延时队列绑定交换机,路由键order.create - * 订单提交时会发送routingKey=order.create.order的消息至exchange,然后会被路由到上面的delayQueue延时队列,延时队列没有消费者,到期后会将消息转发 - */ - @Bean - public Binding delayQueueBinding() { - return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order.exchange","order.create",null); - } - - /** - * 死信队列(普通队列) - */ - @Bean - public Queue closeOrderQueue() { - return new Queue("order.close.queue", true, false, false); - } - - /** - * 死信队列绑定交换机 - * 其中死信路由的routingKey=order.close和延时队列的routingKey一致,延时队列过期时将消息发送给exchange,exchange再路由到死信队列 - */ - @Bean - public Binding closeOrderQueueBinding() { - return new Binding("order.close.queue", Binding.DestinationType.QUEUE,"order.exchange","order.close",null); - } - -} diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMQConfig.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMQConfig.java new file mode 100644 index 000000000..4e1b1368b --- /dev/null +++ b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMQConfig.java @@ -0,0 +1,28 @@ +package com.youlai.common.rabbitmq.config; + +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * @author huawei + * @desc + * @email huawei_code@163.com + * @date 2021/1/17 + */ +@Configuration +@EnableTransactionManagement +public class RabbitMQConfig { + /** + * 使用json序列化机制,进行消息转换 + * + * @return + */ + @Bean + public MessageConverter jackson2MessageConverter() { + return new Jackson2JsonMessageConverter(); + } + +} diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMqConfig.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMqConfig.java deleted file mode 100644 index 02baa0a9a..000000000 --- a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitMqConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.youlai.common.rabbitmq.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -/** - * @author huawei - * @desc - * @email huawei_code@163.com - * @date 2021/1/17 - */ -@Configuration -@EnableTransactionManagement -public class RabbitMqConfig { - - -} diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/queue/OrderCloseQueue.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/queue/OrderCloseQueue.java new file mode 100644 index 000000000..5fda6ff2e --- /dev/null +++ b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/queue/OrderCloseQueue.java @@ -0,0 +1,76 @@ +package com.youlai.common.rabbitmq.queue; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.Exchange; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +/** + * 订单超时关单死信队列 + * + * @author haoxr + * @date 2022/2/4 23:21 + */ +@Component +@ConditionalOnProperty(prefix = "spring.application.name", value = "mall-oms") +@Slf4j +public class OrderCloseQueue { + + /** + * 定义交换机 + */ + @Bean + public Exchange orderExchange() { + return new TopicExchange("order.exchange", true, false); + } + + /** + * 延时队列 + */ + @Bean + public Queue orderDelayQueue() { + log.info("延时队列(order.delay.queue)创建"); + // 延时队列的消息过期了,会自动触发消息的转发,根据routingKey发送到指定的exchange中,exchange路由到死信队列 + Map args = new HashMap<>(); + args.put("x-dead-letter-exchange", "order.exchange"); + args.put("x-dead-letter-routing-key", "order.close"); // 死信路由Key + args.put("x-message-ttl", 60000); // 单位:毫秒,1分钟测试使用 + return new Queue("order.delay.queue", true, false, false, args); + } + + + /** + * 延时队列绑定交换机 + */ + @Bean + public Binding orderDelayQueueBinding() { + return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order.exchange","order.create",null); + } + + + /** + * 死信队列 + */ + @Bean + public Queue orderCloseQueue() { + log.info("死信队列(order.close.queue)创建"); + return new Queue("order.close.queue", true, false, false); + } + + + /** + * 死信队列绑定交换机 + */ + @Bean + public Binding orderCloseQueueBinding() { + return new Binding("order.close.queue", Binding.DestinationType.QUEUE,"order.exchange","order.close",null); + } + +} diff --git a/youlai-common/common-rabbitmq/src/main/resources/META-INF/spring.factories b/youlai-common/common-rabbitmq/src/main/resources/META-INF/spring.factories index 68ab377f9..072817ce9 100644 --- a/youlai-common/common-rabbitmq/src/main/resources/META-INF/spring.factories +++ b/youlai-common/common-rabbitmq/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.youlai.common.rabbitmq.config.RabbitMqConfig + com.youlai.common.rabbitmq.config.RabbitMQConfig,\ + com.youlai.common.rabbitmq.queue.OrderCloseQueue