服务重试-guava-retrying

目录

        • 1、pom引入jar包
        • 2、构建回调方法
          • 2.1 定义回调函数
          • 2.2 构建Retryer
          • 2.3 执行
        • 3 、执行过程(源码解析)
        • 4、高级使用
          • 4.1 WaitStrategy 等待策略
          • 4.2 BlockStrategy 阻塞策略
          • 4.3 StopStrategy 停止策略
          • 4.4 RetryListener 重试监听器
        • 5、关注更多

1、pom引入jar包

        
			com.github.rholder
			guava-retrying
			2.0.0
		

2、构建回调方法

2.1 定义回调函数
   Callable callable = new Callable() {
            @Override
            public Boolean call() throws Exception {
                log.info("方法调用");
                return false;
            }
        };

2.2 构建Retryer
 Retryer retryer = RetryerBuilder.newBuilder()
                //重试条件 (多个)
                .retryIfException()
                .retryIfRuntimeException()
                //自定义异常类
                .retryIfExceptionOfType(IOException.class)
                .retryIfException(Predicates.equalTo(new Exception()))
                //响应结果判断是否需要重试
                .retryIfResult(Predicates.equalTo(false))
                //添加重试监听器:可配置多个自定义监听器,每次重试时都会调用
                .withRetryListener(new DefinedRetryListener())
                //等待策略:默认不等待,  固定等待时间,随机等待时间, 自动增加等待时间,指数等待策略,斐波那契递增策略,异常等待策略
                //等待策略用于计算休眠时间,阻塞策略在休眠时间内进行阻塞
                .withWaitStrategy(WaitStrategies.noWait())
                //阻塞策略: 默认是线程休眠,可自定义阻塞策略
                .withBlockStrategy(BlockStrategies.threadSleepStrategy())
                //重试时间限制:每次重试执行的时间限制, 无限制,固定时间限制
                .withAttemptTimeLimiter(AttemptTimeLimiters.noTimeLimit())
                //停止策略: 默认是永不停止,支持按时间,次数进行配置,或者自定义停止策略
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .build();
2.3 执行
 try {
            retryer.call(callable);
        } catch (RetryException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

3 、执行过程(源码解析)

 public V call(Callable callable) throws ExecutionException, RetryException {
            long startTime = System.nanoTime();
            //循环重试
            for (int attemptNumber = 1; ; attemptNumber++) {
                Attempt attempt;
                try {
                    //执行回调业务方法,AttemptTimeLimiter限制方法执行时间
                    V result = attemptTimeLimiter.call(callable);
                    //封装重试结果
                    attempt = new Retryer.ResultAttempt(result, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
                } catch (Throwable t) {
                    attempt = new Retryer.ExceptionAttempt(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
                }
                //遍历自定义监听器,执行监听器中方法
                for (RetryListener listener : listeners) {
                    listener.onRetry(attempt);
                }
                //判断是否满足重试条件,不满足直接返回结果
                if (!rejectionPredicate.apply(attempt)) {
                    return attempt.get();
                }
                //判断是否应该停止,需要停止且没有正确响应结果 抛出重试异常
                if (stopStrategy.shouldStop(attempt)) {
                    throw new RetryException(attemptNumber, attempt);
                } else {
                    //根据等待策略计算休眠时长,单位毫秒
                    long sleepTime = waitStrategy.computeSleepTime(attempt);
                    try {
                        //阻塞策略执行阻塞,根据休眠时长进行阻塞
                        blockStrategy.block(sleepTime);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        throw new RetryException(attemptNumber, attempt);
                    }
                }
            }
        }

4、高级使用

4.1 WaitStrategy 等待策略

说明:等待策略的功能是计算重试前的等待时间(毫秒)

已有等待策略类:

  1. FixedWaitStrategy :固定时长等待策略,入参休眠时长,单位毫秒
  2. RandomWaitStrategy :随机时长等待策策略,入参(最小,最大), 单位毫秒
  3. IncrementingWaitStrategy :自增时长等待策策略,入参(初始时长,步长) ,单位毫秒
  4. ExponentialWaitStrategy :指数时长等待策策略,规则示例:1,2,4,8 入参(乘数,最大时长)
  5. FibonacciWaitStrategy :斐波纳契等待策策略,规则示例:1,2,3,5,8 入参(乘数,最大时长)
  6. ExceptionWaitStrategy:异常等待策略,基于异常回调执行,返回不同的等待时长
  7. CompositeWaitStrategy: 组合策略,多个等待策略组合在一起使用,多个策略等待时间相加

自定义实现:实现 WaitStrategy

/**
 * 自定义等待策略
 *  @author yang.liu
 */
public class DefinedWaitStrategy implements WaitStrategy {
    /**
     * 计算休眠时间,返回的是毫秒数
     * @param failedAttempt
     * @return
     */
    @Override
    public long computeSleepTime(Attempt failedAttempt) {
        //自定义时长计算规则
        return 2000;
    }
}
4.2 BlockStrategy 阻塞策略

说明: 阻塞策略的功能是 将根据等待策略计算的时长进行阻塞处理

默认实现类:ThreadSleepStrategy,线程休眠

自定义实现: 实现BlockStrategy接口

/**
 * 自定义阻塞策略
 *
 * @author yang.liu
 */
public class DefinedBlockStrategy implements BlockStrategy {
    @Override
    public void block(long sleepTime) throws InterruptedException {
        //自定义阻塞规则
    }
}

4.3 StopStrategy 停止策略

说明:停止策略 判断需要停止重试,默认永不停止

已有实现类:

  1. NeverStopStrategy: 永不停止
  2. StopAfterAttemptStrategy: 重试几次后停止
  3. StopAfterDelayStrategy: 延迟多少毫秒后停止(和最开始执行时间比较)

自定义: 实现StopStrategy接口

/**
 * 自定义停止策略
 * @author yang.liu
 */
public class DefinedStopStrategy implements StopStrategy {
    @Override
    public boolean shouldStop(Attempt failedAttempt) {
        //自定义是否需要停止,true 停止,false 不停止
        //具体自定义规则
        return false;
    }
}

4.4 RetryListener 重试监听器

说明:自定义重试监听器,支持配置多个,每次重试都会调用监听器中回调方法,可在监听器中进行相应业务处理,比如记录每次重试的异常信息等

/**
 * 自定义 重试监听器
 *
 * @author yang.liu
 */
@Slf4j
public class DefinedRetryListener implements RetryListener {
    @Override
    public  void onRetry(Attempt attempt) {
        log.info("重试监听器 次数{} 延迟时间{}", attempt.getAttemptNumber(), attempt.getDelaySinceFirstAttempt());
    }
}

5、关注更多

欢迎关注我的技术公众号,原创Java技术分享,个人成长感悟。
服务重试-guava-retrying_第1张图片

你可能感兴趣的:(spring,boot,2)