在微服务环境下,远程调用feign和异步线程存在请求数据丢失问题

一、无异步线程得情况下feign远程调用:

1、登录拦截器:

``` @Component public class LoginUserInterceptor implements HandlerInterceptor { public static ThreadLocal<MemberResVo> loginUser = new ThreadLocal<>(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //获取登录用户的键
    MemberResVo attribute = (MemberResVo) request.getSession().getAttribute(AuthServerConstant.LONG_USER);
    if (attribute!=null){
        loginUser.set(attribute);
        return true;
    }else {
        request.getSession().setAttribute("msg","请先进行登录!");
        response.sendRedirect("http://auth.gulimall.com/login.html");
        return false;
    }
}

}

<h3>2.问题示例图:</h3>
<p><img style="display: block; margin-left: auto; margin-right: auto;" src="https://img2020.cnblogs.com/blog/2510377/202108/2510377-20210825191329004-206059320.png"  width="706" height="368" /></p>
<h3>3.解决方法:</h3>
```java
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class GuliFeignConfig {
    //fegin过滤器
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            public void apply(RequestTemplate template) {
                //上下文环境保持器,拿到刚进来这个请求包含的数据,而不会因为远程数据请求头被清除
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();//老的请求
                if (request != null) {
                    //同步老的请求头中的数据,这里是获取cookie
                    String cookie = request.getHeader("Cookie");
                    template.header("Cookie", cookie);
                }
            }
        };
    }
}

二、异步情况下丢失上下文问题:

![](https://img2020.cnblogs.com/blog/2510377/202108/2510377-20210825194404806-2044013067.png)

① 在同一线程下进行远程调用,即一连串调用的情况下OrederService通过远程调用先查找adress信息,再查找cart信息,则仅需配置GuliFeignConfig就够了
② 由于采用的异步任务,所以101、102线程在自己的线程中调用登录拦截器interceptor,而其实只有在72号线程中登陆拦截器才进行放行(有请求头数据),这就导致101、102的request为null

解决方式(高亮部分):从总线中获取request数据放入子线程中

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes()
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl&lt;OrderDao, OrderEntity&gt; implements OrderService {
    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    CartFeginService cartFeginService;

    @Autowired
    ThreadPoolExecutor executor;

    @Autowired
    WmsFeignService wmsFeignService;

    /**
     * 订单确认页返回的数据
     * @return
     */
    @Override
    public OrderConfirmVo confirmOrder() throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = new OrderConfirmVo();
        MemberResVo memberResVo = LoginUserInterceptor.loginUser.get();
        //从主线程中获得所有request数据
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        CompletableFuture&lt;Void&gt; getAddressFuture = CompletableFuture.runAsync(() -&gt; {
            //1、远程查询所有地址列表
            RequestContextHolder.setRequestAttributes(requestAttributes);
            List&lt;MemberAddressVo&gt; address = memberFeignService.getAddress(memberResVo.getId());

            confirmVo.setAddress(address);
        }, executor);

        //2、远程查询购物车所选的购物项,获得所有购物项数据
        CompletableFuture&lt;Void&gt; cartFuture = CompletableFuture.runAsync(() -&gt; {
            //放入子线程中request数据
            RequestContextHolder.setRequestAttributes(requestAttributes);
            List&lt;OrderItemVo&gt; items = cartFeginService.getCurrentUserCartItems();
            confirmVo.setItem(items);
        }, executor).thenRunAsync(()-&gt;{
            RequestContextHolder.setRequestAttributes(requestAttributes);
            List&lt;OrderItemVo&gt; items = confirmVo.getItem();
            List&lt;Long&gt; collect = items.stream().map(item -&gt; item.getSkuId()).collect(Collectors.toList());
            //远程调用查询是否有库存
            R hasStock = wmsFeignService.getSkusHasStock(collect);
            //形成一个List集合,获取所有物品是否有货的情况
            List&lt;SkuStockVo&gt; data = hasStock.getData(new TypeReference&lt;List&lt;SkuStockVo&gt;&gt;() {
            });
            if (data!=null){
                //收集起来,Map&lt;Long,Boolean&gt; stocks;
                Map&lt;Long, Boolean&gt; map = data.stream().collect(Collectors.toMap(SkuStockVo::getSkuId, SkuStockVo::getHasStock));
                confirmVo.setStocks(map);
            }
        },executor);
        //feign远程调用在调用之前会调用很多拦截器,因此远程调用会丢失很多请求头

        //3、查询用户积分
        Integer integration = memberResVo.getIntegration();
        confirmVo.setIntegration(integration);
        //其他数据自动计算

        CompletableFuture.allOf(getAddressFuture,cartFuture).get();
        return confirmVo;
    }

}

在微服务环境下,远程调用feign和异步线程存在请求数据丢失问题

上一篇:OSW使用oswbba.jar数据分析遇到的一些问题


下一篇:Spring MVC(二)手写一个简易的Spring MVC框架