feign的ribbon超時配置和hystrix的超時配置說明
先看下我的配置:
ribbon:
MaxAutoRetries: 1 #最大重試次數,當Eureka中可以找到服務,但是服務連不上時將會重試
MaxAutoRetriesNextServer: 1 #切換例項的重試次數
OkToRetryOnAllOperations: false # 對所有的操作請求都進行重試,如果是get則可以,如果是post,put等操作沒有實現冪等的情況下是很危險的,所以設定為false
ConnectTimeout: 1000 #請求連線的超時時間
ReadTimeout: 1800 #請求處理的超時時間
hystrix:
command:
default:execution:
isolation:
thread:
timeoutInMilliseconds: 3000
#如果配置ribbon的重試,hystrix的超時時間要大於ribbon的超時時間,ribbon才會重試
#hystrix的超時時間=(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout 比較好,具體看需求
Ribbon超時與Hystrix超時問題,為了確保Ribbon重試的時候不被熔斷,我們就需要讓Hystrix的超時時間大於Ribbon的超時時間,否則Hystrix命令超時後,該命令直接熔斷,重試機制就沒有任何意義了。
從上面的配置來說,ribbon超時配置為1800,請求超時後,該例項會重試1次,更新例項會重試1次。
所以hystrix的超時時間要大於 (1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout 比較好,具體看需求進行配置。
Ribbon超時與Hystrix超時問題,為了確保Ribbon重試的時候不被熔斷,我們就需要讓Hystrix的超時時間大於Ribbon的超時時間,否則Hystrix命令超時後,該命令直接熔斷,重試機制就沒有任何意義了。
補充知識:Spring Cloud Feign 第一次呼叫超時(Read timed out)的解決辦法
feign-client在第一次呼叫微服務會出現Read timed out異常,提示的報錯資訊:
java.net.SocketTimeoutException: Read timed out
這是由於在呼叫其他微服務介面前,會去請求該微服務的相關資訊(地址、埠等),並做一些初始化操作,由於預設的懶載入特性,導致了在第一次呼叫時,出現超時的情況,解決方法主要有兩種:
第一種辦法是設定超時時間,具體設定成多少,因專案而異,配置如下:
# 全域性關閉Hystrix超時,對所有微服務有效
hystrix.command.default.execution.timeout.enabled: false# 關閉某一個微服務的超時
hystrix.command.<serviceName>.execution.timeout.enabled: false# 全域性設定超時時間為60秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000# 設定某一個微服務的超時時間為60秒
hystrix.command.<serviceName>.execution.isolation.thread.timeoutInMilliseconds: 60000# 設定ribbon超時時間
ribbon:
ReadTimeout: 20000
ConnectTimeout: 20000
第二種辦法,也是比較推薦的方式,配置ribbon立即載入,此處需要注意的是,光配置立即載入是不生效的,還要配置客戶端列表,配置如下,詳細配置請點選:
ribbon:
eager-load:
enabled: true
clients: distribution,material-product,outer-data
最後附上未開啟立即載入時,第一次呼叫列印的日誌:
2019-05-27 18:05:33.819|INFO|1986|http-nio-18884-exec-2|clipcloud-configuration|3752c9412075144e|3752c9412075144e|false|com.migu.clipcloud.configuration.ConfigurationServiceApplication.test1-55|time:2019-05-27 18:05:33.819
2019-05-27 18:05:33.826|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.migu.clipcloud.services.client.FeignHeadConfiguration.lambda$requestInterceptor$2-51|add request header for feignclient,key:cookie,value:JSESSIONID.8250e395=node08z98wvylx12lwh89c6v09qk92.node0; screenResolution=1440x900; m=2258:cmFiYml0bXE6cmFiYml0bXE%253D
2019-05-27 18:05:33.828|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|org.springframework.context.support.AbstractApplicationContext.prepareRefresh-588|Refreshing SpringClientFactory-distribution: startup date [Mon May 27 18:05:33 CST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@203dd56b
2019-05-27 18:05:33.850|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.<init>-153|JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-05-27 18:05:33.864|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.config.ChainedDynamicProperty$ChainLink.checkAndFlip-115|Flipping property: distribution.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-05-27 18:05:33.865|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.util.concurrent.ShutdownEnabledTimer.<init>-58|Shutdown hook installed for: NFLoadBalancer-PingTimer-distribution
2019-05-27 18:05:33.866|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.loadbalancer.BaseLoadBalancer.initWithConfig-192|Client: distribution instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=distribution,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-05-27 18:05:33.884|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.loadbalancer.DynamicServerListLoadBalancer.enableAndInitLearnNewServersFeature-222|Using serverListUpdater PollingServerListUpdater
2019-05-27 18:05:33.886|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.config.ChainedDynamicProperty$ChainLink.checkAndFlip-115|Flipping property: distribution.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-05-27 18:05:33.887|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|com.netflix.loadbalancer.DynamicServerListLoadBalancer.restOfInit-150|DynamicServerListLoadBalancer for client distribution initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=distribution,current list of Servers=[192.168.26.103:18887],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:192.168.26.103:18887; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@173441c2
2019-05-27 18:05:33.906|INFO|1986|http-nio-18884-exec-2|clipcloud-configuration|3752c9412075144e|3752c9412075144e|false|com.migu.clipcloud.configuration.ConfigurationServiceApplication.test1-57|time:2019-05-27 18:05:33.906
2019-05-27 18:05:34.888|INFO|1986|PollingServerListUpdater-0|clipcloud-configuration||||com.netflix.config.ChainedDynamicProperty$ChainLink.checkAndFlip-115|Flipping property: distribution.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
以上這篇feign的ribbon超時配置和hystrix的超時配置說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。