如何设置自定义的Feign客户端连接超时时间?

12

我有一个使用以下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秒。

2个回答

18
我使用这个答案解决了我的问题,该问题是关于:Hystrix命令超时且无回退可用

我在application.properties中添加了hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000以设置自定义超时时间。


很高兴能帮助你! - psantamaria
11
@Roman,您还可以使用操作名称对每个方法进行细粒度的超时控制: hystrix.command."FeignService#sayHello(String)".execution.isolation.thread.timeoutInMilliseconds: 20000 - jihor
我该如何更改Maven项目的超时时间? - das
你有没有关于如何添加超时并解决这个问题的Github链接? - Teja Swaroop

1

您可以使用应用程序.yaml文件上的配置属性来配置超时时间:

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

请注意,这将更改您的默认Feign配置。如果您只想为客户端更新超时时间,请将default替换为在@FeignClient中配置的名称,在您的情况下将是client。另外一件事是,您必须同时指定connectTimeoutreadTimeout才能生效。
有关更多详细信息,请参见:documentation

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接