OpenFeign + Hystrix - 不同客户端的不同超时

时间:2021-02-24 17:03:20

标签: java spring-boot hystrix spring-cloud-feign feign

我让 Hystrix 在我的 Spring Boot 应用程序中使用 Feign。

我已将 Hystrix 的默认超时设置为 10000 毫秒:

feign:
  hystrix:
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

问题是我有这个客户端,我们称之为 HeavyClient,这是一个繁重的调用,有时需要更多时间并导致电路中断。 我只想为这个人增加 Hystrix 中的超时上限。可能吗?

我尝试过使用 Feign 属性,例如:

feign:
  client:
    config:
      HeavyClient:
        connectTimeout: 30000
        readTimeout: 30000

但这行不通。我猜 Hystrix 不会检查 Feign 属性。

我正在使用:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

经过周转和更深入的搜索后,我在 this GitHub issue 上发现 Hystrix 命令的默认命名类似于 InterfaceName#methodNameSignature()

因此,例如,给定以下 @FeignClient

@FeignClient(name = "heavyClient", url = "${heavyclient.url}")
public interface HeavyClient {
    
    @RequestMapping("/process/{operation}")
    public ResponseEntity<Response> process(@PathVariable("operation") String operation);
    
}

它将被配置为:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000
    HeavyClient#process(String):
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000

不幸的是,这对我不起作用...不知道为什么...=s 所以为了解决这个问题,我注册了一个 SetterFactory 类型的 bean,它将创建给定 @FeignClient 名称的命令键:

    @Bean
    public SetterFactory setterFactory() {
        return (target, method) -> HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(target.name()))
                .andCommandKey(HystrixCommandKey.Factory.asKey(target.name()));
    }

然后我可以简单地使用配置:

hystrix:
  command:
    heavyClient:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
相关问题