From 3019bc57370a1696ce587e692fe2f75d28085d69 Mon Sep 17 00:00:00 2001 From: yovinchen Date: Sun, 17 Sep 2023 00:20:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88es=E3=80=81rabbitmq=E3=80=81r?= =?UTF-8?q?edis=E5=AE=8C=E6=88=90=E4=B8=8A=E4=B8=8B=E6=9E=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- guigu-ssyx-parent/common/pom.xml | 1 + guigu-ssyx-parent/common/rabbit_util/pom.xml | 26 ++++++ .../com/atguigu/ssyx/mq/config/MQConfig.java | 22 +++++ .../ssyx/mq/config/MQProducerAckConfig.java | 66 ++++++++++++++ .../com/atguigu/ssyx/mq/constant/MqConst.java | 68 +++++++++++++++ .../ssyx/mq/service/RabbitService.java | 56 ++++++++++++ guigu-ssyx-parent/pom.xml | 1 + guigu-ssyx-parent/service-client/pom.xml | 51 +++++++++++ .../service-product-client/pom.xml | 20 +++++ .../client/product/ProductFeignClient.java | 24 +++++ guigu-ssyx-parent/service/pom.xml | 1 + .../service/service-product/pom.xml | 6 ++ .../product/api/ProductInnnerController.java | 43 +++++++++ .../product/controller/SkuInfoController.java | 2 + .../ssyx/product/service/SkuInfoService.java | 1 - .../service/impl/SkuInfoServiceImpl.java | 12 ++- .../src/main/resources/application-dev.yml | 12 +++ .../service/service-search/pom.xml | 43 +++++++++ .../ssyx/ServiceSearchApplication.java | 24 +++++ .../search/controller/SkuApiController.java | 39 +++++++++ .../ssyx/search/receiver/SkuReceiver.java | 73 ++++++++++++++++ .../ssyx/search/repository/SkuRepository.java | 14 +++ .../ssyx/search/service/SkuService.java | 29 +++++++ .../search/service/impl/SkuServiceImpl.java | 87 +++++++++++++++++++ .../src/main/resources/application-dev.yml | 41 +++++++++ .../src/main/resources/application.yml | 11 +++ 26 files changed, 770 insertions(+), 3 deletions(-) create mode 100644 guigu-ssyx-parent/common/rabbit_util/pom.xml create mode 100644 guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQConfig.java create mode 100644 guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQProducerAckConfig.java create mode 100644 guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java create mode 100644 guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java create mode 100644 guigu-ssyx-parent/service-client/pom.xml create mode 100644 guigu-ssyx-parent/service-client/service-product-client/pom.xml create mode 100644 guigu-ssyx-parent/service-client/service-product-client/src/main/java/com/atguigu/ssyx/client/product/ProductFeignClient.java create mode 100644 guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/api/ProductInnnerController.java create mode 100644 guigu-ssyx-parent/service/service-search/pom.xml create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/ServiceSearchApplication.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/controller/SkuApiController.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/receiver/SkuReceiver.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/repository/SkuRepository.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/SkuService.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/impl/SkuServiceImpl.java create mode 100644 guigu-ssyx-parent/service/service-search/src/main/resources/application-dev.yml create mode 100644 guigu-ssyx-parent/service/service-search/src/main/resources/application.yml diff --git a/guigu-ssyx-parent/common/pom.xml b/guigu-ssyx-parent/common/pom.xml index c31f9ee..77b9f52 100644 --- a/guigu-ssyx-parent/common/pom.xml +++ b/guigu-ssyx-parent/common/pom.xml @@ -14,6 +14,7 @@ common-util service-util + rabbit_util diff --git a/guigu-ssyx-parent/common/rabbit_util/pom.xml b/guigu-ssyx-parent/common/rabbit_util/pom.xml new file mode 100644 index 0000000..b5b3b25 --- /dev/null +++ b/guigu-ssyx-parent/common/rabbit_util/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.atguigu + common + 1.0-SNAPSHOT + + + rabbit_util + + + + org.springframework.cloud + spring-cloud-starter-bus-amqp + + + + 8 + 8 + UTF-8 + + + diff --git a/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQConfig.java b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQConfig.java new file mode 100644 index 0000000..448b84b --- /dev/null +++ b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQConfig.java @@ -0,0 +1,22 @@ +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; + +/** + * ClassName: MQConfig + * Package: com.atguigu.ssyx.mq.config + * + * @author yovinchen + * @Create 2023/9/16 23:32 + */ +@Configuration +public class MQConfig { + + @Bean + public MessageConverter messageConverter() { + return new Jackson2JsonMessageConverter(); + } +} diff --git a/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQProducerAckConfig.java b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQProducerAckConfig.java new file mode 100644 index 0000000..1135d13 --- /dev/null +++ b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/config/MQProducerAckConfig.java @@ -0,0 +1,66 @@ +package com.atguigu.ssyx.mq.config; + +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * ClassName: MQProducerAckConfig + * Package: com.atguigu.ssyx.mq.config + * + * @author yovinchen + * @Create 2023/9/16 23:33 + */ +@Component +public class MQProducerAckConfig implements RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback { + + // 我们发送消息使用的是 private RabbitTemplate rabbitTemplate; 对象 + // 如果不做设置的话 当前的rabbitTemplate 与当前的配置类没有任何关系! + @Autowired + private RabbitTemplate rabbitTemplate; + + // 设置 表示修饰一个非静态的void方法,在服务器加载Servlet的时候运行。并且只执行一次! + @PostConstruct + public void init() { + rabbitTemplate.setReturnCallback(this); + rabbitTemplate.setConfirmCallback(this); + } + + /** + * 表示消息是否正确发送到了交换机上 + * + * @param correlationData 消息的载体 + * @param ack 判断是否发送到交换机上 + * @param cause 原因 + */ + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + if (ack) { + System.out.println("消息发送成功!"); + } else { + System.out.println("消息发送失败!" + cause); + } + } + + /** + * 消息如果没有正确发送到队列中,则会走这个方法!如果消息被正常处理,则这个方法不会走! + * + * @param message + * @param replyCode + * @param replyText + * @param exchange + * @param routingKey + */ + @Override + public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { + System.out.println("消息主体: " + new String(message.getBody())); + System.out.println("应答码: " + replyCode); + System.out.println("描述:" + replyText); + System.out.println("消息使用的交换器 exchange : " + exchange); + System.out.println("消息使用的路由键 routing : " + routingKey); + } +} diff --git a/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java new file mode 100644 index 0000000..00e08de --- /dev/null +++ b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/constant/MqConst.java @@ -0,0 +1,68 @@ +package com.atguigu.ssyx.mq.constant; + +/** + * ClassName: MqConst + * Package: com.atguigu.ssyx.mq.constant + * + * @author yovinchen + * @Create 2023/9/16 23:34 + */ +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"; +} diff --git a/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java new file mode 100644 index 0000000..f73d95d --- /dev/null +++ b/guigu-ssyx-parent/common/rabbit_util/src/main/java/com/atguigu/ssyx/mq/service/RabbitService.java @@ -0,0 +1,56 @@ +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; + +/** + * ClassName: RabbitService + * Package: com.atguigu.ssyx.common.service + * + * @author yovinchen + * @Create 2023/9/16 18:12 + */ +@Service +public class RabbitService { + + // 引入操作rabbitmq 的模板 + @Autowired + private RabbitTemplate rabbitTemplate; + + /** + * 发送消息 + * + * @param exchange 交换机 + * @param routingKey 路由键 + * @param message 消息 + * @return + */ + public boolean sendMessage(String exchange, String routingKey, Object message) { + // 调用发送数据的方法 + rabbitTemplate.convertAndSend(exchange, routingKey, message); + return true; + } + + /** + * 发送延迟消息的方法 + * + * @param exchange 交换机 + * @param routingKey 路由键 + * @param message 消息内容 + * @param delayTime 延迟时间 + * @return + */ + public boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime) { + + // 在发送消息的时候设置延迟时间 + rabbitTemplate.convertAndSend(exchange, routingKey, message, message1 -> { + // 设置一个延迟时间 + message1 + .getMessageProperties() + .setDelay(delayTime * 1000); + return message1; + }); + return true; + } +} diff --git a/guigu-ssyx-parent/pom.xml b/guigu-ssyx-parent/pom.xml index dbf0974..183a7bf 100644 --- a/guigu-ssyx-parent/pom.xml +++ b/guigu-ssyx-parent/pom.xml @@ -12,6 +12,7 @@ common model service + service-client diff --git a/guigu-ssyx-parent/service-client/pom.xml b/guigu-ssyx-parent/service-client/pom.xml new file mode 100644 index 0000000..169e45d --- /dev/null +++ b/guigu-ssyx-parent/service-client/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + com.atguigu + guigu-ssyx-parent + 1.0-SNAPSHOT + + + service-client + pom + + service-product-client + + + + com.atguigu + common-util + 1.0-SNAPSHOT + provided + + + + com.atguigu + model + 1.0-SNAPSHOT + provided + + + + org.springframework.boot + spring-boot-starter-web + provided + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + provided + + + + 8 + 8 + UTF-8 + + + diff --git a/guigu-ssyx-parent/service-client/service-product-client/pom.xml b/guigu-ssyx-parent/service-client/service-product-client/pom.xml new file mode 100644 index 0000000..fd9f6c8 --- /dev/null +++ b/guigu-ssyx-parent/service-client/service-product-client/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.atguigu + service-client + 1.0-SNAPSHOT + + + service-product-client + + + 8 + 8 + UTF-8 + + + diff --git a/guigu-ssyx-parent/service-client/service-product-client/src/main/java/com/atguigu/ssyx/client/product/ProductFeignClient.java b/guigu-ssyx-parent/service-client/service-product-client/src/main/java/com/atguigu/ssyx/client/product/ProductFeignClient.java new file mode 100644 index 0000000..d1ae3b1 --- /dev/null +++ b/guigu-ssyx-parent/service-client/service-product-client/src/main/java/com/atguigu/ssyx/client/product/ProductFeignClient.java @@ -0,0 +1,24 @@ +package com.atguigu.ssyx.client.product; + +import com.atguigu.ssyx.model.product.Category; +import com.atguigu.ssyx.model.product.SkuInfo; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * ClassName: ProductFeignClient + * Package: com.atguigu.ssyx.client.product + * + * @author yovinchen + * @Create 2023/9/16 17:45 + */ +@FeignClient(value = "service-product") +public interface ProductFeignClient { + + @GetMapping("/api/product/inner/getCategory/{categoryId}") + Category getCategory(@PathVariable("categoryId") Long categoryId); + + @GetMapping("/api/product/inner/getSkuInfo/{skuId}") + SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId); +} diff --git a/guigu-ssyx-parent/service/pom.xml b/guigu-ssyx-parent/service/pom.xml index 7a756c1..753b71a 100644 --- a/guigu-ssyx-parent/service/pom.xml +++ b/guigu-ssyx-parent/service/pom.xml @@ -15,6 +15,7 @@ service-acl service-sys service-product + service-search diff --git a/guigu-ssyx-parent/service/service-product/pom.xml b/guigu-ssyx-parent/service/service-product/pom.xml index 9ca3279..2d8c6c5 100644 --- a/guigu-ssyx-parent/service/service-product/pom.xml +++ b/guigu-ssyx-parent/service/service-product/pom.xml @@ -23,6 +23,12 @@ joda-time 2.10.1 + + com.atguigu + rabbit_util + 1.0-SNAPSHOT + compile + 8 diff --git a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/api/ProductInnnerController.java b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/api/ProductInnnerController.java new file mode 100644 index 0000000..c147afb --- /dev/null +++ b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/api/ProductInnnerController.java @@ -0,0 +1,43 @@ +package com.atguigu.ssyx.product.api; + +import com.atguigu.ssyx.model.product.Category; +import com.atguigu.ssyx.model.product.SkuInfo; +import com.atguigu.ssyx.product.service.CategoryService; +import com.atguigu.ssyx.product.service.SkuInfoService; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * ClassName: ProductInnnerController + * Package: com.atguigu.ssyx.product.api + * + * @author yovinchen + * @Create 2023/9/16 17:28 + */ +@RestController +@RequestMapping("api/product") +public class ProductInnnerController { + + @Autowired + CategoryService categoryService; + + @Autowired + SkuInfoService skuInfoService; + + @ApiOperation(value = "根据分类id获取分类信息") + @GetMapping("inner/getCategory/{categoryId}") + public Category getCategory(@PathVariable Long categoryId) { + return categoryService.getById(categoryId); + } + + @ApiOperation(value = "根据skuId获取sku信息") + @GetMapping("inner/getSkuInfo/{skuId}") + public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId) { + return skuInfoService.getById(skuId); + } + +} diff --git a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/controller/SkuInfoController.java b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/controller/SkuInfoController.java index d8fa08d..60b6aa0 100644 --- a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/controller/SkuInfoController.java +++ b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/controller/SkuInfoController.java @@ -83,6 +83,7 @@ public class SkuInfoController { @ApiOperation(value = "删除商品sku信息") @DeleteMapping("remove/{id}") + //TODO 删除es public Result remove(@PathVariable Long id) { try { skuInfoService.removeById(id); @@ -94,6 +95,7 @@ public class SkuInfoController { @ApiOperation(value = "根据id列表删除商品sku信息") @DeleteMapping("batchRemove") + //TODO 删除es public Result batchRemove(@RequestBody List idList) { try { skuInfoService.removeByIds(idList); diff --git a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/SkuInfoService.java b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/SkuInfoService.java index a89b9c3..a561df1 100644 --- a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/SkuInfoService.java +++ b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/SkuInfoService.java @@ -16,7 +16,6 @@ import com.baomidou.mybatisplus.extension.service.IService; * @since 2023-09-15 */ public interface SkuInfoService extends IService { - IPage selectPage(Page pageParam, SkuInfoQueryVo skuInfoQueryVo); void saveSkuInfo(SkuInfoVo skuInfoVo); diff --git a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java b/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java index 8047afa..b6bf8dd 100644 --- a/guigu-ssyx-parent/service/service-product/src/main/java/com/atguigu/ssyx/product/service/impl/SkuInfoServiceImpl.java +++ b/guigu-ssyx-parent/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,6 +43,8 @@ public class SkuInfoServiceImpl extends ServiceImpl impl private SkuImageService skuImagesService; @Autowired private SkuAttrValueService skuAttrValueService; + @Autowired + private RabbitService rabbitService; /** * 获取sku分页列表 @@ -205,12 +209,16 @@ public class SkuInfoServiceImpl extends ServiceImpl impl SkuInfo skuInfo = baseMapper.selectById(skuId); skuInfo.setPublishStatus(status); baseMapper.updateById(skuInfo); - //TODO 商品上架 后续会完善:发送mq消息更新es数据 + + //商品上架:发送mq消息同步es + rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_UPPER, skuId); } else { SkuInfo skuInfo = baseMapper.selectById(skuId); skuInfo.setPublishStatus(status); baseMapper.updateById(skuInfo); - //TODO 商品下架 后续会完善:发送mq消息更新es数据 + + //商品下架:发送mq消息同步es + rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_LOWER, skuId); } } diff --git a/guigu-ssyx-parent/service/service-product/src/main/resources/application-dev.yml b/guigu-ssyx-parent/service/service-product/src/main/resources/application-dev.yml index 4716b29..7ccf036 100644 --- a/guigu-ssyx-parent/service/service-product/src/main/resources/application-dev.yml +++ b/guigu-ssyx-parent/service/service-product/src/main/resources/application-dev.yml @@ -12,6 +12,18 @@ spring: url: jdbc:mysql://82.157.68.223:3306/shequ-product?characterEncoding=utf-8&useSSL=false username: shequ-product password: shequ-product + rabbitmq: + host: 43.143.164.194 + port: 5672 + username: guest + password: guest + publisher-confirm-type: CORRELATED #发布确认模式,消息是否被成功发送到交换机 + publisher-returns: true + listener: + simple: + prefetch: 1 + concurrency: 3 + acknowledge-mode: manual #消费端手动确认 jackson: date-format: yyyy-MM-dd HH:mm:ss diff --git a/guigu-ssyx-parent/service/service-search/pom.xml b/guigu-ssyx-parent/service/service-search/pom.xml new file mode 100644 index 0000000..dbd57a6 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + com.atguigu + service + 1.0-SNAPSHOT + + + service-search + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + com.atguigu + service-product-client + 1.0-SNAPSHOT + compile + + + com.atguigu + rabbit_util + 1.0-SNAPSHOT + compile + + + + + + + + + 8 + 8 + UTF-8 + + + diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/ServiceSearchApplication.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/ServiceSearchApplication.java new file mode 100644 index 0000000..c2a15f3 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/ServiceSearchApplication.java @@ -0,0 +1,24 @@ +package com.atguigu.ssyx; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * ClassName: ServiceSearchApplication + * Package: com.atguigu.ssyx + * + * @author yovinchen + * @Create 2023/9/16 17:20 + */ +@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置 +@EnableDiscoveryClient +@EnableFeignClients +public class ServiceSearchApplication { + public static void main(String[] args) { + SpringApplication.run(ServiceSearchApplication.class, args); + } + +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/controller/SkuApiController.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/controller/SkuApiController.java new file mode 100644 index 0000000..bc4942e --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/controller/SkuApiController.java @@ -0,0 +1,39 @@ +package com.atguigu.ssyx.search.controller; + +import com.atguigu.ssyx.common.result.Result; +import com.atguigu.ssyx.search.service.SkuService; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * ClassName: SkuApiController + * Package: com.atguigu.ssyx.search.controller + * + * @author yovinchen + * @Create 2023/9/16 17:22 + */ +@RestController +@RequestMapping("api/search/sku") +public class SkuApiController { + + @Autowired + private SkuService skuService; + + @ApiOperation(value = "上架商品") + @GetMapping("inner/upperSku/{skuId}") + public Result upperGoods(@PathVariable("skuId") Long skuId) { + skuService.upperSku(skuId); + return Result.ok(null); + } + + @ApiOperation(value = "下架商品") + @GetMapping("inner/lowerSku/{skuId}") + public Result lowerGoods(@PathVariable("skuId") Long skuId) { + skuService.lowerSku(skuId); + return Result.ok(null); + } +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/receiver/SkuReceiver.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/receiver/SkuReceiver.java new file mode 100644 index 0000000..d24e569 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/receiver/SkuReceiver.java @@ -0,0 +1,73 @@ +package com.atguigu.ssyx.search.receiver; + +import com.atguigu.ssyx.mq.constant.MqConst; +import com.atguigu.ssyx.search.service.SkuService; +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.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * ClassName: SkuReceiver + * Package: com.atguigu.ssyx.search.receiver + * + * @author yovinchen + * @Create 2023/9/16 23:57 + */ +@Component +public class SkuReceiver { + + @Autowired + private SkuService skuService; + + /** + * 商品上架 + * + * @param skuId + * @param message + * @param channel + * @throws IOException + */ + @RabbitListener(bindings = @QueueBinding( + value = @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true"), + exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT), + key = {MqConst.ROUTING_GOODS_UPPER} + )) + public void upperSku(Long skuId, Message message, Channel channel) throws IOException { + if (null != skuId) { + skuService.upperSku(skuId); + } + //第一个参数:表示收到的消息的标号 + //第二个参数:如果为true表示可以签收多个消息 + channel.basicAck(message + .getMessageProperties() + .getDeliveryTag(), false); + } + + /** + * 商品下架 + * + * @param skuId + */ + @RabbitListener(bindings = @QueueBinding( + value = @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true"), + exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT), + key = {MqConst.ROUTING_GOODS_LOWER} + )) + public void lowerSku(Long skuId, Message message, Channel channel) throws IOException { + if (null != skuId) { + skuService.lowerSku(skuId); + } + //第一个参数:表示收到的消息的标号 + //第二个参数:如果为true表示可以签收多个消息 + channel.basicAck(message + .getMessageProperties() + .getDeliveryTag(), false); + } +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/repository/SkuRepository.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/repository/SkuRepository.java new file mode 100644 index 0000000..91f2852 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/repository/SkuRepository.java @@ -0,0 +1,14 @@ +package com.atguigu.ssyx.search.repository; + +import com.atguigu.ssyx.model.search.SkuEs; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; + +/** + * ClassName: SkuRepository + * Package: com.atguigu.ssyx.search.repository + * + * @author yovinchen + * @Create 2023/9/16 17:24 + */ +public interface SkuRepository extends ElasticsearchRepository { +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/SkuService.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/SkuService.java new file mode 100644 index 0000000..7053e7a --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/SkuService.java @@ -0,0 +1,29 @@ +package com.atguigu.ssyx.search.service; + +import org.springframework.stereotype.Service; + +/** + * ClassName: SkuService + * Package: com.atguigu.ssyx.search.service + * + * @author yovinchen + * @Create 2023/9/16 17:23 + */ + +@Service +public interface SkuService { + /** + * 上架商品列表 + * + * @param skuId + */ + void upperSku(Long skuId); + + /** + * 下架商品列表 + * + * @param skuId + */ + + void lowerSku(Long skuId); +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/impl/SkuServiceImpl.java b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/impl/SkuServiceImpl.java new file mode 100644 index 0000000..3bdc0af --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/java/com/atguigu/ssyx/search/service/impl/SkuServiceImpl.java @@ -0,0 +1,87 @@ +package com.atguigu.ssyx.search.service.impl; + +import com.alibaba.fastjson.JSON; +import com.atguigu.ssyx.client.product.ProductFeignClient; +import com.atguigu.ssyx.enums.SkuType; +import com.atguigu.ssyx.model.product.Category; +import com.atguigu.ssyx.model.product.SkuInfo; +import com.atguigu.ssyx.model.search.SkuEs; +import com.atguigu.ssyx.search.repository.SkuRepository; +import com.atguigu.ssyx.search.service.SkuService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Objects; + +/** + * ClassName: SkuServiceImpl + * Package: com.atguigu.ssyx.search.service.impl + * + * @author yovinchen + * @Create 2023/9/16 17:23 + */ +@Slf4j +@Service +public class SkuServiceImpl implements SkuService { + + @Autowired + private ProductFeignClient productFeignClient; + + @Autowired + private SkuRepository skuEsRepository; + + /** + * 上架商品列表 + * + * @param skuId + */ + @Override + public void upperSku(Long skuId) { + log.info("upperSku:" + skuId); + //查询sku信息 + SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId); + if (null == skuInfo) return; + + // 查询分类 + SkuEs skuEs = new SkuEs(); + Category category = productFeignClient.getCategory(skuInfo.getCategoryId()); + if (category != null) { + skuEs.setCategoryId(category.getId()); + skuEs.setCategoryName(category.getName()); + } + skuEs.setId(skuInfo.getId()); + skuEs.setKeyword(skuInfo.getSkuName() + "," + skuEs.getCategoryName()); + skuEs.setWareId(skuInfo.getWareId()); + skuEs.setIsNewPerson(skuInfo.getIsNewPerson()); + skuEs.setImgUrl(skuInfo.getImgUrl()); + skuEs.setTitle(skuInfo.getSkuName()); + + + if (Objects.equals(skuInfo.getSkuType(), SkuType.COMMON.getCode())) { + skuEs.setSkuType(0); + skuEs.setPrice(skuInfo + .getPrice() + .doubleValue()); + skuEs.setStock(skuInfo.getStock()); + skuEs.setSale(skuInfo.getSale()); + skuEs.setPerLimit(skuInfo.getPerLimit()); + } else { + //TODO 待完善-秒杀商品 + + } + SkuEs save = skuEsRepository.save(skuEs); + log.info("upperSku:" + JSON.toJSONString(save)); + } + + /** + * a下架商品列表 + * + * @param skuId + */ + + @Override + public void lowerSku(Long skuId) { + skuEsRepository.deleteById(skuId); + } +} diff --git a/guigu-ssyx-parent/service/service-search/src/main/resources/application-dev.yml b/guigu-ssyx-parent/service/service-search/src/main/resources/application-dev.yml new file mode 100644 index 0000000..632ef57 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/resources/application-dev.yml @@ -0,0 +1,41 @@ +server: + port: 8204 +feign: + sentinel: + enabled: true + client: + config: + default: #配置全局的feign的调用超时时间 如果 有指定的服务配置 默认的配置不会生效 + connectTimeout: 30000 # 指定的是 消费者 连接服务提供者的连接超时时间 是否能连接 单位是毫秒 + readTimeout: 50000 # 指定的是调用服务提供者的 服务 的超时时间() 单位是毫秒 +spring: + main: + allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册 + elasticsearch: + rest: + uris: http://43.143.164.194:9200 + rabbitmq: + host: 43.143.164.194 + port: 5672 + username: guest + password: guest + publisher-confirm-type: CORRELATED #发布确认模式,消息是否被成功发送到交换机 + publisher-returns: true + listener: + simple: + prefetch: 1 + concurrency: 3 + acknowledge-mode: manual #消费端手动确认 + + redis: + host: localhost + port: 6379 + database: 0 + timeout: 1800000 + password: + lettuce: + pool: + max-active: 20 #最大连接数 + max-wait: -1 #最大阻塞等待时间(负数表示没限制) + max-idle: 5 #最大空闲 + min-idle: 0 #最小空闲 diff --git a/guigu-ssyx-parent/service/service-search/src/main/resources/application.yml b/guigu-ssyx-parent/service/service-search/src/main/resources/application.yml new file mode 100644 index 0000000..e5f6453 --- /dev/null +++ b/guigu-ssyx-parent/service/service-search/src/main/resources/application.yml @@ -0,0 +1,11 @@ +spring: + application: + name: service-search + profiles: + active: dev + cloud: + nacos: + discovery: + server-addr: localhost:8848 + username: nacos + password: nacos