【问题标题】:How to disable hystrix in one of multiple feign clients如何在多个假装客户端之一中禁用 hystrix
【发布时间】:2019-08-07 13:36:24
【问题描述】:

在我的 Spring Boot 应用程序中,我使用了多个 feign 客户端 (@FeignClient("hello-service"))。在很多情况下,我需要一个断路器机制,所以我有以下配置。

feign.hystrix.enabled=true

但是我不知道如何配置特定的 feign 客户端不使用 Hystrix。可能吗?有没有人设法以这种方式配置 spring 应用程序?

【问题讨论】:

标签: spring spring-cloud hystrix feign


【解决方案1】:

您可以使用禁用的 hystrix 功能创建自己的配置,并将其用于必要的客户端。

public class FeignClientConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
    return Feign.builder();
}
}

详情见 paragraph 7.4

【讨论】:

    【解决方案2】:

    我想扩展 Roman 的答案,因为我一开始不知道如何使用它。

    正如他所说,您需要有这样的配置类

    public class MyFeignConfiguration {
    
        @Bean
        @Scope("prototype")
        public Feign.Builder feignBuilder() {
            return Feign.builder();
        }
    }
    

    你需要把这个配置类包含到你的@FeignClient中,像这样

    @FeignClient(name = "name", url = "http://example.com", configuration = MyFeignConfiguration .class)
    public interface MyApi {
        //...
    }
    

    使用此配置,此客户端将在不使用 Hystrix 包装的情况下构建

    【讨论】:

    • 这种方法对我不起作用。添加新的 @Bean 会禁用我在所有 Feign 客户端上的 Hystrix 行为。即使对于那些不使用新配置类的人也是如此。详情看这里stackoverflow.com/questions/62669138/…
    • 如果 MyFeignConfiguration 类现在有 @Configuration 注解,它应该可以工作。来自官方 Feign 文档:FooConfiguration 不需要使用 @Configuration 进行注释。但是,如果是,请注意将其从任何可能包含此配置的 @ComponentScan 中排除,因为在指定时它将成为 feign.Decoder、feign.Encoder、feign.Contract 等的默认源。这可以通过将其与任何@ComponentScan 或@SpringBootApplication 放在一个单独的、不重叠的包中来避免,也可以在@ComponentScan 中明确排除。
    猜你喜欢
    • 2020-10-21
    • 2019-12-07
    • 2020-10-07
    • 2021-08-10
    • 1970-01-01
    • 2018-02-27
    • 2021-01-30
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多