记一次SpringCloud OpenFeign 服务调用传递 token @Async 上下文信息获取失败

一、场景

在异步方法中使用了feign调用,发现提示"您还未登录或登录已失效"。那原因很明了就是我的登录信息没办法传入到feign的调用方法里。

二、考虑的解决办法

1)尝试一:ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

java 复制代码
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
sendAsyncService.statisticsSendSms(idList, userTokenValue, sendSMSNotificationParams, coreDescriptionParams, attributes);
java 复制代码
public void statisticsSendSms(List<Long> reportIdList,
	                              UserTokenValue userTokenValue,
	                              SendSMSNotificationParams sendSMSNotificationParams,
	                              CoreDescriptionParams coreDescriptionParams,
	                              ServletRequestAttributes attributes) {
		// 设置子线程共享
		RequestContextHolder.setRequestAttributes(attributes, true);
		//figen 调用,需要token
		ResponseResult<String> responseResult = smsApi.sendSMSNotification(sendSMSNotificationParams);

这种情况犹豫反复在测试环境测试,都无任何问题,心里自信满满,但是上线后,却暴漏了,依旧提示"您还未登录或登录已失效"。这是什么情况?

1-1)RequestContextHolder跨线程获取不到requests请求对象的原因

为何会失败呢?因为异步注解,顾名思义,是开启了一个新的线程去执行一些代码。

java 复制代码
/**
 * 给当前线程绑定属性
 * @param inheritable 是否要将属性暴露给子线程
 * */
public static void setRequestAttributes(@Nullable RequestAttributes attributes, boolean inheritable) {
		if (attributes == null) {
			resetRequestAttributes();
		}
		else {
			if (inheritable) {
				inheritableRequestAttributesHolder.set(attributes);
				requestAttributesHolder.remove();
			}
			else {
				requestAttributesHolder.set(attributes);
				inheritableRequestAttributesHolder.remove();
			}
		}
	}

那源代码理解,已经将属性暴漏给子线程了,为何上线后依旧是获取不到呢?那其实从根本思考,主线程加入相应速度快于子线程呢,结果会是如何?结果就是主线程结束,所有的信息都已经销毁了,谁还管你子线程是否执行没执行呢,哈哈哈。测试环境未复现,是因为环境配置低,可能测试的几次都一定概率的主线程相应时间大于了子线程的相应时间,到了线上,由于高配置的环境,这种平衡性被打破了,主线程先行一步了。

缘由了解了,解决的办法也就来了----肯定就是让主线程等一下,等子线程执行完再结束,可是主线程会那么礼貌吗?该如何解决这个问题呢?

2)自定义异步线程池,配置中做相关处理

java 复制代码
// 解决使用@Async注解,获取不到上下文信息的问题
        executor.setTaskDecorator(runnable -> {

            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            AsyncContext asyncContext;
            if (attributes!=null){
                HttpServletRequest request=attributes.getRequest();
                asyncContext=request.startAsync();
            } else {
                asyncContext = null;
            }
            return ()->{
                try {
                    // 我们set 进去 ,其实是一个ThreadLocal维护的.
                    RequestContextHolder.setRequestAttributes(attributes);
                    runnable.run();
                }finally {
                    RequestContextHolder.resetRequestAttributes();
                    if (asyncContext!=null){
                        asyncContext.complete();
                    }
                }

            };
        });

2-1)Servlet的异步处理

是由AsyncContext接口来实现的。

java 复制代码
AsyncContext asyncContext;
            if (attributes!=null){
                HttpServletRequest request=attributes.getRequest();
                asyncContext=request.startAsync();
            } else {
                asyncContext = null;
            }

2-3)异步注解使用了自定义线程池,避免一个方法多个异步注解使用

java 复制代码
@Async("asyncServiceExecutor")
java 复制代码
AsyncContext asyncContext;
            if (attributes!=null){
                HttpServletRequest request=attributes.getRequest();
                asyncContext=request.startAsync();
            } else {
                asyncContext = null;
            }

由于线程池中我们使用了Servlet的异步处理来 set我们需要的token信息,所以避免重复调用。多次调用会报错,即使不影响信息获取。

在Servlet中多次调用自定义线程池可能导致错误,这通常是因为Servlet的单实例模式导致的线程安全问题。Servlet容器通常会为每个Servlet维护一个实例,当多个请求同时访问同一个Servlet实例时,可能会出现竞态条件,比如多个线程尝试使用相同的资源或状态。

相关推荐
pengyu几秒前
【Java设计原则与模式之系统化精讲:壹】 | 编程世界的道与术(实战指导篇)
java·后端·设计模式
陈随易4 分钟前
一行代码,将网页元素变成图片!比 html2canvas 快 93 倍的截图神器来了!
前端·后端·程序员
Kookoos4 分钟前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
掉头发的王富贵6 分钟前
Arthas神器入门:动态调试Java应用,轻松搞定生产环境Bug!
java·后端·debug
汪子熙9 分钟前
解密 Fabric 体系 —— 架构与实践全解析
后端
oraen12 分钟前
一篇文章让你在根本上理解遗传算法,用牛刀杀鸡-使用遗传撕力扣
后端
程序员爱钓鱼13 分钟前
Go语言并发模型与模式:Worker Pool 模式
后端·go·排序算法
Victor35614 分钟前
MySQL(66)如何进行慢查询日志分析?
后端
程序小武14 分钟前
深入理解Python内置模块及第三方库的使用与管理
后端
陈随易16 分钟前
2025年100个产品计划之第12个(杰森排序) - 对 JSON 属性进行排序
前端·后端·程序员