错误信息
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);
。。。算了不扯了,是兰斯不好玩吗?
总之,很多写的都是说加上以上代码子线程就能获取到父线程的信息,实际上本人亲测无用。正确的写法记录以下以便以后翻阅。
- 首先启动类的注解(@EnableAsync)不能忘哈
@EnableMutecSwagger2
@EnableDiscoveryClient
@EnableMutecResourceServer
@SpringBootApplication
@EnableAsync
public class MutecWebinarAdminApplication {
public static void main(</