測試訪問時長
修改下業務類,增加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