構成情報を追加する
#配置自定义的线程池
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());
}
}
リモートサービスを偽って呼び出すプロセスでは、フィルターはレイヤーごとに呼び出されます。フロントエンドから渡されたリクエストヘッダーの情報を、requestInterceptorインターセプターの新しいリクエストに再割り当てする必要があるため、ここに追加します。構成クラス:thisこのメソッドは、偽の要求のみが同期的に呼び出される状況を解決できます。
@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);
}
}
}
};
}
}
- 開発プロセスでは、スレッドプールを使用して他のサービスを非同期的に呼び出す状況が発生する可能性があります。スレッド非同期呼び出しを開始すると、スレッドプール内のスレッドが再取得されるため、リクエストヘッダー情報がありません。他のサービスモジュールがリクエストヘッダー内のトークンやその他の情報を認証する必要がある場合は、どうすればよいですか?
- 偽の+非同期タスクは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;
}
}