springcloud之RestTemplate、Ribbon、Feign之间的超时和重试及关系

RestTemplate和Riboon超时关系

在SpringCloud项目中,RestTemplate一般通过负责均匀来使用,其超时时间不能通过ribbon直接来设置,这样设置是无效的。如:

#ribbon的超时时间
ribbon:
  ReadTimeout: 3000 #读取超时时间
  ConnectTimeout: 3000  #连接超时时间

restTemplate的超时时间可以在具体的客户端指定(java原生、apache客户端、netty、okhttp),这里用Java原生的实现来举例。

@Bean
  public RestTemplate getRestTemplate(){
  
    //default
    SimpleClientHttpRequestFactory requestFactory =
     new SimpleClientHttpRequestFactory();
    requestFactory.setReadTimeout(15000);//读取超时
    requestFactory.setConnectTimeout(5000);//连接超时
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    return restTemplate;
  }

更多的客户端实现参考
HTTP协议参考

Feign超时时间 和重试

使用Feign调用接口分两层,ribbon的调用和hystrix的调用,所以ribbon的超时时间和Hystrix的超时时间的结合就是Feign的超时时间。

#hystrix 生效标志,不同的springcloud版本可能配置不同
feign:
  hystrix:
    enabled: true
#hystrix的超时时间
hystrix:
    command:
        default:
            execution:
              timeout:
                enabled: true
              isolation:
                    thread:
                        timeoutInMilliseconds: 9000
#ribbon的超时时间
ribbon:
  ReadTimeout: 3000
  ConnectTimeout: 3000

涉及到ribbon的重试机制,一般情况下,ribbon 的超时时间(<)hystrix的超时时间。
因为ribbon的重试机制和Feign的重试机制有冲突,所以源码中默认关闭Feign的重试机制,源码如下(这里以springcloud版本:Edgware.SR5为例):
FeignClientsConfiguration类相关的配置代码

@Configuration
public class FeignClientsConfiguration {
... 省略部分代码
/**
这里是初始化重试的实现类,Retryer.NEVER_RETRY 默认feign不进行重试机制
*/
@Bean
  @ConditionalOnMissingBean
  public Retryer feignRetryer() {
    return Retryer.NEVER_RETRY;
  }

 ... 代码省略
 }

public interface Retryer extends Cloneable {
//Retryer 的不进行重试的实现类
  Retryer NEVER_RETRY = new Retryer() {
    public void continueOrPropagate(RetryableException e) {
      throw e;
    }

    public Retryer clone() {
      return this;
    }
  };

  void continueOrPropagate(RetryableException var1);

  Retryer clone();

// Retryer 重试的实现类,默认重试5次
  public static class Default implements Retryer {
    private final int maxAttempts;
    private final long period;
    private final long maxPeriod;
    int attempt;
    long sleptForMillis;

    public Default() {
      this(100L, TimeUnit.SECONDS.toMillis(1L), 5);
    }

    public Default(long period, long maxPeriod, int maxAttempts) {
      this.period = period;
      this.maxPeriod = maxPeriod;
      this.maxAttempts = maxAttempts;
      this.attempt = 1;
    }
  }
}

feign默认关闭了重试,使用了 Retryer NEVER_RETRY = new Retryer()的实现。
如若开启feign重试:

@Bean
Retryer feignRetryer() {
return new Retryer.Default();
}

ribbon重试机制

ribbon:
  # http建立socket超时时间,毫秒
  ConnectTimeout: 2000
  # http读取响应socket超时时间
  ReadTimeout: 10000
  # 同一台实例最大重试次数,不包括首次调用
  MaxAutoRetries: 1
  # 重试负载均衡其他的实例最大重试次数,不包括首次server
  MaxAutoRetriesNextServer: 1
  # 是否所有操作都重试,POST请求注意多次提交错误。
  # 默认false,设定为false的话,只有get请求会重试
  OkToRetryOnAllOperations: true

在feign开启的情况下:
ribbon重试计算公式:MaxAutoRetries+MaxAutoRetriesNextServer+(MaxAutoRetries *MaxAutoRetriesNextServer) ,即重试3次 (不包括首次调用)一共产生4次调用。
如果在重试期间,时间超过了hystrix的超时时间,便会立即执行熔断,fallback。根据上面配置的参数计算hystrix的超时时间>=ReadTimeout * 重试次数,不然重试机制就会没有意义 。
hystrix超时时间的计算: (1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout 即按照以上的配置 hystrix的超时时间应该配置为 (1+1+1)*10=30秒。
当ribbon超时后且hystrix没有超时,便会采取重试机制。当OkToRetryOnAllOperations设置为false时,只会对get请求进行重试。如果设置为true,便会对所有的请求进行重试,如果是put或post等写操作,如果服务器接口没做幂等性,会产生不好的结果,所以OkToRetryOnAllOperations慎用。
如果不配置ribbon的重试次数,默认会重试一次

注意:
1、默认情况下,GET方式请求无论是连接异常还是读取异常,都会进行重试
非GET方式请求,只有连接异常时,才会进行重试。
2、feign肯定会用到ribbon,建议在配置中关闭feign的Hystrix重试和ribbon重试机制,避免产生不良的影响。

参考:ribbon
ribbon 重试源码
feign 解析

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值