Spring Cloud Zuul模塊本身就包含了對於hystrix和ribbon的依賴,當我們使用zuul通過path和serviceId的組合來配置路由的時候,可以通過hystrix和ribbon的配置調整路由請求的各種時間超時機制。
1 ribbon配置舉例
配置連接超時時間1秒,請求處理時間2秒,統一服務server嘗試重連1次,切換server重連1次
ribbon: ConnectTimeout: 1000 ReadTimeout: 2000 MaxAutoRetries: 1 MaxAutoRetriesNextServer: 1
2 hystirx配置舉例
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000
這裡需要注意的是hystrix的配置時間應該大於ribbon全部重試時間的總和,上面我配置的是2次重試,包括首次請求,三次時間是6秒
引用官方大神的一段說明
When using Hystrix commands that wrap Ribbon clients you want to make sure your Hystrix timeout is configured to be longer than the configured Ribbon timeout, including any potential
retries that might be made. For example, if your Ribbon connection timeout is one second and
the Ribbon client might retry the request three times, than your Hystrix timeout should
be slightly more than three seconds.
3 打開zuul的重試配置:
zuul: retryable: true
特別注意zuul的重試配置需要依賴spring的retry,不然的話怎麼配置都是徒勞
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId></dependency>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。