异步feign调用第三方接口失败

错误信息

Scope 'request' is not active for the current thread; consider defining a scoped proxy 
for this bean if you intend to refer to it from a singleton; nested exception is 
java.lang.IllegalStateException: No thread-bound request found: Are you referring to 
request attributes outside of an actual web request, or processing a request outside of
the originally receiving thread? If you are actually operating within a web request and still 
receive this message, your code is probably running outside of DispatcherServlet: In this
case, use RequestContextListener or RequestContextFilter to expose the current request.

当前线程的作用域“请求”未激活;如果您想从一个单体引用它,请考虑为这个bean定义一个作用域代理;嵌套的例外是java。lang.IllegalStateException:未找到线程绑定请求:您是指实际web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际上是在web请求中操作,并且仍然收到此消息,那么您的代码可能是在DispatcherServlet之外运行的:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。(百度翻译)

问题描述

  • 在微服务中使用Feign调用,为了使Feign调用都是登录状态,会把登录状态的Cookie信息都携带上,使用的拦截器往请求里面存储cookie信息使用RequestContextHolder;

  • 开启另外一个线程异步调用feign,因为httpRequest是绑定在ThreadLocal中的,只能在当前线程中共享,子线程是不能获取到父线程的属性.

本人小白,对多线程不太了解,以上都是百度几个小时,啪的一下,很快啊。灵感来了才把问题解决。
首先,你百度应该多多少少会看到这行代码:

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes();

或者这行:

  RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);

。。。算了不扯了,是兰斯不好玩吗?
总之,很多写的都是说加上以上代码子线程就能获取到父线程的信息,实际上本人亲测无用。正确的写法记录以下以便以后翻阅。

  1. 首先启动类的注解(@EnableAsync)不能忘哈
@EnableMutecSwagger2
@EnableDiscoveryClient
@EnableMutecResourceServer
@SpringBootApplication
@EnableAsync
public class MutecWebinarAdminApplication {
   
  public static void main(</
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值