springcloud-Finchley版本 feign & zuul retry设置

说明

网上很多配置感觉有点乱,版本没有具体说明,我这里是针对我自己使用的Finchley版本进行的配置。

Feign Retry 设置

feign.retryer的超时设置优先级>ribbon超时间设置,而且ribbon的超时时间设置必须要小于hystrix的熔断时间。
feign里ribbon超时设置只有ReadTimeout、ConnectTimeout、SocketTimeout有效,而maxAutoRetries 、maxAutoRetriesNextServer 是无效的
超时连接次数是通过feign.Retryer.Default对象里面的maxAttempts参数进行设置的。

关闭Retry设置

  • 创建Retryer对象,设置为NEVER_RETRY
    @Bean
    public Retryer feignRetryer() {
        return Retryer.NEVER_RETRY;
   }

开起Retry设置

  • yml配置
feign:
  hystrix:
    enabled: true #开起hystrix的熔断
hystrix:
  command:
      default:
        execution:
          isolation:
            thread:
              timeoutInMilliseconds: 120000 #断路切换超时
ribbon:
  eureka:
    enable: true #eureka配置请忽略
  eager-load:
    enabled: true #开起饥饿加载,解决第一次请求超时问题
    clients: service-constant,service-flightcount,service-flightinfo,service-flowcontrol,service-statistic
  ReadTimeout: 30000
  ConnectTimeout: 10000
  SocketTimeout: 10000
  OkToRetryOnAllOperations: true #是否重试所有请求,因为我的请求有post所以要开启,如果只有get,最好设置为false,否则要考虑post请求幂等特性,博主这里请求参数太长,使用了post请求
  • Java 对象配置,可以写在yml配置文件中,但是个人还是习惯创建config bean对象,主要设置请求连接数之类的
	//请求连接超时时间设置,设置了这个以后会覆盖掉ribbon本身的超时设置
	@Bean
	public Request.Options options() {
	    return new Request.Options(3000, 3000);
	}
	//开起重试,重试间隔,最大重试间隔,重试次数
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, 1000,3);
    }
  • 重试间隔时间计算公式
   long nextMaxInterval() {
       long interval = (long)((double)this.period * Math.pow(1.5D, (double)(this.attempt - 1)));
       return interval > this.maxPeriod ? this.maxPeriod : interval;
   }

Zuul Retry设置

zuul的重试默认是关闭的,因此这里只有开启方面的设置
zuul的ribbon超时时间和feign略有不同
ReadTimeout、ConnectTimeout、SocketTimeout、maxAutoRetries 、maxAutoRetriesNextServer都有效
ribbon超时时间 = (ReadTimeout + ConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);
但是同样的,ribbon的超时时间需要小于hystrix的熔断时间。
  • yml配置
zuul:
  retryable: true #开启路由重试
  ribbon:
    eager-load:
      enabled: true #开启饥饿加载,解决第一次请求超时问题
  #路由超时设置 URL方式超时设置
  host:
    connect-timeout-millis: 10000
    socket-timeout-millis: 60000
      
#路由超时设置 serviceId方式超时设置
ribbon:
  ReadTimeout: 10000
  SocketTimeout: 5000
  ConnectTimeout: 5000
  MaxAutoRetries: 2
  MaxAutoRetriesNextServer: 0
  OkToRetryOnAllOperations: true #这里需要开启所有操作重试,否则post请求的重试会失败,只会进行get请求的重试
  
hystrix:
  command:
      default:
        execution:
          isolation:
            thread:
              timeoutInMilliseconds: 60000 #断路切换超时
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值