繁体   English   中英

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

[英]How to set custom Feign client connection timeout?

我有具有此 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 秒。

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM