创建名称为'scopedTarget.contextHeadersBuilder的bean时出错

问题描述 投票:0回答:1

我正在使用Spring Boot 1.5.9版本以及Feign and Fallback。通过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.
java spring spring-boot spring-cloud-feign feign
1个回答
0
投票

我找到了当前问题的解决方案。需要添加到应用程序属性文件中

hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
        isolation:
          thread:
            timeoutInMilliseconds: 6000
          strategy: SEMAPHORE
© www.soinside.com 2019 - 2024. All rights reserved.