From 938d8592423cc9d66a65fe3079e149ab81ef3d7c Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Thu, 4 Apr 2024 18:14:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(search):=20=E6=95=B4=E5=90=88rabbitMQ?= =?UTF-8?q?=E5=8F=91=E6=B6=88=E6=81=AF=EF=BC=8C=E5=95=86=E5=93=81=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/encodings.xml | 1 + common/pom.xml | 1 + common/rabbit-util/pom.xml | 26 +++++++++ .../ssyx/mq/config/RabbitMQConfiguration.java | 28 ++++++++++ .../com/atguigu/ssyx/mq/constant/MqConst.java | 53 +++++++++++++++++++ .../ssyx/mq/service/RabbitService.java | 22 ++++++++ service/service-product/pom.xml | 6 +++ .../product/ServiceProductApplication.java | 2 +- .../service/impl/SkuInfoServiceImpl.java | 9 +++- .../src/main/resources/application-dev.yml | 6 +++ .../src/main/resources/application.yml | 18 +++++++ service/service-search/pom.xml | 5 ++ .../ssyx/search/MqListener/SkuListener.java | 40 ++++++++++++++ .../ssyx/search/ServiceSearchApplication.java | 2 +- .../src/main/resources/application-dev.yml | 2 +- .../src/main/resources/application.yml | 8 ++- .../src/main/resources/application-dev.yml | 8 +-- .../src/main/resources/application.yml | 9 +--- 18 files changed, 226 insertions(+), 20 deletions(-) create mode 100644 common/rabbit-util/pom.xml create mode 100644 common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/config/RabbitMQConfiguration.java create mode 100644 common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java create mode 100644 common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java create mode 100644 service/service-search/src/main/java/com/atguigu/ssyx/search/MqListener/SkuListener.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 7556275..3a83e53 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -4,6 +4,7 @@ + diff --git a/common/pom.xml b/common/pom.xml index bff215d..721a070 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -15,6 +15,7 @@ common-util service-util + rabbit-util diff --git a/common/rabbit-util/pom.xml b/common/rabbit-util/pom.xml new file mode 100644 index 0000000..1878dfd --- /dev/null +++ b/common/rabbit-util/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + com.atguigu + common + 1.0-SNAPSHOT + + rabbit-util + Archetype - rabbit-util + https://maven.apache.org + + + + + org.springframework.cloud + spring-cloud-starter-bus-amqp + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.16.0-rc1 + + + diff --git a/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/config/RabbitMQConfiguration.java b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/config/RabbitMQConfiguration.java new file mode 100644 index 0000000..056e457 --- /dev/null +++ b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/config/RabbitMQConfiguration.java @@ -0,0 +1,28 @@ +package com.atguigu.ssyx.mq.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; + +@Configuration +public class RabbitMQConfiguration { + /** + * * 重写,并引入消息转换器 + * + * com.fasterxml.jackson.dataformat + * jackson-dataformat-xml + * 2.16.0-rc1 + * + * + * @return MessageConverter + */ + @Bean + public MessageConverter jsonMessageConverter() { + // 1.定义消息转换器 + Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(); + // 2.配置自动创建消息id,用于识别不同消息,也可以在业务中基于ID判断是否是重复消息 + converter.setCreateMessageIds(true); + return converter; + } +} \ No newline at end of file diff --git a/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java new file mode 100644 index 0000000..abaf16e --- /dev/null +++ b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java @@ -0,0 +1,53 @@ +package com.atguigu.ssyx.mq.constant; + +public class MqConst { + // 消息补偿 + public static final String MQ_KEY_PREFIX = "ssyx.mq:list"; + public static final int RETRY_COUNT = 3; + + // 商品上下架 + public static final String EXCHANGE_GOODS_DIRECT = "ssyx.goods.direct"; + public static final String ROUTING_GOODS_UPPER = "ssyx.goods.upper"; + public static final String ROUTING_GOODS_LOWER = "ssyx.goods.lower"; + // 队列 + public static final String QUEUE_GOODS_UPPER = "ssyx.goods.upper"; + public static final String QUEUE_GOODS_LOWER = "ssyx.goods.lower"; + + // 团长上下线 + public static final String EXCHANGE_LEADER_DIRECT = "ssyx.leader.direct"; + public static final String ROUTING_LEADER_UPPER = "ssyx.leader.upper"; + public static final String ROUTING_LEADER_LOWER = "ssyx.leader.lower"; + // 队列 + public static final String QUEUE_LEADER_UPPER = "ssyx.leader.upper"; + public static final String QUEUE_LEADER_LOWER = "ssyx.leader.lower"; + + // 订单 + public static final String EXCHANGE_ORDER_DIRECT = "ssyx.order.direct"; + public static final String ROUTING_ROLLBACK_STOCK = "ssyx.rollback.stock"; + public static final String ROUTING_MINUS_STOCK = "ssyx.minus.stock"; + + public static final String ROUTING_DELETE_CART = "ssyx.delete.cart"; + // 解锁普通商品库存 + public static final String QUEUE_ROLLBACK_STOCK = "ssyx.rollback.stock"; + public static final String QUEUE_SECKILL_ROLLBACK_STOCK = "ssyx.seckill.rollback.stock"; + public static final String QUEUE_MINUS_STOCK = "ssyx.minus.stock"; + public static final String QUEUE_DELETE_CART = "ssyx.delete.cart"; + + // 支付 + public static final String EXCHANGE_PAY_DIRECT = "ssyx.pay.direct"; + public static final String ROUTING_PAY_SUCCESS = "ssyx.pay.success"; + public static final String QUEUE_ORDER_PAY = "ssyx.order.pay"; + public static final String QUEUE_LEADER_BILL = "ssyx.leader.bill"; + + // 取消订单 + public static final String EXCHANGE_CANCEL_ORDER_DIRECT = "ssyx.cancel.order.direct"; + public static final String ROUTING_CANCEL_ORDER = "ssyx.cancel.order"; + // 延迟取消订单队列 + public static final String QUEUE_CANCEL_ORDER = "ssyx.cancel.order"; + + // 定时任务 + public static final String EXCHANGE_DIRECT_TASK = "ssyx.exchange.direct.task"; + public static final String ROUTING_TASK_23 = "ssyx.task.23"; + // 队列 + public static final String QUEUE_TASK_23 = "ssyx.queue.task.23"; +} \ No newline at end of file diff --git a/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java new file mode 100644 index 0000000..2b88788 --- /dev/null +++ b/common/rabbit-util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java @@ -0,0 +1,22 @@ +package com.atguigu.ssyx.mq.service; + +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class RabbitService { + @Autowired + private RabbitTemplate rabbitTemplate; + + /** + * 发送消息 + * + * @param exchange 交换机 + * @param routerKey 路由 + * @param message 消息 + */ + public void sendMessage(String exchange, String routerKey, Object message) { + rabbitTemplate.convertAndSend(exchange, routerKey, message); + } +} \ No newline at end of file diff --git a/service/service-product/pom.xml b/service/service-product/pom.xml index 5683617..7f9b4ff 100644 --- a/service/service-product/pom.xml +++ b/service/service-product/pom.xml @@ -18,6 +18,12 @@ + + com.atguigu + rabbit-util + 1.0-SNAPSHOT + + cn.hutool hutool-all diff --git a/service/service-product/src/main/java/com/atguigu/ssyx/product/ServiceProductApplication.java b/service/service-product/src/main/java/com/atguigu/ssyx/product/ServiceProductApplication.java index 9fdb154..77b7367 100644 --- a/service/service-product/src/main/java/com/atguigu/ssyx/product/ServiceProductApplication.java +++ b/service/service-product/src/main/java/com/atguigu/ssyx/product/ServiceProductApplication.java @@ -6,7 +6,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication -@ComponentScan(basePackages = {"com.atguigu.ssyx.common"}) +@ComponentScan(basePackages = {"com.atguigu.ssyx.common", "com.atguigu.ssyx.mq"}) @EnableTransactionManagement public class ServiceProductApplication { public static void main(String[] args) { diff --git a/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java b/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java index f740277..26f0f20 100644 --- a/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java +++ b/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java @@ -4,6 +4,8 @@ import com.atguigu.ssyx.model.product.SkuAttrValue; import com.atguigu.ssyx.model.product.SkuImage; import com.atguigu.ssyx.model.product.SkuInfo; import com.atguigu.ssyx.model.product.SkuPoster; +import com.atguigu.ssyx.mq.constant.MqConst; +import com.atguigu.ssyx.mq.service.RabbitService; import com.atguigu.ssyx.product.mapper.SkuInfoMapper; import com.atguigu.ssyx.product.service.SkuAttrValueService; import com.atguigu.ssyx.product.service.SkuImageService; @@ -41,7 +43,8 @@ public class SkuInfoServiceImpl extends ServiceImpl impl private SkuImageService skuImageService; @Autowired private SkuPosterService skuPosterService; - + @Autowired + private RabbitService rabbitService; /** * 获取sku分页列表 @@ -197,8 +200,12 @@ public class SkuInfoServiceImpl extends ServiceImpl impl skuInfo.setId(skuId); if (status == 1) { skuInfo.setPublishStatus(status); + // 商品上架,发送MQ消息 + rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_UPPER, skuId); } else { skuInfo.setPublishStatus(0); + // 商品下架:发送mq消息同步es + rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_LOWER, skuId); } baseMapper.updateById(skuInfo); } diff --git a/service/service-product/src/main/resources/application-dev.yml b/service/service-product/src/main/resources/application-dev.yml index f955656..189e4ce 100644 --- a/service/service-product/src/main/resources/application-dev.yml +++ b/service/service-product/src/main/resources/application-dev.yml @@ -2,6 +2,12 @@ server: port: 8203 bunny: + rabbitmq: + host: 192.168.1.4 + port: 5672 + username: bunny + password: "02120212" + datasource: host: 106.15.251.123 port: 3305 diff --git a/service/service-product/src/main/resources/application.yml b/service/service-product/src/main/resources/application.yml index 8d33931..95db11c 100644 --- a/service/service-product/src/main/resources/application.yml +++ b/service/service-product/src/main/resources/application.yml @@ -26,6 +26,24 @@ spring: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 + rabbitmq: + host: ${bunny.rabbitmq.host} + port: ${bunny.rabbitmq.port} + username: ${bunny.rabbitmq.username} + password: ${bunny.rabbitmq.password} + publisher-confirm-type: CORRELATED + publisher-returns: true + listener: + simple: + prefetch: 1 + concurrency: 3 + acknowledge-mode: manual + retry: + enabled: true # 开启消费者失败重试 + initial-interval: 1000ms # 初始失败等待时长 + multiplier: 1 # 下次失败等待时间被树,下次等待时长 multiplier * last-interval + max-attempts: 3 # 最大重试次数 + stateless: true # true 无状态 false 有状态。如果业务中包含事务,这里改为false mybatis-plus: type-aliases-package: com.atguigu.model # 配置每个包前缀 diff --git a/service/service-search/pom.xml b/service/service-search/pom.xml index a7554ea..4859f50 100644 --- a/service/service-search/pom.xml +++ b/service/service-search/pom.xml @@ -18,6 +18,11 @@ + + com.atguigu + rabbit-util + 1.0-SNAPSHOT + com.atguigu service-product-client diff --git a/service/service-search/src/main/java/com/atguigu/ssyx/search/MqListener/SkuListener.java b/service/service-search/src/main/java/com/atguigu/ssyx/search/MqListener/SkuListener.java new file mode 100644 index 0000000..2ef9022 --- /dev/null +++ b/service/service-search/src/main/java/com/atguigu/ssyx/search/MqListener/SkuListener.java @@ -0,0 +1,40 @@ +package com.atguigu.ssyx.search.MqListener; + +import com.atguigu.ssyx.mq.constant.MqConst; +import com.rabbitmq.client.Channel; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.Exchange; +import org.springframework.amqp.rabbit.annotation.Queue; +import org.springframework.amqp.rabbit.annotation.QueueBinding; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class SkuListener { + // 商品上架 + @RabbitListener(bindings = @QueueBinding( + value = @Queue(name = MqConst.QUEUE_GOODS_UPPER, durable = "true"), + exchange = @Exchange(name = MqConst.EXCHANGE_GOODS_DIRECT), + key = {MqConst.ROUTING_GOODS_UPPER} + )) + public void upperSku(Long skuId, Message message, Channel channel) throws IOException { + if (skuId != null) { + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + } + } + + // 商品下架 + @RabbitListener(bindings = @QueueBinding( + value = @Queue(name = MqConst.QUEUE_GOODS_LOWER, durable = "true"), + exchange = @Exchange(name = MqConst.EXCHANGE_GOODS_DIRECT), + key = {MqConst.ROUTING_GOODS_LOWER} + )) + public void lowerSku(Long skuId, Message message, Channel channel) throws IOException { + if (skuId != null) { + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + } + } +} + diff --git a/service/service-search/src/main/java/com/atguigu/ssyx/search/ServiceSearchApplication.java b/service/service-search/src/main/java/com/atguigu/ssyx/search/ServiceSearchApplication.java index eada2c8..fd78dfe 100644 --- a/service/service-search/src/main/java/com/atguigu/ssyx/search/ServiceSearchApplication.java +++ b/service/service-search/src/main/java/com/atguigu/ssyx/search/ServiceSearchApplication.java @@ -8,7 +8,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) -@ComponentScan(basePackages = {"com.atguigu.ssyx.common"}) +@ComponentScan(basePackages = {"com.atguigu.ssyx.common", "com.atguigu.ssyx.mq"}) @EnableFeignClients(basePackages = {"com.atguigu.ssyx.client"}) @EnableDiscoveryClient public class ServiceSearchApplication { diff --git a/service/service-search/src/main/resources/application-dev.yml b/service/service-search/src/main/resources/application-dev.yml index 87baa8f..e1320f4 100644 --- a/service/service-search/src/main/resources/application-dev.yml +++ b/service/service-search/src/main/resources/application-dev.yml @@ -3,7 +3,7 @@ server: bunny: rabbitmq: - host: 116.196.101.14 + host: 192.168.1.4 port: 5672 username: bunny password: "02120212" diff --git a/service/service-search/src/main/resources/application.yml b/service/service-search/src/main/resources/application.yml index c74c324..da807b7 100644 --- a/service/service-search/src/main/resources/application.yml +++ b/service/service-search/src/main/resources/application.yml @@ -34,6 +34,12 @@ spring: prefetch: 1 concurrency: 3 acknowledge-mode: manual + retry: + enabled: true # 开启消费者失败重试 + initial-interval: 1000ms # 初始失败等待时长 + multiplier: 1 # 下次失败等待时间被树,下次等待时长 multiplier * last-interval + max-attempts: 3 # 最大重试次数 + stateless: true # true 无状态 false 有状态。如果业务中包含事务,这里改为false cloud: sentinel: @@ -60,7 +66,7 @@ feign: enabled: true client: config: - default: #配置全局的feign的调用超时时间 如果 有指定的服务配置 默认的配置不会生效 + default: #配置全局的feign的调用超时时间 如果 有指定的服务配置 默认的配置不会生效 connectTimeout: 30000 # 指定的是 消费者 连接服务提供者的连接超时时间 是否能连接 单位是毫秒 readTimeout: 50000 # 指定的是调用服务提供者的 服务 的超时时间() 单位是毫秒 diff --git a/service/service-sys/src/main/resources/application-dev.yml b/service/service-sys/src/main/resources/application-dev.yml index 0b0222f..314bbeb 100644 --- a/service/service-sys/src/main/resources/application-dev.yml +++ b/service/service-sys/src/main/resources/application-dev.yml @@ -12,10 +12,4 @@ bunny: nacos: server-addr: z-bunny.cn:8848 discovery: - namespace: ssyx - - minio: - endpointUrl: "http://129.211.31.58:9000" - bucket-name: ssyx - accessKey: bunny - secretKey: "02120212" \ No newline at end of file + namespace: ssyx \ No newline at end of file diff --git a/service/service-sys/src/main/resources/application.yml b/service/service-sys/src/main/resources/application.yml index ae79b49..8a4d40a 100644 --- a/service/service-sys/src/main/resources/application.yml +++ b/service/service-sys/src/main/resources/application.yml @@ -53,11 +53,4 @@ logging: pattern: dateformat: HH:mm:ss:SSS file: - path: "logs/${spring.application.name}" - -# bunny: -# minio: -# endpointUrl: ${bunny.minio.endpointUrl} -# accessKey: ${bunny.minio.accessKey} -# secretKey: ${bunny.minio.secretKey} -# bucket-name: ${bunny.minio.bucket-name} \ No newline at end of file + path: "logs/${spring.application.name}" \ No newline at end of file