项目中出现了设置了hystrix超时时间,依旧很快就发生了超时异常的情况。
hystrix的隔离级别设置成了信号量
You can use semaphores (or counters) to limit the number of concurrent calls to any given dependency, instead of using thread pool/queue sizes. This allows Hystrix to shed load without using thread pools but it does not allow for timing out and walking away. If you trust the client and you only want load shedding, you could use this approach.
官方文档里面指出使用信号量,无法设置超时。
而我们的项目中有出现,存在超时时间和信号量同时配置的情况。这个时候配置超时时间是不起效的。
未设置ribbon超时时间
设置了隔离隔离策略是SEMAPHORE
,还是出现了超时现象。

经过排查是ribbon超时。ribbon.ConnectTimeout
和 ribbon.ReadTimeout
默认超时时间都只有1000毫秒。
// ribbon的超时时间计算
ribbonTimeout = (ribbon.ConnectTimeout + ribbon.ReadTimeout) * (ribbon.MaxAutoRetries + 1) * (ribbon.MaxAutoRetriesNextServer + 1)
hystrixTimeout要大于ribbonTimeout,否则hystrix
熔断了以后,ribbon
的重试就都没有意义了。而当feign设置了超时时间,Ribbon
会依据feign
的connectTimeout
设置同步。
所以正确的超时时间设置应该是:
- 同时设置ribbon和hystrix的超时时间,且
hystrixTimeout > ribbonTimeout
。 - 或者同时设置feign和hystrix的超时时间,且
feignConnectTimeout > ribbonTimeout
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。