避坑指南(六):Hystrix超时时间小于ribbon超时时间报错

​问题

zuul配置如下:

server:
  port: 5566
​
zuul:
  prefix: /gateway
  sensitive-headers:
  routes:
    auth:
      path: /auth/**
      service-id: xmall-auth
      strip-prefix: true
    product:
      path: /product/**
      service-id: xmall-product
      strip-prefix: true
  ribbon-isolation-strategy: thread
​
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 70000
​
​
# ribbon配置
ribbon:
  MaxConnectionsPerHost: 300
  MaxTotalConnections: 1000
  ConnectTimeout: 60000
  ReadTimeout: 60000
#  MaxAutoRetries: 1
#  MaxAutoRetriesNextServer: 1
​
management:
  endpoints:
    web:
      exposure:
        include: '*'

然而,当系统启动,或者访问zuul的时候,总会报如下错误:

The Hystrix timeout of 70000ms for the command xxx is set lower than the combination of the Ribbon read and connect time, 240000ms。

 

分析

Ribbon 总超时时间计算公式如下:

RibbonTimeout = (RibbonReadTimeout + RibbonConnectTimeout) × (MaxAutoRetries + 1) × (MaxAutoRetriesNextServer + 1)

如问题中配置,那么Ribbon 总超时时间为:

RibbonTimeout = (60000 + 60000) × (0 + 1) × (1 + 1) = 240000ms。

此值,与错误信息中的240000ms吻合。

不巧的是,Hystrix 超时时间为 70000ms,小于Ribbon的超时时间240000ms。从逻辑上来说,HystrixTimeout 必须要大于 RibbonTimeout。因为 Hystrix 一旦开启熔断,Ribbon 的重试便没有任何意义了。

 

解决

解决方案很简单,Hystrix超时时间配置的比Ribbon超时时间大即可,也可以调低Ribbon的ReadTime、Connect Time、重试次数等参数。

server:
  port: 5566
​
zuul:
  prefix: /gateway
  sensitive-headers:
  routes:
    auth:
      path: /auth/**
      service-id: xmall-auth
      strip-prefix: true
    product:
      path: /product/**
      service-id: xmall-product
      strip-prefix: true
  ribbon-isolation-strategy: thread
​
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 300000
​
​
# ribbon配置
ribbon:
  MaxConnectionsPerHost: 300
  MaxTotalConnections: 1000
  ConnectTimeout: 60000
  ReadTimeout: 60000
#  MaxAutoRetries: 1
#  MaxAutoRetriesNextServer: 1
​
management:
  endpoints:
    web:
      exposure:
        include: '*'

将Hystrix超时时间配置为300000ms,大于Ribbon的超时时间240000ms。

 

 

本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。

微信搜索【银河架构师】,发现更多精彩内容。

避坑指南(六):Hystrix超时时间小于ribbon超时时间报错_第1张图片

 

你可能感兴趣的:(Spring,Cloud)