使用Feign远程调用丢失请求头问题

在使用Feign进行远程调用时,当前服务是能拿到请求头信息的,请求头包含有登录认证Cookie等重要信息,但是在调用远程服务时,远程服务却拿不到请求头信息,因为使用Feign进行远程调用实际上是发起新的Request请求了,所以就获取不到上一个请求的数据。请求示意图如下所示:

此时我们需要在在当前服务中添加配置类,用于将当前请求信息传递给远程服务,下面是配置类示例代码:

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;

/**
 * @author: 
 * @Desc:
 * @create:
 **/
@Configuration
public class GuliFeignConfig {
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                //RequestContextHolder是feign设计用于获取请求上下文数据的工具类
               ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                if(request!=null){
                    //获取源请求的请求头数据
                    String cookie = request.getHeader("Cookie");
                    //给新请求同步源请求的Cookie
                    template.header("Cookie",cookie);
                }

            }
        };
    }
}

添加上面的配置类后,在同步环境中能可确保远程服务能获取当前服务的请求头数据。但是如果使用异步操作,仅靠上面的配置类是不够的。这是因为:

数据是线程私有的,不能共享,所以使用异步的远程服务获取不了主线程的请求数据。此时需要进行配置:

方法1:硬编码,在主线程中获取到请求数据,在异步线程操作中赋值进去,

优点:简单易懂,上手快;

缺点:侵入业务代码,且每使用一个线程就要写这样一段代码,还会导致大量冗余代码。

示例代码:

java 复制代码
         //获取当前线程请求数据
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //使用异步编排处理多个远程请求任务
        //1.远程查询会员的收货地址
        CompletableFuture<Void> getAddressFuture = CompletableFuture.runAsync(() -> {
            //使用异步编排后,由于线程不同,需要将源请求上下文数据赋给当前线程
            RequestContextHolder.setRequestAttributes(requestAttributes);
            List<MemberAddressVo> address = orderFeignServie.getAddress(memberRespVo.getId());
            orderConfirmVo.setAddress(address);
        }, executor);

        //2.远程查询购物车中的购物项
        CompletableFuture<Void> getCartItemFuture = CompletableFuture.runAsync(() -> {
            //使用异步编排后,由于线程不同,需要将请求源上下文数据赋给当前线程
            RequestContextHolder.setRequestAttributes(requestAttributes);
            List<OrderItemVo> orderItemVos = cartFeignService.currentUserCartItem();
            orderConfirmVo.setItems(orderItemVos);
        }, executor);

方法2: 创建一个抽象类,实现Runnable方法。

优点:无侵入,代码简洁;

下面是示例代码:

java 复制代码
public abstract class MyRunnable implements Runnable{
    private RequestAttributes requestAttributes;

    public MyRunnable() {
        requestAttributes = RequestContextHolder.getRequestAttributes();
    }

    public abstract void myRun();

    @Override
    public void run() {
        //每一个线程都来共享之前的请求数据
        RequestContextHolder.setRequestAttributes(requestAttributes);
        myRun();
    }
}

然后在业务代码中使用MyRunnable的myRun()方法:

java 复制代码
//开启第一个异步任务
CompletableFuture<Void> addressFuture2 = CompletableFuture.runAsync(new MyRunnable() {
	@Override
	public void myRun() {
		//1、远程查询所有的收获地址列表
		List<MemberAddressVo> address = memberFeignService.getAddress(memberResponseVo.getId());
		confirmVo.setMemberAddressVos(address);
	}
}, threadPoolExecutor);

如此便可在异步使用Feign进行远程调用时获取到当前请求的请求数据。

更多信息可参考:谷粒商城实战笔记-问题记录-Feign异步调用丢失请求头问题_异步调用feign请求对象丢失-CSDN博客

相关推荐
搬码临时工7 分钟前
如何更改远程桌面连接的默认端口?附外网访问内网计算机方法
服务器·网络·远程工作·访问公司内网
sy_cora10 分钟前
IEEE 列表会议第五届机器人、自动化与智能控制国际会议
运维·人工智能·机器人·自动化
数巨小码人39 分钟前
Linux常见命令
大数据·linux·运维·服务器·elasticsearch·搜索引擎
家庭云计算专家42 分钟前
还没用过智能文档编辑器吗?带有AI插件的ONLYOFFICE介绍
服务器·人工智能·docker·容器·编辑器
magic 2451 小时前
第五章:Linux用户管理
linux·运维·服务器
小叶子来了啊1 小时前
信息系统运行管理员:临阵磨枪版
运维·服务器·数据库
-天涯762 小时前
5.13/14 linux安装centos及一些操作命令随记
linux·运维·服务器
佩奇的技术笔记2 小时前
CentOS系统中升级Python 3.12.2版本
linux·运维·服务器
HackerKevn2 小时前
【项目】自主实现HTTP服务器:从Socket到CGI全流程解析
服务器·网络协议·http
北漂老男孩2 小时前
主流数据库运维故障排查卡片式速查表与视觉图谱
运维·数据库