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
How to set custom Feign client connection timeout?
提问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=4000
to my application.properties
to 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 default
with the name configured in @FeignClient
in your case it will be client
, another thing is that you must specify both connectTimeout
and readTimeout
for this to take effect.
请注意,这将更改您的默认 feign 配置,如果您只想为您的客户端更新超时,请替换default
为@FeignClient
在您的情况下配置的名称client
,另一件事是您必须同时指定两者connectTimeout
并readTimeout
使其生效。
For more detail see this: documentation
有关更多详细信息,请参阅:文档