parent
981b3d87e2
commit
0252bd0f0b
@ -0,0 +1 @@
|
||||
package cn.iocoder.mall.searchservice.rpc;
|
||||
@ -0,0 +1,7 @@
|
||||
package cn.iocoder.mall.searchservice.rpc.product;
|
||||
|
||||
/**
|
||||
* 商品搜索 RPC 接口
|
||||
*/
|
||||
public interface SearchProductRpc {
|
||||
}
|
||||
@ -1,4 +1,15 @@
|
||||
package cn.iocoder.mall.searchservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SearchServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 解决 ES java.lang.IllegalStateException: availableProcessors is already
|
||||
System.setProperty("es.set.netty.runtime.available.processors", "false");
|
||||
SpringApplication.run(SearchServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
package cn.iocoder.mall.searchservice.convert;
|
||||
@ -0,0 +1,30 @@
|
||||
package cn.iocoder.mall.searchservice.convert.product;
|
||||
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryRespDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO;
|
||||
import cn.iocoder.mall.searchservice.dal.es.dataobject.ESProductDO;
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductCreateBO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface SearchProductConvert {
|
||||
|
||||
SearchProductConvert INSTANCE = Mappers.getMapper(SearchProductConvert.class);
|
||||
|
||||
|
||||
@Mapping(source = "spu.id", target = "id")
|
||||
@Mapping(source = "spu.name", target = "name")
|
||||
@Mapping(source = "spu.sellPoint", target = "sellPoint")
|
||||
@Mapping(source = "spu.description", target = "description")
|
||||
@Mapping(source = "spu.cid", target = "cid")
|
||||
@Mapping(source = "category.name", target = "categoryName")
|
||||
@Mapping(source = "spu.picUrls", target = "picUrls")
|
||||
@Mapping(source = "spu.visible", target = "visible")
|
||||
@Mapping(source = "spu.sort", target = "sort")
|
||||
SearchProductCreateBO convert(ProductSpuRespDTO spu, ProductCategoryRespDTO category);
|
||||
|
||||
ESProductDO convert(SearchProductCreateBO bean);
|
||||
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
package cn.iocoder.mall.searchservice.manager.product;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.productservice.rpc.category.ProductCategoryRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.ProductSkuRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuListQueryReqDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuRespDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.spu.ProductSpuRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ProductSearchManager {
|
||||
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSpuRpc.version}")
|
||||
private ProductSpuRpc productSpuRpc;
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSkuRpc.version}")
|
||||
private ProductSkuRpc productSkuRpc;
|
||||
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
|
||||
private ProductCategoryRpc productCategoryRpc;
|
||||
|
||||
// @DubboReference( version = "${dubbo.consumer.CartService.version}")
|
||||
// private CartService cartService;
|
||||
|
||||
public Boolean saveProduct(Integer id) {
|
||||
// 获得商品 SPU
|
||||
CommonResult<ProductSpuRespDTO> productSpuResult = productSpuRpc.getProductSpu(id);
|
||||
productSpuResult.checkError();
|
||||
// 获得商品 SKU
|
||||
CommonResult<List<ProductSkuRespDTO>> listProductSkusResult =
|
||||
productSkuRpc.listProductSkus(new ProductSkuListQueryReqDTO().setProductSpuId(id));
|
||||
listProductSkusResult.checkError();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
package cn.iocoder.mall.searchservice.manager.product;
|
||||
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.productservice.rpc.category.ProductCategoryRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.category.dto.ProductCategoryRespDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.ProductSkuRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuListQueryReqDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuRespDTO;
|
||||
import cn.iocoder.mall.productservice.rpc.spu.ProductSpuRpc;
|
||||
import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO;
|
||||
import cn.iocoder.mall.searchservice.convert.product.SearchProductConvert;
|
||||
import cn.iocoder.mall.searchservice.service.product.SearchProductService;
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductCreateBO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SearchProductManager {
|
||||
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSpuRpc.version}")
|
||||
private ProductSpuRpc productSpuRpc;
|
||||
@DubboReference(version = "${dubbo.consumer.ProductSkuRpc.version}")
|
||||
private ProductSkuRpc productSkuRpc;
|
||||
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
|
||||
private ProductCategoryRpc productCategoryRpc;
|
||||
|
||||
// @DubboReference( version = "${dubbo.consumer.CartService.version}")
|
||||
// private CartService cartService;
|
||||
|
||||
@Autowired
|
||||
private SearchProductService searchProductService;
|
||||
|
||||
public Boolean saveProduct(Integer id) {
|
||||
// 获得商品 SPU
|
||||
CommonResult<ProductSpuRespDTO> productSpuResult = productSpuRpc.getProductSpu(id);
|
||||
productSpuResult.checkError();
|
||||
if (productSpuResult.getData() == null) {
|
||||
log.error("[saveProduct][商品 SPU({}) 不存在]", id);
|
||||
return false;
|
||||
}
|
||||
// 获得商品 SKU
|
||||
CommonResult<List<ProductSkuRespDTO>> listProductSkusResult =
|
||||
productSkuRpc.listProductSkus(new ProductSkuListQueryReqDTO().setProductSpuId(id));
|
||||
listProductSkusResult.checkError();
|
||||
if (CollectionUtils.isEmpty(listProductSkusResult.getData())) {
|
||||
log.error("[saveProduct][商品 SPU({}) 的 SKU 不存在]", id);
|
||||
return false;
|
||||
}
|
||||
// 获得商品分类
|
||||
CommonResult<ProductCategoryRespDTO> getProductCategoryResult =
|
||||
productCategoryRpc.getProductCategory(productSpuResult.getData().getCid());
|
||||
getProductCategoryResult.checkError();
|
||||
if (getProductCategoryResult.getData() == null) {
|
||||
log.error("[saveProduct][商品 SPU({}) 的分类({}) 不存在]", id, productSpuResult.getData().getCid());
|
||||
return false;
|
||||
}
|
||||
// 保存商品到 ES 中
|
||||
SearchProductCreateBO searchProductCreateBO = SearchProductConvert.INSTANCE.convert(
|
||||
productSpuResult.getData(), getProductCategoryResult.getData());
|
||||
ProductSkuRespDTO productSku = listProductSkusResult.getData().stream()
|
||||
.min(Comparator.comparing(ProductSkuRespDTO::getPrice)).orElse(null);
|
||||
assert productSku != null;
|
||||
// // 价格计算 TODO 芋艿:需要补充,暂时使用这个逻辑
|
||||
// CalcSkuPriceBO calSkuPriceResult = cartService.calcSkuPrice(sku.getId());
|
||||
searchProductCreateBO.setOriginalPrice(productSku.getPrice());
|
||||
searchProductCreateBO.setBuyPrice(productSku.getPrice());
|
||||
searchProductCreateBO.setQuantity(productSku.getQuantity());
|
||||
searchProductService.createSearchProduct(searchProductCreateBO);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package cn.iocoder.mall.searchservice.mq.consumer;
|
||||
|
||||
import cn.iocoder.mall.searchservice.manager.product.SearchProductManager;
|
||||
import cn.iocoder.mall.searchservice.mq.consumer.message.ProductUpdateMessage;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Service
|
||||
@RocketMQMessageListener(
|
||||
topic = ProductUpdateMessage.TOPIC,
|
||||
consumerGroup = "${spring.application.name}-consumer-group-" + ProductUpdateMessage.TOPIC
|
||||
)
|
||||
public class PayTransactionPaySuccessConsumer implements RocketMQListener<ProductUpdateMessage> {
|
||||
|
||||
@Autowired
|
||||
private SearchProductManager productSearchManager;
|
||||
|
||||
@Override
|
||||
public void onMessage(ProductUpdateMessage message) {
|
||||
Boolean result = productSearchManager.saveProduct(message.getId());
|
||||
Assert.isTrue(result, String.format("重构商品(%d)的 ES 索引,必然成功。实际结果是 %s", message.getId(), result));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
package cn.iocoder.mall.searchservice.rpc;
|
||||
@ -0,0 +1,7 @@
|
||||
package cn.iocoder.mall.searchservice.rpc.product;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
||||
@DubboService
|
||||
public class SearchProductRpcImpl implements SearchProductRpc {
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
package cn.iocoder.mall.searchservice.service.product;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ProductSearchService {
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.mall.searchservice.service.product;
|
||||
|
||||
import cn.iocoder.mall.searchservice.convert.product.SearchProductConvert;
|
||||
import cn.iocoder.mall.searchservice.dal.es.dataobject.ESProductDO;
|
||||
import cn.iocoder.mall.searchservice.dal.es.repository.ESProductRepository;
|
||||
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductCreateBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SearchProductService {
|
||||
|
||||
@Autowired
|
||||
private ESProductRepository productRepository;
|
||||
|
||||
public void createSearchProduct(SearchProductCreateBO createBO) {
|
||||
ESProductDO productDO = SearchProductConvert.INSTANCE.convert(createBO);
|
||||
productRepository.save(productDO);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>search</artifactId>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>search-application</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- Mall 相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<artifactId>search-rest</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<artifactId>search-rpc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,26 +0,0 @@
|
||||
package cn.iocoder.mall.search.application;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.config.ConfigFileApplicationListener;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.search"})
|
||||
@EnableAsync(proxyTargetClass = true)
|
||||
public class SearchApplication {
|
||||
/**
|
||||
* 设置需要读取的配置文件的名字。
|
||||
* 基于 {@link org.springframework.boot.context.config.ConfigFileApplicationListener#CONFIG_NAME_PROPERTY} 实现。
|
||||
*/
|
||||
private static final String CONFIG_NAME_VALUE = "biz,rest,rpc,application";
|
||||
public static void main(String[] args) {
|
||||
|
||||
// 设置环境变量
|
||||
System.setProperty(ConfigFileApplicationListener.CONFIG_NAME_PROPERTY, CONFIG_NAME_VALUE);
|
||||
|
||||
// 解决 ES java.lang.IllegalStateException: availableProcessors is already
|
||||
System.setProperty("es.set.netty.runtime.available.processors", "false");
|
||||
SpringApplication.run(SearchApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
swagger:
|
||||
enable: true
|
||||
title: 搜索子系统
|
||||
description: 搜索子系统
|
||||
version: 1.0.0
|
||||
base-package: cn.iocoder.mall.search.application.controller
|
||||
@ -1,6 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: search-application
|
||||
# Profile 的配置项
|
||||
profiles:
|
||||
active: local
|
||||
@ -1,27 +0,0 @@
|
||||
package cn.iocoder.mall.search.biz.mq;
|
||||
|
||||
import cn.iocoder.mall.product.api.message.ProductUpdateMessage;
|
||||
import cn.iocoder.mall.search.biz.ProductSearchService;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Service
|
||||
@RocketMQMessageListener(
|
||||
topic = ProductUpdateMessage.TOPIC,
|
||||
consumerGroup = "search-consumer-group-" + ProductUpdateMessage.TOPIC
|
||||
)
|
||||
public class PayTransactionPaySuccessConsumer implements RocketMQListener<ProductUpdateMessage> {
|
||||
|
||||
@Autowired
|
||||
private ProductSearchService productSearchService;
|
||||
|
||||
@Override
|
||||
public void onMessage(ProductUpdateMessage message) {
|
||||
Boolean result = productSearchService.save(message.getId());
|
||||
Assert.isTrue(result, String.format("重构商品 ES 索引,必然成功。实际结果是 %s", result));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue