服務重試-guava-retrying

1、pom引入jar包

        <dependency>
			<groupId>com.github.rholder</groupId>
			<artifactId>guava-retrying</artifactId>
			<version>2.0.0</version>
		</dependency>

2、構建回調方法

2.1 定義回調函數
   Callable<Boolean> callable = new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                log.info("方法調用");
                return false;
            }
        };

2.2 構建Retryer
 Retryer<Boolean> retryer = RetryerBuilder.<Boolean>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<V> callable) throws ExecutionException, RetryException {
            long startTime = System.nanoTime();
            //循環重試
            for (int attemptNumber = 1; ; attemptNumber++) {
                Attempt<V> attempt;
                try {
                    //執行回調業務方法,AttemptTimeLimiter限制方法執行時間
                    V result = attemptTimeLimiter.call(callable);
                    //封裝重試結果
                    attempt = new Retryer.ResultAttempt<V>(result, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
                } catch (Throwable t) {
                    attempt = new Retryer.ExceptionAttempt<V>(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 <V> void onRetry(Attempt<V> attempt) {
        log.info("重試監聽器 次數{} 延遲時間{}", attempt.getAttemptNumber(), attempt.getDelaySinceFirstAttempt());
    }
}

5、關注更多

歡迎關注我的技術公衆號,原創Java技術分享,個人成長感悟。
公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章