Skip to content

Commit

Permalink
迁移搜索服务
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Aug 3, 2020
1 parent 72ed490 commit 128b9dc
Show file tree
Hide file tree
Showing 57 changed files with 366 additions and 871 deletions.
8 changes: 8 additions & 0 deletions mall-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<!-- <version>2.7.8</version>-->
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
Expand Down Expand Up @@ -247,6 +248,13 @@
<version>${xxl-job.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.nacos</groupId>-->
<!-- <artifactId>nacos-client</artifactId>-->
<!-- <version>1.3.1</version>-->
<!-- </dependency>-->

<!-- Transaction 相关 -->
<dependency>
<groupId>io.seata</groupId>
Expand Down
13 changes: 13 additions & 0 deletions management-web-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,21 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>nacos-client</artifactId>-->
<!-- <groupId>com.alibaba.nacos</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>

<!-- TODO 芋艿 解决 1.3.1 beta 的问题 -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.nacos</groupId>-->
<!-- <artifactId>nacos-client</artifactId>-->
<!-- <version>1.3.1</version>-->
<!-- </dependency>-->

<!-- 工具类相关 -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
3 changes: 2 additions & 1 deletion management-web-app/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ spring:
dubbo:
# Dubbo 注册中心
registry:
address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
# address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
address: nacos://400-infra.server.iocoder.cn:8848?namespace=dev # 指定 Dubbo 服务注册中心的地址
3 changes: 1 addition & 2 deletions management-web-app/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ spring:
dubbo:
# Dubbo 注册中心
registry:
# address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
# address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
address: nacos://400-infra.server.iocoder.cn:8848?namespace=dev # 指定 Dubbo 服务注册中心的地址

Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public interface ProductSpuRpc {
/**
* 顺序获得商品 SPU 编号数组
*
* @param limit 数量
* @param lastSpuId 最后一个商品 SPU 编号
* @param limit 数量
* @return 商品 SPU 编号数组
*/
CommonResult<List<Integer>> listProductSpuIds(Integer limit, Integer lastSpuId);
CommonResult<List<Integer>> listProductSpuIds(Integer lastSpuId, Integer limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductSpuMapper extends BaseMapper<ProductSpuDO> {

Expand All @@ -25,4 +27,9 @@ default IPage<ProductSpuDO> selectPage(ProductSpuPageBO pageBO) {
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()), query);
}

default List<ProductSpuDO> selectListByIdGt(Integer id, Integer limit) {
return selectList(new QueryWrapperX<ProductSpuDO>().gtIfPresent("id", id)
.orderByAsc("id").last("LIMIT " + limit));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ private ProductCategoryBO checkProductCategory(Integer cid) {
/**
* 顺序获得商品 SPU 编号数组
*
* @param limit 数量
* @param lastSpuId 最后一个商品 SPU 编号
* @param limit 数量
* @return 商品 SPU 编号数组
*/
public List<Integer> listProductSpuIds(Integer limit, Integer lastSpuId) {
return productAttrService.listProductSpuIds(limit, lastSpuId);
public List<Integer> listProductSpuIds(Integer lastSpuId, Integer limit) {
return productSpuService.listProductSpuIds(lastSpuId, limit);
}

private List<ProductAttrKeyValueBO> checkProductAttr(List<ProductSkuCreateOrUpdateBO> skuBOs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public CommonResult<PageResult<ProductSpuRespDTO>> pageProductSpu(ProductSpuPage
}

@Override
public CommonResult<List<Integer>> listProductSpuIds(Integer limit, Integer lastSpuId) {
return success(productSpuManager.listProductSpuIds(limit, lastSpuId));
public CommonResult<List<Integer>> listProductSpuIds(Integer lastSpuId, Integer limit) {
return success(productSpuManager.listProductSpuIds(lastSpuId, limit));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,4 @@ public List<ProductAttrValueBO> listProductAttrValues(ProductAttrValueListQueryB
return ProductAttrConvert.INSTANCE.convertList03(productAttrValueDOs);
}

/**
* 顺序获得商品 SPU 编号数组
*
* 一般情况下,该接口我们用于提供顺序的 SPU 编号数组,以便调用方进一步根据自己需要获取商品信息
* 例如说,搜索服务会不断获取商品编号,重建该商品编号的索引
*
* @param limit 数量
* @param lastSpuId 最后一个商品 SPU 编号
* @return 商品 SPU 编号数组
*/
public List<Integer> listProductSpuIds(Integer limit, Integer lastSpuId) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.mall.productservice.service.spu;

import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.productservice.convert.spu.ProductSpuConvert;
import cn.iocoder.mall.productservice.dal.mysql.dataobject.spu.ProductSpuDO;
Expand Down Expand Up @@ -91,4 +92,18 @@ public PageResult<ProductSpuBO> pageProductSpu(ProductSpuPageBO pageBO) {
return ProductSpuConvert.INSTANCE.convertPage(productSpuDOPage);
}

/**
* 顺序获得商品 SPU 编号数组
*
* 一般情况下,该接口我们用于提供顺序的 SPU 编号数组,以便调用方进一步根据自己需要获取商品信息
* 例如说,搜索服务会不断获取商品编号,重建该商品编号的索引
*
* @param lastSpuId 最后一个商品 SPU 编号
* @param limit 数量
* @return 商品 SPU 编号数组
*/
public List<Integer> listProductSpuIds(Integer lastSpuId, Integer limit) {
return CollectionUtils.convertList(productSpuMapper.selectListByIdGt(lastSpuId, limit), ProductSpuDO::getId);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/**
* 占位,避免包折叠
*/
package cn.iocoder.mall.searchservice.enums;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.iocoder.mall.searchservice.enums.product;

/**
* 搜索商品条件的字段枚举
*/
public enum SearchProductConditionFieldEnum {

CATEGORY("category");

/**
* 字段
*/
private final String field;

SearchProductConditionFieldEnum(String field) {
this.field = field;
}

}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/**
* 占位,避免包折叠
*/
package cn.iocoder.mall.searchservice.rpc;
7 changes: 7 additions & 0 deletions search-service-project/search-service-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!-- Test 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- 工具类相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.mapstruct.factory.Mappers;
import org.springframework.data.domain.Page;

import java.util.List;

@Mapper
public interface SearchProductConvert {

Expand All @@ -30,8 +32,11 @@ public interface SearchProductConvert {

ESProductDO convert(SearchProductSaveBO bean);

@Mapping(source = "content", target = "list")
@Mapping(source = "getTotalElements", target = "total")
PageResult<SearchProductBO> convert(Page<ESProductDO> searchPage);
List<SearchProductBO> convertList(List<ESProductDO> list);

default PageResult<SearchProductBO> convertPage(Page<ESProductDO> searchPage) {
return new PageResult<SearchProductBO>().setList(convertList(searchPage.getContent()))
.setTotal(searchPage.getTotalElements());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
@Slf4j
public class SearchProductManager {

private static final Integer REBUILD_FETCH_PER_SIZE = 100;

@DubboReference(version = "${dubbo.consumer.ProductSpuRpc.version}")
private ProductSpuRpc productSpuRpc;
@DubboReference(version = "${dubbo.consumer.ProductSkuRpc.version}")
Expand All @@ -37,6 +39,40 @@ public class SearchProductManager {
@Autowired
private SearchProductService searchProductService;

/**
* 重建所有商品的 ES 索引
*
* @return 重建数量
*/
public Integer rebuild() {
// TODO 芋艿,因为目前商品比较少,所以写的很粗暴。等未来重构
Integer lastId = null;
int rebuildCounts = 0;
while (true) {
// 从商品服务,增量获取商品列表编号
CommonResult<List<Integer>> listProductSpuIdsResult = productSpuRpc.listProductSpuIds(lastId, REBUILD_FETCH_PER_SIZE);
listProductSpuIdsResult.checkError();
List<Integer> spuIds = listProductSpuIdsResult.getData();
// 逐个重建索引到 ES 中
spuIds.forEach(this::saveProduct);
// 设置新的 lastId ,或者结束
rebuildCounts += listProductSpuIdsResult.getData().size();
if (spuIds.size() < REBUILD_FETCH_PER_SIZE) {
break;
} else {
lastId = spuIds.get(spuIds.size() - 1);
}
}
// 返回成功
return rebuildCounts;
}

/**
* 重建指定商品的 ES 索引
*
* @param id 商品 SPU 编号
* @return 是否重建成功
*/
public Boolean saveProduct(Integer id) {
// 获得商品 SPU
CommonResult<ProductSpuRespDTO> productSpuResult = productSpuRpc.getProductSpu(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
* 商品更新 Topic 的消费者,重建对应的商品的 ES 索引
*/
@Service
@RocketMQMessageListener(
topic = ProductUpdateMessage.TOPIC,
consumerGroup = "${spring.application.name}-consumer-group-" + ProductUpdateMessage.TOPIC
)
public class PayTransactionPaySuccessConsumer implements RocketMQListener<ProductUpdateMessage> {
public class ProductUpdateConsumer implements RocketMQListener<ProductUpdateMessage> {

@Autowired
private SearchProductManager productSearchManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* 占位
* 占位,避免包折叠
*/
package cn.iocoder.mall.searchservice.service;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.enums.product.SearchProductConditionFieldEnum;
import cn.iocoder.mall.searchservice.enums.product.SearchProductPageQuerySortFieldEnum;
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductBO;
import cn.iocoder.mall.searchservice.service.product.bo.SearchProductConditionBO;
Expand Down Expand Up @@ -47,7 +48,7 @@ public PageResult<SearchProductBO> getSearchPage(SearchProductPageQueryBO pageQu
Page<ESProductDO> searchPage = productRepository.search(pageQueryBO.getCid(), pageQueryBO.getKeyword(),
pageQueryBO.getPageNo(), pageQueryBO.getPageSize(), pageQueryBO.getSorts());
// 转换结果
return SearchProductConvert.INSTANCE.convert(searchPage);
return SearchProductConvert.INSTANCE.convertPage(searchPage);
}

private void checkSortFieldInvalid(List<SortingField> sorts) {
Expand All @@ -74,11 +75,11 @@ public void saveSearchProduct(SearchProductSaveBO saveBO) {
* 在我们搜索商品时,需要获得关键字可选择的分类、品牌等等搜索条件,方便用户进一步检索
*
* @param keyword 关键字
* @param fields 需要返回的搜索条件。目前可传入的参数为
* @param fields 需要返回的搜索条件{@link SearchProductConditionFieldEnum}。目前可传入的参数为
* 1. category :商品分类,会返回商品分类编号
* @return 搜索条件
*/
public SearchProductConditionBO getSearchCondition(String keyword, Collection<String> fields) {
public SearchProductConditionBO getSearchProductCondition(String keyword, Collection<String> fields) {
// 创建 ES 搜索条件
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// 筛选
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ dubbo:
# Dubbo 服务提供者的配置
provider:
tag: ${DUBBO_TAG} # Dubbo 路由分组
# Dubbo 服务消费者的配置
consumer:
tag: ${DUBBO_TAG} # Dubbo 路由分组
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* 占位
*/
package cn.iocoder.mall.searchservice.manager;
Loading

0 comments on commit 128b9dc

Please sign in to comment.