当前位置:   article > 正文

guava-retry介绍_guava retry

guava retry

guava-retrying是Google Guava库的一个扩展包,可以为任意函数调用创建可配置的重试机制。该扩展包比较简单,大约包含了10个方法和类,官网:GitHub - rholder/guava-retrying: This is a small extension to Google's Guava library to allow for the creation of configurable retrying strategies for an arbitrary function call, such as something that talks to a remote service with flaky uptime.https://github.com/rholder/guava-retrying

  1. <dependency>
  2. <groupId>com.github.rholder</groupId>
  3. <artifactId>guava-retrying</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>

看一个使用例子:

  1. Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
  2. .retryIfException()
  3. .retryIfResult(aBoolean -> Objects.equals(aBoolean, false))
  4. .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(10, TimeUnit.SECONDS, Executors.newCachedThreadPool()))
  5. .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
  6. .withStopStrategy(StopStrategies.stopAfterAttempt(5))
  7. .withRetryListener(new RetryListener() {
  8. @Override
  9. public <V> void onRetry(Attempt<V> attempt) {
  10. System.out.print("retry time=" + attempt.getAttemptNumber());
  11. }
  12. }).build();
  13. try {
  14. retryer.call(() -> {
  15. // 逻辑处理
  16. return null;
  17. });
  18. } catch (Exception e) {
  19. System.out.println("exception:" + e);
  20. }

1、常用类介绍

1)Attemp

Attemp既是一次任务重试(call),也是一次请求的结果,记录了当前请求的重试次数,是否包含异常和请求的返回值。我们可以配合监听器使用,用于记录重试过程的细节,常用的方法有如下几个:

  • getAttemptNumber(),表示准备开始第几次重试;
  • getDelaySinceFirstAttempt(),表示距离第一次重试的延迟,也就是与第一次重试的时间差,单位毫秒;
  • hasException(),表示是异常导致的重试还是正常返回;
  • hasResult(),表示是否返回了结果;因为有时候是因为返回了特定结果才进行重试;
  • getExceptionCause(),如果是异常导致的重试,那么获取具体具体的异常类型;
  • getResult(),返回重试的结果;
  • get(),如果有的话,返回重试的结果;和getResult不同的在于对异常的处理;

2)Retryer:

Retryer是最核心的类,是用于执行重试策略的类,通过RetryerBuilder类进行构造,并且RetryerBuilder负责将设置好的重试策咯添加到Retryer中,最终通过执行Retryer的核心方法call来执行重试策略(一次任务的执行是如何进行的?),call方法源码如下:

  1. public V call(Callable<V> callable) throws ExecutionException, RetryException {
  2. long startTime = System.nanoTime();
  3. for (int attemptNumber = 1; ; attemptNumber++) {
  4. Attempt<V> attempt;
  5. try {
  6. V result = attemptTimeLimiter.call(callable);//任务执行的时间限制
  7. attempt = new ResultAttempt<V>(result, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
  8. } catch (Throwable t) {
  9. attempt = new ExceptionAttempt<V>(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
  10. }
  11. for (RetryListener listener : listeners) {
  12. listener.onRetry(attempt);
  13. }
  14. // 判断是否满足重试条件,来决定是否继续等待并进行重试
  15. if (!rejectionPredicate.apply(attempt)) {
  16. return attempt.get();
  17. }
  18. if (stopStrategy.shouldStop(attempt)) {//重试停止 策略
  19. throw new RetryException(attemptNumber, attempt);
  20. } else {
  21. long sleepTime = waitStrategy.computeSleepTime(attempt);//重试等待 策略
  22. try {
  23. blockStrategy.block(sleepTime);//根据重试等待计算的时间,执行阻塞策略
  24. } catch (InterruptedException e) {
  25. // 线程中断,抛出异常
  26. Thread.currentThread().interrupt();
  27. throw new RetryException(attemptNumber, attempt);
  28. }
  29. }
  30. }
  31. }

大致流程如下:

  1. 判断是否超过任务时长限制;
  2. 执行重试listener
  3. 判断是否满足重试条件
  4. 重试停止策略;
  5. 重试等待策略;
  6. 组册策略;

3)RetryListener:

当执行call方法时,会调用监听器中的onRetry方法。重写该类的onRetry方法可以实现自定义逻辑,例如:

  1. @Slf4j
  2. public class RetryLogListener implements RetryListener {
  3. @Override
  4. public <V> void onRetry(Attempt<V> attempt) {
  5. // 第几次重试,(注意:第一次重试其实是第一次调用)
  6. log.info("retry time : [{}]", attempt.getAttemptNumber());
  7. // 距离第一次重试的延迟
  8. log.info("retry delay : [{}]", attempt.getDelaySinceFirstAttempt());
  9. // 重试结果: 是异常终止, 还是正常返回
  10. log.info("hasException={}", attempt.hasException());
  11. log.info("hasResult={}", attempt.hasResult());
  12. // 是什么原因导致异常
  13. if (attempt.hasException()) {
  14. log.info("causeBy={}" , attempt.getExceptionCause().toString());
  15. } else {
  16. // 正常返回时的结果
  17. log.info("result={}" , attempt.getResult());
  18. }
  19. log.info("log listen over.");
  20. }
  21. }

2、WaitStrategies 重试等待策略

当执行失败后,用来指定不同的等待策略来进行第二、三...次的重试。

通过withWaitStrategy方法可以设置不同的重试等待策略,WaitStrategies.java中定义了很多重试等待方法,常见的有:

1)ExponentialWaitStrategy 指数等待策略

实现了指数补偿算法(Exponential Backoff),其源码根据重试次数计算等待时长,如下:

  1. @Override
  2. public long computeSleepTime(Attempt failedAttempt) {
  3. double exp = Math.pow(2, failedAttempt.getAttemptNumber());
  4. long result = Math.round(multiplier * exp);
  5. if (result > maximumWait) {
  6. result = maximumWait;
  7. }
  8. return result >= 0L ? result : 0L;
  9. }

例如:

WaitStrategies.exponentialWait() . #默认乘数multiplier是1,最大值是Long.MAX_VALUE

也可以指定乘数和最大值:

.withWaitStrategy(WaitStrategies.exponentialWait(100, 5, TimeUnit.MINUTES))

第一次失败后,依次等待时长:2^1 * 100;2^2 * 100;2^3 * 100;...直到最多5分钟。 5分钟后,每隔5分钟重试一次。

2)FibonacciWaitStrategy 斐波那契等待策略

失败后,按照斐波那契数列进行等待,例如:

WaitStrategies.fibonacciWait() #默认乘数multiplier是1,最大值是Long.MAX_VALUE

也可以指定乘数和最大值

.withWaitStrategy(WaitStrategies.fibonacciWait(100, 2, TimeUnit.MINUTES))

第一次失败后,依次等待时长:1*100;1*100;2*100;3*100;5*100;...直到最多2分钟,2分钟后每隔2分钟重试一次;

3)FixedWaitStrategy 固定时长等待策略

失败后,将等待固定的时长进行重试,例如:

withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS))

4)RandomWaitStrategy 随机时长等待策略

可以设置一个随机等待的最大时长,也可以设置一个随机等待的时长区间,例如:

withWaitStrategy(WaitStrategies.randomWait(10, TimeUnit.SECONDS));

withWaitStrategy(WaitStrategies.randomWait(1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS));

5)IncrementingWaitStrategy 递增等待策略

根据初始值和递增值,等待时长依次递增。例如:

withWaitStrategy(WaitStrategies.incrementingWait(1, TimeUnit.SECONDS, 5, TimeUnit.SECONDS))

第一次失败后,将依次等待1s;6s(1+5);11(1+5+5)s;16(1+5+5+5)s;...

6)ExceptionWaitStrategy 异常等待策略

根据所发生的异常指定重试的等待时长;如果异常不匹配,则等待时长为0;

withWaitStrategy(WaitStrategies.exceptionWait(ArithmeticException.class, e -> 1000L))

7)CompositeWaitStrategy 复合等待策略

如果所执行的程序满足一个或多个等待策略,那么等待时间为所有等待策略时间的总和。例如:

.withWaitStrategy(WaitStrategies.join(WaitStrategies.exceptionWait(ArithmeticException.class, e -> 1000L),WaitStrategies.fixedWait(5, TimeUnit.SECONDS)))

3、BlockStrategies 阻塞策略

在重试等待过程中,根据等待策略计算的时间,来阻塞。默认只提供一种阻塞策略:ThreadSleepStrategy,实现方式是通过Thread.sleep(sleepTime)来实现

默认的阻塞策略是线程休眠,可以自定义阻塞策略,这里使用自旋锁实现,不阻塞线程。

  1. package net.ijiangtao.tech.framework.spring.ispringboot.demo.retryer.guava.strategy;
  2. import com.github.rholder.retry.BlockStrategy;
  3. import lombok.NoArgsConstructor;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.time.Duration;
  6. import java.time.LocalDateTime;
  7. /**
  8. * 自旋锁的实现, 不响应线程中断
  9. */
  10. @Slf4j
  11. @NoArgsConstructor
  12. public class SpinBlockStrategy implements BlockStrategy {
  13. @Override
  14. public void block(long sleepTime) throws InterruptedException {
  15. LocalDateTime startTime = LocalDateTime.now();
  16. long start = System.currentTimeMillis();
  17. long end = start;
  18. log.info("[SpinBlockStrategy]...begin wait.");
  19. while (end - start <= sleepTime) {
  20. end = System.currentTimeMillis();
  21. }
  22. //使用Java8新增的Duration计算时间间隔
  23. Duration duration = Duration.between(startTime, LocalDateTime.now());
  24. log.info("[SpinBlockStrategy]...end wait.duration={}", duration.toMillis());
  25. }
  26. }

使用时:

  1. //自定义阻塞策略:自旋锁
  2. .withBlockStrategy(new SpinBlockStrategy())

4、StopStrategies 重试停止策略

用来指定重试多少次后停止重试。因为无限制的重试不一定是一个好的方式,可能会给下游系统带来灾难性。

通过withStopStrategy方法可以设置重试停止策略,StopStrategies.java中定义了很多重试停止策略,常见的有:

1)NeverStopStrategy

无限重试,例如:

withStopStrategy(StopStrategies.neverStop())

2)StopAfterAttemptStrategy

重试n次后,停止;例如:

withStopStrategy(StopStrategies.stopAfterAttempt(3))

3)StopAfterDelayStrategy

重试多长时间后,停止;例如:

withStopStrategy(StopStrategies.stopAfterDelay(3, TimeUnit.MINUTES))

5、AttemptTimeLimiters 任务执行时长限制

这个表示单次任务执行时间限制(如果单次任务执行超时,则终止执行当前任务);

通过withAttemptTimeLimiter方法可以设置任务的执行时间限制,常见的有:

1)NoAttemptTimeLimit 无时长限制

顾名思义,不限制执行时长;每次都是等执行任务执行完成之后,才进行后续的重试策咯。

.withAttemptTimeLimiter(AttemptTimeLimiters.noTimeLimit())

2) FixedAttemptTimeLimit

可以指定任务的执行时长限制,并且为了控制线程管理,最好指定相应的线程池。

.withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(10, TimeUnit.SECONDS));

.withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(10, TimeUnit.SECONDS, Executors.newCachedThreadPool()));

参考:

https://www.jianshu.com/p/a289dde63043

https://juejin.cn/post/6844903785031008263

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/828903
推荐阅读
相关标签
  

闽ICP备14008679号