【问题标题】:How to set custom Feign client connection timeout?如何设置自定义 Feign 客户端连接超时?
【发布时间】:2017-05-19 03:04:05
【问题描述】:

我有这个 Gradle 依赖项的 Spring Boot 应用程序:

compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile("org.springframework.cloud:spring-cloud-starter-feign")
compile("org.springframework.cloud:spring-cloud-starter-ribbon")
compile("org.springframework.cloud:spring-cloud-starter-hystrix")
compile("org.springframework.cloud:spring-cloud-starter-config")

我还有 Feign 客户端:

@FeignClient(name = "client")
public interface FeignService {

    @RequestMapping(value = "/path", method = GET)
    String response();

}

我的application.properties:

client.ribbon.listOfServers = http://localhost:8081
ribbon.eureka.enabled=false

当查询时间超过 1 秒时出现异常:

com.netflix.hystrix.exception.HystrixRuntimeException: response timed-out and no fallback available.

所以我的问题是:如何设置自定义 Feign 客户端连接超时?例如 2 秒。

【问题讨论】:

    标签: java hystrix feign


    【解决方案1】:

    您可以使用 application.yaml 文件中的配置属性来配置超时:

    feign:
      client:
        config:
          default:
            connectTimeout: 5000
            readTimeout: 5000
    

    请注意,这将更改您的默认 feign 配置,如果您只想为您的客户端更新超时,请将 default 替换为 @FeignClient 中配置的名称,在您的情况下它将是 client,另一件事是您必须同时指定 connectTimeout 和 readTimeout 才能生效。

    更多详情请看:documentation

    【讨论】:

      【解决方案2】:

      我使用answer 解决了我的问题:Hystrix command fails with “timed-out and no fallback available”。

      我将hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000 添加到我的application.properties 以设置自定义超时。

      【讨论】:

      • 很高兴为您提供帮助!
      • @Roman 你也可以使用操作名对每个方法做细粒度的超时控制:hystrix.command."FeignService#sayHello(String)".execution.isolation.thread.timeoutInMilliseconds: 20000
      • 如何更改 maven 项目的超时时间??
      • 你有任何关于如何添加超时并解决此问题的 github 链接吗?
      猜你喜欢
      • 2021-06-02
      • 2019-11-12
      • 2011-08-03
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2019-02-28
      • 1970-01-01
      相关资源
      最近更新 更多