简体   繁体   English

多个 Feign 客户端超时配置

[英]Multiple Feign client timeout configurations

If you prefer using configuration properties to configured all @FeignClient, you can create configuration properties with default feign name.如果您更喜欢使用配置属性来配置所有 @FeignClient,则可以使用默认 feign 名称创建配置属性。 It's also possible to set these timeouts per specific client by naming the client.也可以通过命名客户端来为每个特定客户端设置这些超时。 And, we could, of course, list a global setting and also per-client overrides together without a problem .而且,我们当然可以毫无问题地列出全局设置和每个客户端的覆盖

My client:我的客户:

@FeignClient( contextId = "fooFeignClient", name = "foo-client", url = "${foo.url}",
    fallbackFactory = FooFallBackFactory.class,
    configuration = FooFeignConfiguration.class)

I'm trying to do this and I want foo-client.readTimeout to override default.readTimeout when I'm using foo-client:我正在尝试这样做,并且我希望 foo-client.readTimeout 在使用 foo-client 时覆盖 default.readTimeout:

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 3000
        readTimeout: 3000
        loggerLevel: full
      foo-client:
        connectTimeout: 3000
        readTimeout: 5000        
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: "false"
        isolation:
          strategy: "THREAD"
          thread:
            timeoutInMilliseconds: "3000"

But this is not happening.但这并没有发生。 I'm not sure if Hystrix's timeoutInMilliseconds could be impacting, but from my tests, it's not interfering.我不确定 Hystrix 的 timeoutInMilliseconds 是否会产生影响,但从我的测试来看,它不会产生影响。 I want the feign.readTimeout to be 5000 only for the foo-client, not for the other clients.我希望 feign.readTimeout 仅为 foo 客户端的 5000,而不是其他客户端。 But it looks like it is ignoring this foo-client configuration and using only the default configuration.但看起来它忽略了这个 foo-client 配置并且只使用了默认配置。

I know it's not a @Configuration class problem because the Feign documentation says that if we create both @Configuration bean and configuration properties, configuration properties will win.我知道这不是 @Configuration class 问题,因为 Feign 文档说如果我们同时创建 @Configuration bean 和配置属性,配置属性将会获胜。

As you have seen in the documentation, you can set timeouts per specific client.正如您在文档中看到的,您可以为每个特定客户端设置超时。 But you have only configured it for feign client, not for hystrix command.但是您只为 feign 客户端配置了它,而不是为 hystrix 命令配置它。 Remember that they have independent timeouts.请记住,它们有独立的超时。

I would recommend to use a configuration class instead of editing your application.yml in order to handle multiples feign clients with hystrix .我建议使用配置 class 而不是编辑您的 application.yml 以使用 hystrix 处理多个 feign 客户端 The following example sets up a Feign.Builder where timeouts are being injected for Feign client and Hystrix command.以下示例设置了一个 Feign.Builder,其中为 Feign 客户端和 Hystrix 命令注入了超时。

# application.yml
microservices:
    foo:
        feign:
            url: xxxx
            connect-timeout: 5s
            read-timeout: 5s
        hystrix:
            enabled: true
            timeout: 5s
@FeignClient(name = "foo",
             url = "${microservice.foo.feign.url}",
             configuration = FooFeignConfiguration.class)
public interface FooFeignRepository {
}
@Configuration
@RequiredArgsConstructor
public class FooFeignConfiguration {
    
    @Value("${microservice.foo.hystrix.enabled:true}")
    private final Boolean hystrixEnabled;

    @DurationUnit(value = ChronoUnit.MILLIS)
    @Value("${microservice.foo.feign.connect-timeout:5000}")
    private final Duration feignConnectTimeout;
    
    @DurationUnit(value = ChronoUnit.MILLIS)
    @Value("${microservice.foo.feign.read-timeout:5000}")
    private final Duration feignReadTimeout;
    
    @DurationUnit(value = ChronoUnit.MILLIS)
    @Value("${microservice.foo.hystrix.timeout:5000}")
    private final Duration hystrixTimeout;

    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return (hystrixEnabled ?
            HystrixFeign.builder()
                .options(new Request.Options(
                    (int) feignConnectTimeout.toMillis(), 
                    (int) feignReadTimeout.toMillis()))
                .setterFactory((target, method) -> {
                    return new SetterFactory.Default()
                        .create(target, method)
                        .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                            .withExecutionTimeoutInMilliseconds((int) hystrixTimeout.toMillis()));
                }):
            Feign.builder())
        .retryer(Retryer.NEVER_RETRY);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM