From c7bb7d206f1dcab94045cc9e45cfc4160d86a3d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Tue, 5 Apr 2022 22:14:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(mall-oms):=20=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=9C=AA=E6=94=AF=E4=BB=98=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=AE=A2=E5=8D=95=E7=9A=84=E6=AD=BB=E4=BF=A1?= =?UTF-8?q?=E9=98=9F=E5=88=97=E8=B0=83=E6=95=B4=E6=A0=B9=E6=8D=AE=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8A=A8=E6=80=81=E5=88=9B=E5=BB=BA=E3=80=81=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E4=BA=A4=E6=8D=A2=E6=9C=BA=E5=92=8C=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall/oms/pojo/form/OrderSubmitForm.java | 8 ++-- .../oms/service/impl/OrderServiceImpl.java | 2 +- .../mall/oms/controller/RabbitMQTest.java | 2 +- .../rabbitmq/dynamic/RabbitModuleInfo.java | 13 +------ .../dynamic/RabbitModuleInitializer.java | 38 +++++++++++-------- .../rabbitmq/queue/OrderCloseQueue.java | 17 ++++----- .../main/resources/META-INF/spring.factories | 1 - 7 files changed, 36 insertions(+), 45 deletions(-) diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/pojo/form/OrderSubmitForm.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/pojo/form/OrderSubmitForm.java index ac6bf00ae..916b0a4fb 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/pojo/form/OrderSubmitForm.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/pojo/form/OrderSubmitForm.java @@ -27,7 +27,6 @@ public class OrderSubmitForm { */ private Long totalAmount; - /** * 支付金额(单位:分) */ @@ -38,9 +37,9 @@ public class OrderSubmitForm { */ private List orderItems; - // 收货地址 - - + /** + * 订单备注 + */ @Size(max = 500, message = "订单备注长度不能超过500") private String remark; @@ -49,7 +48,6 @@ public class OrderSubmitForm { */ private String couponId; - /** * 收获地址 */ diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java index 47e56151f..68123101d 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java @@ -191,7 +191,7 @@ public class OrderServiceImpl extends ServiceImpl impleme result = orderItemService.saveBatch(saveOrderItems); if (result) { // 订单超时取消 - rabbitTemplate.convertAndSend("order.exchange", "order.create", orderToken); + rabbitTemplate.convertAndSend("order.exchange", "order.create.routing.key", orderToken); } } Assert.isTrue(result, "订单提交失败"); diff --git a/mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/RabbitMQTest.java b/mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/RabbitMQTest.java index 10fbaa35d..4027b7798 100644 --- a/mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/RabbitMQTest.java +++ b/mall-oms/oms-boot/src/test/java/com/youlai/mall/oms/controller/RabbitMQTest.java @@ -15,6 +15,6 @@ public class RabbitMQTest { @Test public void createOrderTest() { - rabbitTemplate.convertAndSend("order.exchange", "order.create", "4acd475a-c6aa-4d9a-a3a5-40da7472cbee"); + rabbitTemplate.convertAndSend("order.exchange", "order.create.routing.key", "4acd475a-c6aa-4d9a-a3a5-40da7472cbee"); } } diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInfo.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInfo.java index 8c0a36d60..d3c600a04 100644 --- a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInfo.java +++ b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInfo.java @@ -55,11 +55,6 @@ public class RabbitModuleInfo { */ private boolean autoDelete = false; // 默认false,不自动删除 - /** - * 是否延迟交换机 - */ - private boolean delayed; - /** * 交换机其他参数 */ @@ -96,16 +91,12 @@ public class RabbitModuleInfo { /** * 绑定死信队列的交换机名称 */ - private String deadExchangeName; - + private String deadLetterExchange; /** * 绑定死信队列的路由key */ - private String deadRoutingKey; - - - private Long messageTtl; + private String deadLetterRoutingKey; private Map arguments; diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInitializer.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInitializer.java index 85a9fa169..af7c4c276 100644 --- a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInitializer.java +++ b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/dynamic/RabbitModuleInitializer.java @@ -1,6 +1,7 @@ package com.youlai.common.rabbitmq.dynamic; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.convert.Convert; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; @@ -31,14 +32,14 @@ public class RabbitModuleInitializer implements SmartInitializingSingleton { @Override public void afterSingletonsInstantiated() { - log.info("初始化 RabbitMQ 队列、交换机和绑定关系"); - initRabbitModule(); + log.info("RabbitMQ 根据配置动态创建和绑定队列、交换机"); + declareRabbitModule(); } /** - * 初始化 RabbitMQ 队列、交换机和绑定关系 + * RabbitMQ 根据配置动态创建和绑定队列、交换机 */ - private void initRabbitModule() { + private void declareRabbitModule() { List rabbitModuleInfos = rabbitModuleProperties.getModules(); if (CollectionUtil.isEmpty(rabbitModuleInfos)) { return; @@ -47,9 +48,9 @@ public class RabbitModuleInitializer implements SmartInitializingSingleton { configParamValidate(rabbitModuleInfo); // 队列 - Queue queue = convertToQueue(rabbitModuleInfo.getQueue()); + Queue queue = convertQueue(rabbitModuleInfo.getQueue()); // 交换机 - Exchange exchange = convertToExchange(rabbitModuleInfo.getExchange()); + Exchange exchange = convertExchange(rabbitModuleInfo.getExchange()); // 绑定关系 String routingKey = rabbitModuleInfo.getRoutingKey(); String queueName = rabbitModuleInfo.getQueue().getName(); @@ -88,19 +89,25 @@ public class RabbitModuleInitializer implements SmartInitializingSingleton { * @param queue * @return */ - public Queue convertToQueue(RabbitModuleInfo.Queue queue) { + public Queue convertQueue(RabbitModuleInfo.Queue queue) { Map arguments = queue.getArguments(); - // 是否需要绑定死信队列 - String deadExchangeName = queue.getDeadExchangeName(); - String deadRoutingKey = queue.getDeadRoutingKey(); - if (StrUtil.isNotBlank(deadExchangeName) && StrUtil.isNotBlank(deadRoutingKey)) { + // 转换ttl的类型为long + if (arguments != null && arguments.containsKey("x-message-ttl")) { + arguments.put("x-message-ttl", Convert.toLong(arguments.get("x-message-ttl"))); + } - if (arguments != null) { + // 是否需要绑定死信队列 + String deadLetterExchange = queue.getDeadLetterExchange(); + String deadLetterRoutingKey = queue.getDeadLetterRoutingKey(); + if (StrUtil.isNotBlank(deadLetterExchange) && StrUtil.isNotBlank(deadLetterRoutingKey)) { + + if (arguments == null) { arguments = new HashMap<>(4); - arguments.put("x-dead-letter-exchange", deadExchangeName); - arguments.put("x-dead-letter-routing-key", deadRoutingKey); } + arguments.put("x-dead-letter-exchange", deadLetterExchange); + arguments.put("x-dead-letter-routing-key", deadLetterRoutingKey); + } return new Queue(queue.getName(), queue.isDurable(), queue.isExclusive(), queue.isAutoDelete(), arguments); @@ -113,7 +120,7 @@ public class RabbitModuleInitializer implements SmartInitializingSingleton { * @param exchangeInfo * @return */ - public Exchange convertToExchange(RabbitModuleInfo.Exchange exchangeInfo) { + public Exchange convertExchange(RabbitModuleInfo.Exchange exchangeInfo) { AbstractExchange exchange = null; @@ -140,7 +147,6 @@ public class RabbitModuleInitializer implements SmartInitializingSingleton { break; } - exchange.setDelayed(exchangeInfo.isDelayed()); return exchange; } 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 index d9f3700de..6abecaa35 100644 --- 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 @@ -1,10 +1,7 @@ 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.amqp.core.*; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @@ -18,8 +15,8 @@ import java.util.Map; * @author haoxr * @date 2022/2/4 23:21 */ -@Component -@ConditionalOnProperty(prefix = "spring.application.name", value = "mall-oms") + +@Deprecated @Slf4j public class OrderCloseQueue { @@ -28,7 +25,7 @@ public class OrderCloseQueue { */ @Bean public Exchange orderExchange() { - return new TopicExchange("order.exchange", true, false); + return new DirectExchange("order.exchange", true, false); } /** @@ -40,7 +37,7 @@ public class OrderCloseQueue { // 延时队列的消息过期了,会自动触发消息的转发,根据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-dead-letter-routing-key", "order.close.routing.key"); // 死信路由Key args.put("x-message-ttl", 60 * 1000L); // 单位:毫秒,1分钟测试使用 return new Queue("order.delay.queue", true, false, false, args); } @@ -51,7 +48,7 @@ public class OrderCloseQueue { */ @Bean public Binding orderDelayQueueBinding() { - return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order.exchange","order.create",null); + return new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order.exchange", "order.create.routing.key", null); } @@ -69,7 +66,7 @@ public class OrderCloseQueue { */ @Bean public Binding orderCloseQueueBinding() { - return new Binding("order.close.queue", Binding.DestinationType.QUEUE,"order.exchange","order.close",null); + return new Binding("order.close.queue", Binding.DestinationType.QUEUE, "order.exchange", "order.close.routing.key", 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 f4f88880a..024feda7a 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,4 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.youlai.common.rabbitmq.config.RabbitConfig,\ - com.youlai.common.rabbitmq.queue.OrderCloseQueue,\ com.youlai.common.rabbitmq.dynamic.RabbitModuleProperties