仅为一个客户端创建 Feign 配置?

Hel*_*rld 6 java rest spring spring-cloud feign

我有一大堆使用共享配置的 Feign 客户端(MyFeignConfiguration.class):

@FeignClient(name = "clientA", url = "http://serviceA.com", fallbackFactory = ServiceAFallbackFactory.class, configuration = MyFeignConfiguration.class)

@FeignClient(name = "clientB", url = "http://serviceB.com", fallbackFactory = ServiceBFallbackFactory.class, configuration = MyFeignConfiguration.class)

@FeignClient(name = "clientC", url = "http://serviceC.com", fallbackFactory = ServiceCFallbackFactory.class, configuration = MyFeignConfiguration.class)
Run Code Online (Sandbox Code Playgroud)

但是,对于新客户端,我想将用于 OkHttp 的底层 Http 客户端更改为 OkHttp。在MyFeignConfiguration课堂上,我可以添加以下内容:

@Configuration
class MyFeignConfiguration {
    @Bean
    public Client getClient() {
        return OkHttpClient() // use the OkHttp client 
    }
   
    @Bean
    public ErrorDecoder getErrorDecoder() {
        //... existing configs
    }
Run Code Online (Sandbox Code Playgroud)

然而,现在所有的客户端都在使用这个OkHttp客户端。如何配置新的 feign 客户端以便只有它使用 OkHttp 客户端?另外,我仍然需要使用ErrorDecoderMyFeignConfiguration类中现有的默认配置(如 )。

小智 6

查看文档有一个导入的注释https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-overriding-defaults

FooConfiguration不需要用@Configuration注解。但是,如果是,请注意将其从任何包含此配置的 @ComponentScan 中排除,因为在指定时它将成为 feign.Decoder、feign.Encoder、feign.Contract 等的默认源。可以通过将其与任何 @ComponentScan 或 @SpringBootApplication 放在单独的、不重叠的包中来避免这种情况,或者可以将其显式排除在 @ComponentScan 中。

尝试去掉注释:@Configuration