簡體   English   中英

多個 Feign 客戶端超時配置

[英]Multiple Feign client timeout configurations

如果您更喜歡使用配置屬性來配置所有 @FeignClient,則可以使用默認 feign 名稱創建配置屬性。 也可以通過命名客戶端來為每個特定客戶端設置這些超時。 而且,我們當然可以毫無問題地列出全局設置和每個客戶端的覆蓋

我的客戶:

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

我正在嘗試這樣做,並且我希望 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"

但這並沒有發生。 我不確定 Hystrix 的 timeoutInMilliseconds 是否會產生影響,但從我的測試來看,它不會產生影響。 我希望 feign.readTimeout 僅為 foo 客戶端的 5000,而不是其他客戶端。 但看起來它忽略了這個 foo-client 配置並且只使用了默認配置。

我知道這不是 @Configuration class 問題,因為 Feign 文檔說如果我們同時創建 @Configuration bean 和配置屬性,配置屬性將會獲勝。

正如您在文檔中看到的,您可以為每個特定客戶端設置超時。 但是您只為 feign 客戶端配置了它,而不是為 hystrix 命令配置它。 請記住,它們有獨立的超時。

我建議使用配置 class 而不是編輯您的 application.yml 以使用 hystrix 處理多個 feign 客戶端 以下示例設置了一個 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