Commit 2def4d75 by fanjiaxin

联调问题处理

parent 3cc33c49
Pipeline #71642 passed with stages
in 1 minute 12 seconds
package com.netease.yanxuan.wx.store.sharer.biz.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线程池配置类
*
* @author fanjiaxin
* @date 2024/6/12 15:55
*/
@Slf4j
@Configuration
@EnableAsync
public class ExecutorConfig {
/**
* 异步线程池
*/
@Bean(name = "asyncProductListServiceExecutor")
public Executor asyncProductListServiceExecutor() {
log.info("start asyncProductListServiceExecutor...");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//配置核心线程数
executor.setCorePoolSize(10);
//配置最大线程数
executor.setMaxPoolSize(20);
//配置队列大小
executor.setQueueCapacity(500);
//线程空闲时间
executor.setKeepAliveSeconds(30);
//配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("async-product-list-service-executor-");
/**
* rejection-policy:当pool已经达到max size的时候,如何处理新任务
* CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
*/
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//等待所有任务完成再关闭
executor.setWaitForTasksToCompleteOnShutdown(true);
//最长等待时间,秒
executor.setAwaitTerminationSeconds(60);
//初始化执行器
executor.initialize();
return executor;
}
}
package com.netease.yanxuan.wx.store.sharer.biz.service.impl;
import com.alibaba.fastjson.JSON;
import com.netease.yanxuan.wx.store.sharer.biz.config.DrmSharerConfig;
import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserContextHolder;
import com.netease.yanxuan.wx.store.sharer.biz.core.LoginUserInfo;
......@@ -8,7 +9,9 @@ import com.netease.yanxuan.wx.store.sharer.biz.meta.model.vo.ProductPromotionLin
import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageQuery;
import com.netease.yanxuan.wx.store.sharer.biz.meta.page.PageVO;
import com.netease.yanxuan.wx.store.sharer.biz.service.IProductService;
import com.netease.yanxuan.wx.store.sharer.common.constant.CoreConstant;
import com.netease.yanxuan.wx.store.sharer.common.exception.NoBindSharerException;
import com.netease.yanxuan.wx.store.sharer.common.handler.RedisClient;
import com.netease.yanxuan.wx.store.sharer.common.util.AsyncUtils;
import com.netease.yanxuan.wx.store.sharer.integration.constant.WeChatApi;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopPromoteProductDetailRequest;
......@@ -29,7 +32,6 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.concurrent.Executor;
/**
* @Description 商品-业务实现类
......@@ -45,7 +47,7 @@ public class ProductServiceImpl implements IProductService {
private final WeChatShopPromoteProductLinkRequest weChatShopPromoteProductLinkRequest;
private final WeChatShopSharerListRequest weChatShopSharerListRequest;
private final DrmSharerConfig drmSharerConfig;
private final Executor asyncProductListServiceExecutor;
private final RedisClient redisClient;
@Override
......@@ -58,11 +60,7 @@ public class ProductServiceImpl implements IProductService {
if (!CollectionUtils.isEmpty(productListVO.getProduct_list())) {
// 使用异步多线程处理
List<ProductListVO> productList = AsyncUtils.processListAsync(productListVO.getProduct_list(), item -> {
WeChatPromoteProductDetailVO detailVO = weChatShopPromoteProductDetailRequest.handle(item.getShop_appid(),
Long.valueOf(item.getProduct_id()));
if (null == detailVO) {
return null;
}
WeChatPromoteProductDetailVO detailVO = getProductDetailCache(item.getShop_appid(), item.getProduct_id());
WeChatPromoteProductDetailVO.WeChatPromoteProductVO product = detailVO.getProduct();
if (null == product) {
return null;
......@@ -122,7 +120,7 @@ public class ProductServiceImpl implements IProductService {
listVO.setProductId(item.getProduct_id());
listVO.setProductPromotionLink(productInfo.getProduct_promotion_link());
return listVO;
}, asyncProductListServiceExecutor);
});
// 过滤空
productList = Optional.of(productList).orElseGet(ArrayList::new)
.stream()
......@@ -154,4 +152,22 @@ public class ProductServiceImpl implements IProductService {
}
return null;
}
/**
* 获取商品详情,并缓存到redis中
*/
private WeChatPromoteProductDetailVO getProductDetailCache(String shopAppid, String productId) {
WeChatPromoteProductDetailVO detailVO;
String productDetailKey = CoreConstant.REDIS_PRODUCT_DETAIL_KEY + shopAppid + "&" + productId;
String productDetailJson = redisClient.getStr(productDetailKey);
if (StringUtils.isBlank(productDetailJson)) {
detailVO = weChatShopPromoteProductDetailRequest.handle(shopAppid, Long.valueOf(productId));
if (null != detailVO) {
redisClient.setStr(productDetailKey, JSON.toJSONString(detailVO), CoreConstant.REDIS_PRODUCT_DETAIL_EXPIRE_SECONDS);
}
} else {
detailVO = JSON.parseObject(productDetailJson, WeChatPromoteProductDetailVO.class);
}
return detailVO;
}
}
......@@ -56,7 +56,12 @@ public interface CoreConstant {
int REDIS_TOKEN_WECHAT_SHOP_LOCK_TIME = 5;
/**
* Redis商品详情ID缓存KEY
* Redis商品详情缓存KEY
*/
String REDIS_PRODUCT_DETAIL_ID_KEY = "PRODUCT:DETAIL:ID:KEY";
String REDIS_PRODUCT_DETAIL_KEY = "PRODUCT:DETAIL:";
/**
* Redis商品详情过期时间,秒
*/
int REDIS_PRODUCT_DETAIL_EXPIRE_SECONDS = 6 * 60 * 60;
}
......@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment