首页 > 技术文章 > SpringCloud(七)超时、重试

applesnt 2020-04-01 16:07 原文

一、Ribbon(单独配置)

可以通过ribbon.xx来进行全局配置。也可以通过服务名.ribbon.xx来对指定服务配置
全局配置:

ribbon:
  ConnectTimeout: 3000 #连接超时时间(ms)
  ReadTimeout: 3000 #通信超时时间(ms)
  MaxAutoRetries: 1 #同一台实例最大重试次数,不包括首次调用
  MaxAutoRetriesNextServer: 1 #重试负载均衡其他的实例最大重试次数,不包括首次调用
  OkToRetryOnAllOperations: false  #是否所有操作都重试 

服务配置:

#某个服务设置 
mhb-cloud-zuul-consumer
  ribbon:
    ConnectTimeout: 3000 #连接超时时间(ms)
    ReadTimeout: 3000 #通信超时时间(ms)
    MaxAutoRetries: 1 #同一台实例最大重试次数,不包括首次调用
    MaxAutoRetriesNextServer: 1 #重试负载均衡其他的实例最大重试次数,不包括首次调用
    OkToRetryOnAllOperations: false  #是否所有操作都重试

根据上面的参数计算重试的次数 -- 重试3次 则一共产生4次调用:
MaxAutoRetries+MaxAutoRetriesNextServer+(MaxAutoRetries *MaxAutoRetriesNextServer)

二、Ribbon+Feign

Feign默认支持Ribbon;Ribbon的重试机制和Feign的重试机制有冲突,所以源码中默认关闭Feign的重试机制,使用Ribbon的重试机制

二、Ribbon+Feign+Hystrix

设置Hystrix的超时时间,要先设置Ribbon的超时时间
Hystrix超时时间的计算方式为:
(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout

ribbon:
  ConnectTimeout: 3000 #连接超时时间(ms)
  ReadTimeout: 3000 #通信超时时间(ms)
  MaxAutoRetries: 1 #同一台实例最大重试次数,不包括首次调用
  MaxAutoRetriesNextServer: 1 #重试负载均衡其他的实例最大重试次数,不包括首次调用
  OkToRetryOnAllOperations: false  #是否所有操作都重试

#hystrix的超时时间
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
            isolation:
              thread:
                timeoutInMilliseconds: 9000 #超时时间(ms)

推荐阅读