Spring Cloud Zuul的重試配置

Spring Cloud Zuul模塊自己就包含了對於hystrix和ribbon的依賴,當咱們使用zuul經過path和serviceId的組合來配置路由的時候,能夠經過hystrix和ribbon的配置調整路由請求的各類時間超時機制。java

  • 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秒spring

引用官方大神的一段說明code

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,否則的話怎麼配置都是徒勞server

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
相關文章
相關標籤/搜索