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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41470321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:23:57  来源:igfitidea点击:

How to set custom Feign client connection timeout?

javahystrixfeign

提问by Roman Cherepanov

I have Spring Boot application with this Gradle dependencies:

我有具有此 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")

Also I have Feign client:

我还有 Feign 客户端:

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

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

}

My application.properties:

我的application.properties

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

When query time is more than 1 second I get exception:

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

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

So my question is:how can I set custom Feign client connection timeout? For example to 2 seconds.

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

采纳答案by Roman Cherepanov

I solved my problem using this answeron question: Hystrix command fails with “timed-out and no fallback available”.

我使用以下问题的答案解决了我的问题:Hystrix command failed with “timed-out and no fallback available”

I added hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000to my application.propertiesto set custom timeout.

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

回答by Francesco Lo Cascio

You can be configured timeout using configuration properties on application.yaml file:

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

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

Notice that this will change your default feign configuration, if you want to update the timeouts just for your client replace defaultwith the name configured in @FeignClientin your case it will be client, another thing is that you must specify both connectTimeoutand readTimeoutfor this to take effect.

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

For more detail see this: documentation

有关更多详细信息,请参阅:文档