测试访问时长
修改下业务类,增加sleep休眠时长,以此查看Zuul的熔断
@GetMapping("/test1") public Object test1() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return "dbToEs"; } @GetMapping("/test2") public Object test2() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "dbToEs"; } @GetMapping("/test3") public Object test3() { try { Thread.sleep(5500); } catch (InterruptedException e) { e.printStackTrace(); } return "dbToEs"; }
大概执行2秒多,然后还没执行完,zuul就执行熔断了。
报错信息
com.netflix.zuul.exception.ZuulException:Forwarding error
Caused by: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
Caused by: java.net.SocketTimeoutException: Read timed out
很明显,根据报错信息,应该是zuul的调用等待时间超时
解决办法
如果路由方式是serviceId的方式,配置ribbon生效,如果是url的方式,则配置zuul.host生效。(此处重要!使用serviceId路由和url路由是不一样的超时策略)
如果你在zuul配置了熔断(fallback)的话,hystrix熔断超时也要配置,不然如果你配置的ribbon超时时间大于hystrix熔断的超时,那么会先走hystrix熔断,相当于你配的ribbon超时就不生效了。
以下是两种配置文件的方式,可根据需要选取配置。
配置application.yml文件
hystrix:
command:
my-userService:
execution:
isolation:
thread:
timeoutInMilliseconds:6000
ribbon:
NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList #可不写
ListOfServers:http://example1.com,http://example2.com #可不写 (负载配置)
ConnectTimeout:10000
ReadTimeout:3000
MaxTotalHttpConnections:5000
MaxConnectionsPerHost:1000
zuul:
max:
host:
connections: 10000
host:
socket-timeout-millis: 6000
connect-timeout-millis: 6000
配置application.properties文件
hystrix.command.eureka-consumer.execution.isolation.thread.timeoutInMilliseconds=10000
#ribbon.eureka.enabled= false #这一行我是注掉的,因为在我项目内报错,主要看大家的项目
ribbon.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList #可不写
ribbon.ListOfServers=http://example1.com,http://example2.com #可不写(负载配置)
ribbon.ReadTimeout=8000
ribbon.ConnectTimeout=10000
ribbon.SocketTimeout=8000
zuul.max.host.connections=10000
zuul.host.socket-timeout-millis=6000
zuul.host.connect-timeout-millis=6000
文章转载至:https://blog.csdn.net/tianyaleixiaowu/article/details/78772269、https://www.cnblogs.com/dauber/p/9424505.html