Openfeign异步调用丢失上下文信息

lijunyi2022-06-29SpringCloud微服务

OpenFeign 异步调用丢失上下文信息

1、场景

CompletableFuture<T> future1 = CompletableFuture.supplyAsync(() -> {

    return feign.remoteCall();
},executor);

CompletableFuture<T> future2 = CompletableFuture.supplyAsync(() -> {

    return feign.remoteCall();
},executor);

CompletableFuture.allOf(future1,future2).join();

.....

2、解决方案-新建拦截器

@Component
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor 
{

    @Override
    public void apply(RequestTemplate template) 
    {
        HttpServletRequest httpServletRequest = RequestContextUtils.getRequest();
        Map<String, String> headers = getHeaders(httpServletRequest);
        for (Map.Entry<String, String> entry : headers.entrySet()) 
        {
            template.header(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 获取原请求头
     */
    private Map<String, String> getHeaders(HttpServletRequest request) 
    {
        Map<String, String> map = new LinkedHashMap<>();
        Enumeration<String> enumeration = request.getHeaderNames();
        if (enumeration != null) 
        {
            while (enumeration.hasMoreElements()) 
            {
                String key = enumeration.nextElement();
                String value = request.getHeader(key);
                if (StrUtil.equals(OAuthConstant.TOKEN_NAME,key))
                {
                    map.put(key, value);
                    break;
                }
            }
        }
        return map;
    }
}

这里将请求头中Token信息放入RequestTemplate的头中,取出请求头中信息用的是RequestContextHolder

RequestContextHolder中是将请求信息放入ThreadLocal中的,只能取到同一个线程的数据。

因此要解决异步调用的问题,只需要在发起远程调用之前给异步线程添加上主线程的上下文信息

//获取主线程的请求信息
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();

.....

Last Updated 6/14/2024, 3:05:31 AM