添加 配置信息
#配置自定义的线程池
gulimall.thread.core-size=20
gulimall.thread.max-size=200
gulimall.thread.keep-alive-time=10
添加配置类
//跟配置文件绑定
@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
private Integer coreSize;
private Integer maxSize;
private Integer keepAliveTime;
}
添加线程池配置类
//开启属性配置(因为ThreadPoolConfigProperties写了@Component,所以就不用写了)
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
return new ThreadPoolExecutor(pool.getCoreSize(),
pool.getMaxSize(),
pool.getKeepAliveTime(), TimeUnit.SECONDS,
new LinkedBlockingDeque<>(100000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
}
}
在feign调用远程服务的过程中,会一层层调用过滤器,我们需要在requestInterceptor拦截器里面重新将前端传递过来的请求头里面的信息赋值到新的请求,所以这里添加一个配置类:这种方式能够解决只是一个feign请求同步调用的的情况。
@Configuration
public class GuliFeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
if (request != null) {
//同步请求头数据 主要同步Cookie
String cookie = request.getHeader("Cookie");
//给新请求同步的老请求Cookie
template.header("Cookie", cookie);
}
}
}
};
}
}
- 在我们开发过程中,可能会遇到使用线程池异步调用其他服务的情况,由于在我们开启线程异步调用的时候,是重新在线程池里面获取一个线程,所以会不存在任何的请求头信息。如果其他服务模块需要对请求做请求头里面的token等信息的认证,那么我们应该怎么做呢?
- Feign + 异步任务 需要共享RequestAttributes 每个任务都要setRequestAttributes(),将前端传递过来的请求头里面的参数取出来再重新赋值给线程池里面的新线程。
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
@Autowired
MemberFeignService memberFeignService;
@Autowired
CartFeignService cartFeignService;
@Autowired
WareFeignServicewareFeignService;
@Autowired
ThreadPoolExecutor executor;
//共享前端页面传过来的vo
ThreadLocal<OrderSubmitVo> confirmVoThreadLocal = new ThreadLocal<>();
/**
* 去结算
* 给订单确认ye返回数据
*/
@Override
public OrderConfirmVo confirmOrder() throws ExecutionException, InterruptedException {
//要返回的大对象
OrderConfirmVo confirmVo = new OrderConfirmVo();
//在TreadLocal中获取用户
MemberResVo memberResVo = LoginUserInterceptor.loginUser.get();
//Feign + 异步任务 需要共享RequestAttributes 每个任务都要setRequestAttributes(),将前端传递过来的请求头里面的参数取出来再重新赋值给线程池里面的新线程。
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
CompletableFuture<Void> getAddressFuture = CompletableFuture.runAsync(() -> {
//每一个线程都要共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//1 远程查询大对象的第一个属性 收货地址列表
List<MemberAddressVo> address = memberFeignService.getAddress(memberResVo.getId());
confirmVo.setAddress(address);
//Feign在远程调用之前要构造请求,调用很多的拦截器
}, executor);
CompletableFuture<Void> cartFuture = CompletableFuture.runAsync(() -> {
//每一个线程都要共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//2 远程查询大对象的第二个属性 所有购物项
List<OrderItemVo> items = cartFeignService.currentUserItems();
confirmVo.setItems(items);
}, executor).thenRunAsync(() -> {
//执行下一个任务线程
R r = wareFeignService.getSkuHasStock(ids);
}, executor);
CompletableFuture.allOf(getAddressFuture, cartFuture).get();
return confirmVo;
}
}