我也思考这个问题, 测试经常给我提bug, 还有前端一直问我请求不成功, 第一和第二次都有可能出现这个问题,接下来就没有这个问题,上网看了一下问题,
参考了Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题?
解决方案是最后那个, 然后在调用方服务yml增加下面的配置(A调用B, 就在A配置):
ribbon: eager-load: enabled: true clients: content-service, product-service
启动的时候日志:
2019-06-02 11:24:26.874 INFO 15264 --- [ main] com.netflix.loadbalancer.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client content-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=content-service,current list of Servers=[169.254.71.137:9021],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:169.254.71.137:9021; 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@153409b8
2019-06-02 11:24:26.914 INFO 15264 --- [ main] org.springframework.context.annotation.AnnotationConfigApplicationContext : Refreshing SpringClientFactory-product-service: startup date [Sun Jun 02 11:24:26 CST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@a10c1b5
2019-06-02 11:24:26.952 INFO 15264 --- [ main] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-06-02 11:24:27.012 INFO 15264 --- [ main] com.netflix.config.ChainedDynamicProperty : Flipping property: product-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-02 11:24:27.020 INFO 15264 --- [ main] com.netflix.util.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-product-service
2019-06-02 11:24:27.021 INFO 15264 --- [ main] com.netflix.loadbalancer.BaseLoadBalancer : Client: product-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=product-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-06-02 11:24:27.022 INFO 15264 --- [ main] com.netflix.loadbalancer.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2019-06-02 11:24:27.024 INFO 15264 --- [ main] com.netflix.config.ChainedDynamicProperty : Flipping property: product-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-02 11:24:27.025 INFO 15264 --- [ main] com.netflix.loadbalancer.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client product-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=product-service,current list of Servers=[169.254.71.137:9011],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:169.254.71.137:9011; 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@63ad5fe7
上面日志说明了A找到B了, 问题就解决了
2. 第二问题使用zuul和feign会出现:
Caused by: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
at rx.exceptions.Exceptions.propagate(Exceptions.java:58)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:464)
at rx.observables.BlockingObservable.single(BlockingObservable.java:341)
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112)
... 131 common frames omitted
Caused by: java.net.SocketTimeoutException: Read timed out
超时问题, 问题出在哪里?有可能是熔断超时时间默认时间是1s, 由于是第一次, feign要找到client, 接着还要建立链接, 最后获取数据返回, 所以时间会超过1s, 报了上面的超时, 解决方案如下:
spring:
#重试
cloud:
loadbalancer:
retry:
enabled: true
ribbon:
# 这个是解决找不到服务
eager-load:
enabled: true
clients: content-service, product-service
# 下面是解决超时的问题(下面的时间根据业务微调)
ReadTimeout: 8000
ConnectTimeout: 8000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000