修改多线程优化查询

测试结果为平均100ms
This commit is contained in:
yovinchen 2024-04-01 23:50:02 +08:00
parent 4e0bc6d3c2
commit dc88a39cef

View File

@ -8,12 +8,13 @@ import com.yovinchen.xlcs.model.product.Category;
import com.yovinchen.xlcs.model.product.SkuInfo; import com.yovinchen.xlcs.model.product.SkuInfo;
import com.yovinchen.xlcs.model.search.SkuEs; import com.yovinchen.xlcs.model.search.SkuEs;
import com.yovinchen.xlcs.vo.user.LeaderAddressVo; import com.yovinchen.xlcs.vo.user.LeaderAddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*;
/** /**
* ClassName: HomeServiceImpl * ClassName: HomeServiceImpl
@ -23,6 +24,7 @@ import java.util.Map;
* @since 2023/9/25 12:12 * @since 2023/9/25 12:12
*/ */
@Service @Service
@Slf4j
public class HomeServiceImpl implements HomeService { public class HomeServiceImpl implements HomeService {
@ -43,30 +45,105 @@ public class HomeServiceImpl implements HomeService {
*/ */
@Override @Override
public Map<String, Object> homeData(Long userId) { public Map<String, Object> homeData(Long userId) {
Map<String, Object> result = new ConcurrentHashMap<>();
Map<String, Object> result = new HashMap<>(); ExecutorService executorService = Executors.newFixedThreadPool(4);
//1 根据userId获取当前登录用户提货地址信息 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
// 远程调用service-user模块接口获取需要数据
// 1 根据userId获取当前登录用户地址信息
CompletableFuture<Void> getAddressFuture = CompletableFuture.runAsync(() -> {
try {
LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId); LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId);
if (leaderAddressVo != null) {
result.put("leaderAddressVo", leaderAddressVo); result.put("leaderAddressVo", leaderAddressVo);
}
} catch (Exception e) {
log.error("获取地址信息任务出错: {}", e.getMessage(), e);
}
}, executorService);
// 设置获取地址信息任务的超时
scheduler.schedule(() -> {
if (!getAddressFuture.isDone()) {
getAddressFuture.complete(null);
log.info("获取地址信息任务超时");
}
}, 1, TimeUnit.SECONDS);
// 2 获取所有分类 // 2 获取所有分类
// 远程调用service-product模块接口 CompletableFuture<Void> getCategoryFuture = CompletableFuture.runAsync(() -> {
try {
List<Category> categoryList = productFeignClient.findAllCategoryList(); List<Category> categoryList = productFeignClient.findAllCategoryList();
if (categoryList != null) {
result.put("categoryList", categoryList); result.put("categoryList", categoryList);
}
} catch (Exception e) {
log.error("获取所有分类任务出错: {}", e.getMessage(), e);
}
}, executorService);
// 设置获取所有分类任务的超时
scheduler.schedule(() -> {
if (!getCategoryFuture.isDone()) {
getCategoryFuture.complete(null);
log.info("获取所有分类任务超时");
}
}, 1, TimeUnit.SECONDS);
// 3 获取新人专享商品 // 3 获取新人专享商品
// 远程调用service-product模块接口 CompletableFuture<Void> getNewPersonSkuInfoFuture = CompletableFuture.runAsync(() -> {
try {
List<SkuInfo> newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList(); List<SkuInfo> newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList();
if (newPersonSkuInfoList != null) {
result.put("newPersonSkuInfoList", newPersonSkuInfoList); result.put("newPersonSkuInfoList", newPersonSkuInfoList);
}
} catch (Exception e) {
log.error("获取新人专享商品任务出错: {}", e.getMessage(), e);
}
}, executorService);
// 设置获取新人专享商品任务的超时
scheduler.schedule(() -> {
if (!getNewPersonSkuInfoFuture.isDone()) {
getNewPersonSkuInfoFuture.complete(null);
log.info("获取新人专享商品任务超时");
}
}, 1, TimeUnit.SECONDS);
// 4 获取爆款商品 // 4 获取爆款商品
// 远程调用service-search模块接口 CompletableFuture<Void> getHotSkuFuture = CompletableFuture.runAsync(() -> {
// hotscore 热门评分降序排序 try {
List<SkuEs> hotSkuList = skuFeignClient.findHotSkuList(); List<SkuEs> hotSkuList = skuFeignClient.findHotSkuList();
if (hotSkuList != null) {
result.put("hotSkuList", hotSkuList); result.put("hotSkuList", hotSkuList);
}
} catch (Exception e) {
log.error("获取爆款商品任务出错: {}", e.getMessage(), e);
}
}, executorService);
// 设置获取爆款商品任务的超时
scheduler.schedule(() -> {
if (!getHotSkuFuture.isDone()) {
getHotSkuFuture.complete(null);
log.info("获取爆款商品任务超时");
}
}, 1, TimeUnit.SECONDS);
// 等待所有任务完成
CompletableFuture<Void> allFutures = CompletableFuture.allOf(getAddressFuture, getCategoryFuture, getNewPersonSkuInfoFuture, getHotSkuFuture);
try {
allFutures.get(); // 等待所有任务完成
} catch (InterruptedException | ExecutionException e) {
log.error("任务执行出错: {}", e.getMessage(), e);
} finally {
executorService.shutdown(); // 关闭线程池
scheduler.shutdown(); // 关闭调度器
}
//5 封装获取数据到map集合返回
return result; return result;
} }
} }