✅P210_商城业务-商品详情-异步编排优化

gong_yz大约 1 分钟谷粒商城

编写业务线程池配置类

1、导入编写配置文件依赖

cfmall-product/pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2、编写配置类

cfmall-product/src/main/java/com/gyz/cfmall/product/config/ThreadPoolConfigProperties.java

@Data
@Component
@ConfigurationProperties(prefix = "cfmall.thread")
public class ThreadPoolConfigProperties {
    private Integer coreSize;

    private Integer maxSize;

    private Integer keepAliveTime;
}

3、配置配置文件

cfmall-product/src/main/resources/application.properties

#配置线程池
cfmall.thread.coreSize=20
cfmall.thread.maxSize=200
cfmall.thread.keepAliveTime=10

4、编写线程池

cfmall-product/src/main/java/com/gyz/cfmall/product/config/MyThreadPoolConfig.java

@Configuration
public class MyThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(
                pool.getCoreSize(),
                pool.getMaxSize(),
                pool.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new BlockingArrayQueue<>(100000),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}

异步编排

3、4、5操作需要1操作完成之后才能执行

异步编排后的逻辑

/**
 * com.gyz.cfmall.product.service.impl.SkuInfoServiceImpl#item
 */

@Resource
private SkuImagesService imagesService;
@Resource
private SpuInfoDescService descService;
@Resource
private AttrGroupService attrGroupService;
@Resource
private SkuSaleAttrValueService skuSaleAttrValueService;
@Resource
private ThreadPoolExecutor executor;

@Override
public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
    SkuItemVo skuItemVo = new SkuItemVo();
    
    CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
        //1、获取sku的基本信息,数据来自于pms_sku_info表
        SkuInfoEntity skuInfoEntity = this.baseMapper.selectOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        skuItemVo.setSkuInfo(skuInfoEntity);
        return skuInfoEntity;
    }, executor);
    
    CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
        //3、spu的介绍,数据来源于pms_spu_info_desc表
        Long spuId = res.getSpuId();
        SpuInfoDescEntity descEntity = descService.getById(spuId);
        skuItemVo.setDesc(descEntity);
    }, executor);
    
    CompletableFuture<Void> groupAttrFuture = infoFuture.thenAcceptAsync((res) -> {
        //4、spu的规格参数
        Long catalogId = res.getCatalogId();
        List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuIdAndCatalogId(res.getSpuId(), catalogId);
        skuItemVo.setGroupAttrs(attrGroupVos);
    }, executor);
    
    CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res) -> {
        //5、获取spu的销售属性组合
        List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
        skuItemVo.setSaleAttr(saleAttrVos);
    }, executor);
    
    CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
        //2、sku的图片信息,数据来自于 pms_sku_images表
        List<SkuImagesEntity> imagesList = imagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
        skuItemVo.setImages(imagesList);
    }, executor);
    
    // 等待所有任务完成返回结果
    CompletableFuture.allOf(descFuture, groupAttrFuture, saleFuture, imgFuture).get();
    
    return skuItemVo;
}