java定时框架quartz在调用其他微服务模块时报错(非web请求使用feign完成微服务调用)

异常信息

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.

解析异常信息
创建带有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.编写一个类实现RequestAttributes接口,该接口可以不做任何配置

import org.springframework.web.context.request.RequestAttributes;
public class NonWebRequestAttributes  implements RequestAttributes {


	@Override
	public Object getAttribute(String s, int i) {

		return null;
	}

	@Override
	public void setAttribute(String s, Object o, int i) {

	}

	@Override
	public void removeAttribute(String s, int i) {

	}

	@Override
	public String[] getAttributeNames(int i) {
		return new String[0];
	}

	@Override
	public void registerDestructionCallback(String s, Runnable runnable, int i) {

	}

	@Override
	public Object resolveReference(String s) {
		return null;
	}

	@Override
	public String getSessionId() {
		return null;
	}

	@Override
	public Object getSessionMutex() {
		return null;
	}
}

2.编写一个类实现RequestInterceptor接口
2.1:在该类编写监听器
2.2:编写重复策略
2.3:重写apply方法

import com.govmade.pas.common.core.constant.CommonConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Retryer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
@Slf4j
@Component
public class FeignConfig implements RequestInterceptor {
	public FeignConfig() {
	}

	@Bean
	public Retryer feignRetryer() {
		return new Retryer.Default(100, 1000, 5);
	}

	@Bean
	public RequestContextListener requestContextListenerBean() {
		return new RequestContextListener();
	}

	@Override
	public void apply(RequestTemplate requestTemplate) {

		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if (Objects.nonNull(requestAttributes)) {
			RequestContextHolder.setRequestAttributes(requestAttributes, true);
			try {
				ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
				HttpServletRequest request = attributes.getRequest();


				requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
			} catch (Exception e) {
				log.info("定时任务介入,授权异常");
			}

		} else {
			HttpServletRequest httpRequest = this.getHttpServletRequestSafely();
			RequestContextHolder.setRequestAttributes(new NonWebRequestAttributes(),Boolean.TRUE);

			requestTemplate.header(CommonConstants.VERSION, "1.0");
			if (null != httpRequest && null != httpRequest.getAttribute("X-Request-No")) {
				requestTemplate.header("X-Request-No", httpRequest.getAttribute("X-Request-No").toString());
			}
		}

	}

	public HttpServletRequest getHttpServletRequestSafely() {
		try {
			RequestAttributes requestAttributesSafely = this.getRequestAttributesSafely();
			return requestAttributesSafely instanceof NonWebRequestAttributes ? null : ((ServletRequestAttributes) requestAttributesSafely).getRequest();
		} catch (Exception var2) {
			return null;
		}
	}

	public RequestAttributes getRequestAttributesSafely() {
		RequestAttributes requestAttributes = null;
		try {
			requestAttributes = RequestContextHolder.currentRequestAttributes();
		} catch (IllegalStateException var3) {
			requestAttributes = new NonWebRequestAttributes();
		}

		return requestAttributes;
	}
}

3.如果出现了转换异常的情况,根据代码报错信息去找到该行,进行catch(excetion e)处理一下(根据业务情况来判定)

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值