spring-cloud服務閘道器中的Timeout設定
大家在初次使用spring-cloud的gateway的時候,肯定會被裡面各種的Timeout搞得暈頭轉向。hytrix有設定,ribbon也有。我們一開始也是亂設一桶,Github上各種專案裡也沒幾個設定正確的。對Timeout的研究源於一次log中的warning
The Hystrix timeout of 60000 ms for the command “foo” is set lower than the combination of the Ribbon read and connect timeout, 200000ms.
hytrix超時時間
log出自AbstractRibbonCommand.java
假設:
- 這裡gateway會請求一個serviceName=foo的服務
|
|
緊接著,看一下我們的配置是什麼
|
|
ribbon超時時間
這裡ribbon的超時時間是50000ms,那麼為什麼log中寫的ribbon時間是200000ms?
繼續分析原始碼:
|
|
可以看到ribbonTimeout是一個總時間,所以從邏輯上來講,作者希望hystrixTimeout要大於ribbonTimeout,否則hystrix熔斷了以後,ribbon的重試就都沒有意義了。
ribbon單服務設定
到這裡最前面的疑問已經解開了,但是hytrix可以分服務設定timeout,ribbon可不可以? 原始碼走起,這裡看的檔案是DefaultClientConfigImpl.java
|
|
以我們的服務為例:getConfigKey(key)
returns foo.ribbon.ReadTimeout
getDefaultPropName(key)
returns ribbon.ReadTimeout
一目瞭然,{serviceName}.ribbon.{propertyName}
就可以了。
小結
感覺ribbon和hytrix的配置獲取原始碼略微有點亂,所以也導致大家在設定的時候有些無所適從。spring-cloud
的程式碼一直在迭代,無論github上還是文件可能都相對滯後,這時候閱讀原始碼並且動手debug一下是最能接近事實真相的了。