Spring 使用 feign 和 hystrix 启动:无法让请求超时正常工作
Spring boot with feign and hystrix: Can't get request timeouts to work
我在让 hystrix 超时工作时遇到问题。我创建了一个示例项目以在此处显示:https://github.com/stianlagstad/spring-boot-timeout-demo.
在 bootstrap.yml
中,我设置了这样的超时:
hystrix:
command:
default:
execution.isolation.thread.timeoutInMilliseconds: 60000
circuitBreaker:
enabled: true
sleepWindowInMilliseconds: 300000
fallback.enabled: false
# My client
MyFeignClient#getPost:
execution.isolation.thread.timeoutInMilliseconds: 1
我希望这样做的结果是 hystrix 命令应该在 60 秒后超时,除了 MyFeignClient
中的 getPost
应该在 1 毫秒后超时。不过,我没有看到。 getPost
方法 returns 每次都有一个答案,我很确定它需要超过一毫秒的时间。
我也尝试在测试中使用 ConfigurationManager
手动设置超时,但这似乎也不起作用:https://github.com/stianlagstad/spring-boot-timeout-demo/blob/master/src/test/java/com/example/TimeoutDemoApplicationTests.java
如何使我设置的超时生效?
您需要在两个地方修复您的属性。
首先,添加以下 属性。从 Dalston 版本开始,Feign 的 Hystrix 支持是可选的。你的类路径上已经有 hystrix,所以你需要做的就是添加下面的 属性.
feign:
hystrix:
enabled: true
其次,你为你的伪装指定了错误的HystrixCommandKey
。您需要像下面这样更改您的 HystrixCommandKey。
MyFeignClient#getPost():
#getPost
后需要括号。
我在让 hystrix 超时工作时遇到问题。我创建了一个示例项目以在此处显示:https://github.com/stianlagstad/spring-boot-timeout-demo.
在 bootstrap.yml
中,我设置了这样的超时:
hystrix:
command:
default:
execution.isolation.thread.timeoutInMilliseconds: 60000
circuitBreaker:
enabled: true
sleepWindowInMilliseconds: 300000
fallback.enabled: false
# My client
MyFeignClient#getPost:
execution.isolation.thread.timeoutInMilliseconds: 1
我希望这样做的结果是 hystrix 命令应该在 60 秒后超时,除了 MyFeignClient
中的 getPost
应该在 1 毫秒后超时。不过,我没有看到。 getPost
方法 returns 每次都有一个答案,我很确定它需要超过一毫秒的时间。
我也尝试在测试中使用 ConfigurationManager
手动设置超时,但这似乎也不起作用:https://github.com/stianlagstad/spring-boot-timeout-demo/blob/master/src/test/java/com/example/TimeoutDemoApplicationTests.java
如何使我设置的超时生效?
您需要在两个地方修复您的属性。
首先,添加以下 属性。从 Dalston 版本开始,Feign 的 Hystrix 支持是可选的。你的类路径上已经有 hystrix,所以你需要做的就是添加下面的 属性.
feign:
hystrix:
enabled: true
其次,你为你的伪装指定了错误的HystrixCommandKey
。您需要像下面这样更改您的 HystrixCommandKey。
MyFeignClient#getPost():
#getPost
后需要括号。