【发布时间】:2020-11-27 15:43:42
【问题描述】:
可通过 GitHub 上的项目重现:spring-cloud-feign-hystrix-timeout-problem
我正在使用 Spring Boot 2.3.1.RELEASE 和 Spring Cloud Hoxton.SR6。即没有 Zuul 和 Eureka 的 Feign 客户端和 Hystrix 来获取 REST 响应。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
然后我在 Spring Boot 2.3.1.RELEASE 和 Spring Cloud Hoxton.SR6 之上使用以下依赖项:
-
org.springframework.boot:spring-boot-starter -
org.springframework.boot:spring-boot-starter-web -
org.springframework.cloud:spring-cloud-starter-openfeign -
org.springframework.cloud:spring-cloud-starter-netflix-hystrix
我启用 @EnableFeignClients 和 @EnableCircuitBreaker 并使用带有简单回退的 @FeignClient 来记录并重新抛出异常:
@FeignClient(name="my-feign", url = "${feign.url}", fallbackFactory = MyFallbackFactory.class) {
public interface MyFeignClient {
@PostMapping(value = "/api/dto")
postDto(@RequestBody Dto dto);
}
使用以下application.yml,超时时间约为 1 秒,因为 Hystrix 默认使用相同的值:
feign:
hystrix:
enabled: true
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
11:52:05.493 INFO 10200 --- [nio-8060-exec-2] com.mycompany.rest.MyController:现在调用 REST!
11:52:06.538 ERROR 24044 --- [nio-8060-exec-1] oaccC[.[.[/].[dispatcherServlet] :Servlet.service() 用于 servlet [dispatcherServlet] 在上下文中的路径 [ ] 抛出异常 [请求处理失败;嵌套异常是 com.netflix.hystrix.exception.HystrixRuntimeException: MyFeignClient#postDto(Dto) timed-out and fallback failed.] 根本原因
我尝试了什么?
只要我添加以下几行将超时时间增加到 60 秒,超时时间就会有效地变为 2 秒:
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 60000
11:53:33.590 INFO 16052 --- [nio-8060-exec-2] com.mycompany.rest.MyController:现在调用 REST!
11:53:35.614 ERROR 16052 --- [nio-8060-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() 用于 servlet [dispatcherServlet] 在上下文中的路径 [ ] 抛出异常 [请求处理失败;嵌套异常是 com.netflix.hystrix.exception.HystrixRuntimeException: MyFeignClient#postDto(Dto) failed and fallback failed.] 根本原因
只要增加了 Hystrix 读取/连接超时,调用就会在 2 秒 内立即回退。但是,只要我在 feign.client.config.default... 超时中声明它,我希望达到 5 秒。我觉得我缺少另一个配置。
问:如何增加超时时间?
编辑:
-
mvn dependency:tree: https://pastebin.com/LJFGaMTc -
pom.xml: https://pastebin.com/98uXHTaR - 堆栈跟踪:https://pastebin.com/7rQweC8w
【问题讨论】:
-
我不知道这是否有帮助,请您最后检查一下这个文件中的 feign config gitlab.com/spring-boot-cloud-samples/…
-
@silentsudo:谢谢!
feign.client.config.default...适用于所有服务,我没有进一步的配置。我也不使用 Resilience4j,而是使用 Hystrix。我的问题是否遵循最小且可重复的示例? -
任何反对的理由?缺少什么?
标签: java spring-boot spring-cloud hystrix spring-cloud-feign