spring cloud alibaba 学习(十一)feign 重试策略 Retryer


前言

请求远程接口失败时,会进入到重试策略。

一、Retryer

1、Retryer 接口

public interface Retryer extends Cloneable {

  /**
   * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
   * 在休眠后执行重试,或抛出异常
   */
  void continueOrPropagate(RetryableException e);

  Retryer clone();

}

2、默认不进行重试 NEVER_RETRY

Retryer NEVER_RETRY = new Retryer() {

    @Override
    public void continueOrPropagate(RetryableException e) {
      //直接抛出异常
      throw e;
    }

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

在 FeignClientsConfiguration 中 定义了默认的重试策略

	@Bean
	@ConditionalOnMissingBean
	public Retryer feignRetryer() {
		return Retryer.NEVER_RETRY;
	}

二、Default

feign 里定义的默认重试策略

class Default implements Retryer {
	
	//最大重试次数
    private final int maxAttempts;
    //重试周期
    private final long period;
    //重试最大周期
    private final long maxPeriod;
    //当前重试次数
    int attempt;
    //休眠总时间
    long sleptForMillis;


	public void continueOrPropagate(RetryableException e) {
	  //达到了最大重试次数,抛异常
      if (attempt++ >= maxAttempts) {
        throw e;
      }

      long interval;
      if (e.retryAfter() != null) {
        //根据 RetryableException 里 retryAfter 计算时间间隔
        interval = e.retryAfter().getTime() - currentTimeMillis();
        //不能超过 maxPeriod
        if (interval > maxPeriod) {
          interval = maxPeriod;
        }
        //时间到了,直接返回进行重试
        if (interval < 0) {
          return;
        }
      } else {
        //计算时间间隔
        interval = nextMaxInterval();
      }
      try {
        //休眠
        Thread.sleep(interval);
      } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
        throw e;
      }
      sleptForMillis += interval;
    }

    /**
     * Calculates the time interval to a retry attempt. <br>
     * The interval increases exponentially with each attempt, at a rate of nextInterval *= 1.5
     * (where 1.5 is the backoff factor), to the maximum interval.
     *
     * @return time in nanoseconds from now until the next attempt.
     */
    long nextMaxInterval() {
      //根据当前重试次数和重试周期计算时间间隔,重试次数越多,周期越长
      long interval = (long) (period * Math.pow(1.5, attempt - 1));
      //时间间隔不能超过最大重试周期
      return interval > maxPeriod ? maxPeriod : interval;
    }
}

注册Default

@Configuration
public class CustomFeignClientsConfiguration {

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_lrs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值