Spring Cloud Zuul的重试配置

本文介绍了如何在SpringCloud Zuul中配置Hystrix和Ribbon以实现路由请求的时间超时机制,并提供了具体的配置示例。同时,还强调了正确配置Hystrix超时时间的重要性,确保其大于Ribbon的所有重试时间总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值