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

时间:2017-01-04 17:57:23

标签: java hystrix feign

我有这个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 个答案:

答案 0 :(得分:16)

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

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

答案 1 :(得分:1)

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

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

请注意,这将更改您的默认伪装配置,如果您仅想为客户端更新超时,则将default@FeignClient中配置的名称替换为{{1} },另一件事是您必须同时指定clientconnectTimeout才能生效。

有关更多详细信息,请参见:documentation

相关问题