是否可以在 BootstrapConfiguration 上使用 @EnableFeignClients?

Should it be possible to use @EnableFeignClients on a BootstrapConfiguration?

我想使用来自 PropertySourceLocator 的假客户端,它必须在 bootstrap 期间注册。我的 spring.factories 中有以下内容:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
test.PropertiesConfiguration

@Configuration
@EnableFeignClients
public class PropertiesConfiguration {

    @Bean
    public PropertySourceLocator propertySourceLocator() {
        return new MyPropertySourceLocator();
    }
}

假客户端自动连接到 MyPropertySourceLocator。然而,虽然同一个客户端在常规 bean 中工作,但它在这里不起作用。所有请求都失败并出现此异常:

feign.codec.DecodeException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:173)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:141)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
    at com.sun.proxy.$Proxy23.get(Unknown Source)
    at test.MyPropertySourceLocator.getProperty(MyPropertySourceLocator.java:32)
    at org.springframework.core.env.CompositePropertySource.getProperty(CompositePropertySource.java:59)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:60)
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:511)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.getProperty(PropertySourcesPlaceholderConfigurer.java:135)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.getProperty(PropertySourcesPlaceholderConfigurer.java:132)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:70)
    at org.springframework.core.env.AbstractPropertyResolver.resolvePlaceholder(AbstractPropertyResolver.java:207)
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:962)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:342)
    // SNIP
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectFactory.getObject(DefaultListableBeanFactory.java:1419)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyProvider.get(DefaultListableBeanFactory.java:1436)
    at org.springframework.cloud.netflix.feign.support.SpringDecoder.decode(SpringDecoder.java:57)
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
    ... 93 common frames omitted

这发生在 @FeignClient(url = "localhost:8080")。将色带与 @FeignClient("config") 一起使用完全失败:

Caused by: java.lang.NullPointerException
    at feign.ribbon.LoadBalancingTarget.apply(LoadBalancingTarget.java:91)
    at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:164)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:108)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
    at com.sun.proxy.$Proxy26.get(Unknown Source)
    at test.MyPropertySourceLocator.getProperty(MyPropertySourceLocator.java:32)

我想使用 REST 访问配置(因此假装)。可能的解决方法当然是直接使用 feign。

bootstrap 上下文应该是轻量级的(并且创建起来很快),所以它不是完整的 @EnableAutoconfiguration。要使用 @FeignClient,您需要导入 FeignAutoConfiguration(以及任何依赖于 IIRC 的内容)。