xxl-job集成feign调用外部接口报错:org.springframework.beans.factory.BeanCreationException: Error creating bean

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.oauth2ClientContext': 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.

image-20220308140541005

解析异常信息
创建带有name的bean时出错

“scopedTarget。oauth2ClientContext’:范围’请求’是不活动的当前线程;

如果您打算从一个单例对象引用它,请考虑为这个bean定义一个作用域代理;

嵌套异常是java.lang.IllegalStateException: No thread-bound request found: Are you reference

请求属性以外的实际web请求,或处理请求之外的原始

接收线程吗?如果你在一个web请求中操作并且仍然收到这个消息,

你的代码可能在DispatcherServlet之外运行:在这种情况下,使用RequestContextListener或RequestContextFilter

以公开当前请求。

原因分析
通过以上报错信息,分析出该错误是因为非web请求方式调用feign失败,因为我们在调用微服务接口时,一般情况下,我们都是在web接口里的具体实现方法通过feign来调用,也就是说,无论怎么样我们的最外层都是有一个web请求来搭建桥梁的,但是本次业务逻辑使用定时器的方式,也就是说我们从项目一run起来,该定时器就会执行,所以导致我们的调用变成非web请求方式。

解决办法

1.模拟一个http请求(没问题)

 RequestContextHolder.setRequestAttributes(
   new ServletRequestAttributes(
     new MockHttpServletRequest()));

2.1.编写一个类实现RequestAttributes接口,该接口可以不做任何配置 (参数有问题)

2.2 编写一个类实现RequestInterceptor接口
2.2.1:在该类编写监听器
2.2.2:编写重复策略
2.2.3:重写apply方法

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值