先看下我的配置:
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..execution.timeout.enabled: false # 全局设置超时时间为60秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000# 设置某一个微服务的超时时间为60秒
hystrix.command..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.-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.-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的超时配置说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。