整合es、rabbitmq、redis完成上下架修改es

This commit is contained in:
2023-09-17 00:20:42 +08:00
parent ca82fbe892
commit 3019bc5737
26 changed files with 770 additions and 3 deletions

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-search</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-product-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>rabbit_util</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.atguigu</groupId>-->
<!-- <artifactId>service-product-api</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- </dependency>-->
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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<SkuEs, Long> {
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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 #最小空闲

View File

@@ -0,0 +1,11 @@
spring:
application:
name: service-search
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: localhost:8848
username: nacos
password: nacos