【发布时间】:2020-04-16 07:45:08
【问题描述】:
我正在使用 Spring Boot 1.5.9 版本以及 Feign 和 Fallback。 通过 Feign 客户端调用单个服务时出现奇怪的错误。 请帮忙。
我的 Feign 配置
@Configuration
@Import(HeaderConverter.class)
public class SecuritiesFeignConfig {
private final HeaderConverter headerConverter;
public SecuritiesFeignConfig(HeaderConverter headerConverter) {
this.headerConverter = headerConverter;
}
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> headerConverter.getCallContextHeaders().forEach(requestTemplate::header);
}
@Bean
public Decoder feignDecoder() {
HttpMessageConverter<?> jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
private ObjectMapper customObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
和我在应用程序中的主要配置类
@Configuration
@Import(FiltersConfig.class)
public class frameworkConfig {
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MSACallContextImpl callContext() {
return new MSACallContextImpl();
}
@Bean
public CallContextCreator callContextCreator() {
return new CallContextCreator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public ContextHeadersBuilder contextHeadersBuilder(MSACallContextImpl callContextImpl) {
return new ContextHeadersBuilder(callContextImpl);
}
@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
}
但是当我尝试通过 Feign 客户端进行服务调用时,出现以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.contextHeadersBuilder': 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
【问题讨论】:
-
你正在使用一个你不应该使用的范围。
-
对不起,你的意思是:@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)?
-
您正在使用
ContextHeadersBuilder的 bean 由不在请求范围内的组件使用。因此,mkaing 这个 bean 请求作用域是行不通的。为什么首先需要请求范围? -
如果我将“request”替换为“singleton” - 它对我有用。但在这种情况下,我们失去了上下文 =(
标签: java spring spring-boot spring-cloud-feign feign