赞
踩
通常我们在使用openfeign发起服务调用时,需要同步等待调用结束。某些场景下,我们需要实现异步调用。
- @Resource
- OauthFeign oauthFeign;
-
-
- @GetMapping("/get")
- public Object get() {
-
- CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(() -> {
- return oauthFeign.openFeignApi();
- });
- return completableFuture;
- }
RequestContextHolder中是将请求信息放入ThreadLocal中的,只能取到同一个线程的数据。
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
在异步线程内部设置 RequestContextHolder.setRequestAttributes(attributes);
- CompletableFuture<T> future1 = CompletableFuture.supplyAsync(() -> {
- //openfeign的调用
- return feign.remoteCall();
- },executor);
-
- CompletableFuture<T> future2 = CompletableFuture.supplyAsync(() -> {
- //openfeign的调用
- return feign.remoteCall();
- },executor);
-
- CompletableFuture.allOf(future1,future2).join();
-
- .....
- //获取主线程的请求信息
- RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
-
- CompletableFuture<T> future1 = CompletableFuture.supplyAsync(() -> {
- //将主线程的请求信息设置到异步线程中,否则会丢失请求上下文,导致调用失败
- RequestContextHolder.setRequestAttributes(attributes);
-
- //openfeign的调用
- return feign.remoteCall();
- },executor);
-
-
- CompletableFuture<T> future2 = CompletableFuture.supplyAsync(() -> {
- //将主线程的请求信息设置到异步线程中,否则会丢失请求上下文,导致调用失败
- RequestContextHolder.setRequestAttributes(attributes);
-
- //openfeign的调用
- return feign.remoteCall();
- },executor);
-
- CompletableFuture.allOf(future1,future2).join();
-
- .....
- 1. 定义一个Feign RequestInterceptor拦截器:
- java
- public class RequestContextInterceptor implements RequestInterceptor {
- @Override
- public void apply(RequestTemplate requestTemplate) {
- ServletRequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
- if (requestAttributes != null) {
- RequestContextHolder.setRequestAttributes(requestAttributes, true);
- }
- }
- }
-
-
-
- 2. 在FeignClient上添加配置,使用该拦截器:
- java
- @FeignClient(name="hello", configuration = HelloConfiguration.class)
- public interface HelloClient {
- // ...
- }
-
- public class HelloConfiguration {
- @Bean
- public RequestInterceptor requestContextInterceptor() {
- return new RequestContextInterceptor();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。