Ошибка создания бина с именем 'scopedTarget.contextHeadersBuilder - PullRequest
0 голосов
/ 16 апреля 2020

Я использую версию 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.

1 Ответ

0 голосов
/ 17 апреля 2020

Я нашел решение для текущей проблемы. Нужно добавить в файл свойств приложения

hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
        isolation:
          thread:
            timeoutInMilliseconds: 6000
          strategy: SEMAPHORE
...