Guava Retryer實現接口重試

前言

小黑在開發中遇到個問題,我負責的模塊需要調用某個三方服務接口查詢信息,查詢結果直接影響後續業務邏輯的處理;

這個接口偶爾會因網絡問題出現超時,導致我的業務邏輯無法繼續處理;

這個問題該如何解決呢?,小黑首先想到的就是重試嘛,如果失敗了就再調用一次。

問題來了,如果又失敗了呢?接着重試嘛。我們循環處理,比如循環5次,全失敗則任務服務不可用,結束調用。

如果我又想着5次調用間隔一段時間呢?第一次先隔1秒,然後3秒,然後5秒呢?

小黑髮現事情沒那麼簡單,如果自己搞容易出BUG呀。

轉念一想,這個常見挺常見,網上應該有輪子呀,找找看。一不小心就讓我給找着啦,哈哈。

Guava Retryer

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.

使用Guava Retryer你可以自定義來執行重試,同時也可以監控每次重試的結果和行為,最重要的基於 Guava 風格的重試方式真的很方便。

引入依賴

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

快速開始

Callable<Boolean> callable = new Callable<Boolean>() {
    public Boolean call() throws Exception {
        return true; // do something useful here
    }
};

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
    .retryIfResult(Predicates.<Boolean>isNull()) // callable返回null時重試
    .retryIfExceptionOfType(IOException.class) // callable拋出IOException重試
    .retryIfRuntimeException() // callable拋出RuntimeException重試
    .withStopStrategy(StopStrategies.stopAfterAttempt(3)) // 重試3次後停止
    .build();
try {
    retryer.call(callable);
} catch (RetryException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}

Callablecall()方法返回null,拋出IOException或者RuntimeException時會重試;
將在嘗試重試3次後停止,並拋出包含上次失敗嘗試信息的RetryException;
如果call()方法中彈出任何其他異常,它將被包裝並在ExecutionException中重新調用。

指數退避(Exponential Backoff)

根據wiki上對Exponential backoff的說明,指數補償是一種通過反饋,成倍地降低某個過程的速率,以逐漸找到合適速率的算法。

在以太網中,該算法通常用於衝突後的調度重傳。根據時隙和重傳嘗試次數來決定延遲重傳。

c次碰撞後(比如請求失敗),會選擇0和2^c - 1之間的隨機值作為時隙的數量。

對於第1次碰撞來說,每個發送者將會等待0或1個時隙進行發送。
而在第2次碰撞後,發送者將會等待0到3( 由2^2 -1 計算得到)個時隙進行發送。
而在第3次碰撞後,發送者將會等待0到7( 由2^3 - 1 計算得到)個時隙進行發送。
以此類推……

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
        .retryIfExceptionOfType(IOException.class)
        .retryIfRuntimeException()
        .withWaitStrategy(WaitStrategies.exponentialWait(100, 5, TimeUnit.MINUTES)) // 指數退避
        .withStopStrategy(StopStrategies.neverStop()) // 永遠不停止重試
        .build();

創建一個永遠重試的重試器,在每次重試失敗後以指數級退避間隔遞增,直到最多5分鐘。5分鐘後,從那時起每隔5分鐘重試一次。

斐波那契退避(Fibonacci Backoff)

斐波那契數列指的是這樣一個數列:

0,1,1,2,3,5,8,13,21,34,55,89…

這個數列從第3項開始,每一項都等於前兩項之和。

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
        .retryIfExceptionOfType(IOException.class)
        .retryIfRuntimeException()
        .withWaitStrategy(WaitStrategies.fibonacciWait(100, 2, TimeUnit.MINUTES)) // 斐波那契退避
        .withStopStrategy(StopStrategies.neverStop())
        .build();

創建一個永遠重試的重試器,在每次重試失敗後以增加斐波那契退避間隔的方式等待,直到最多2分鐘。2分鐘後,從那時起每隔2分鐘重試一次。

與指數退避策略類似,斐波那契退避策略遵循一種模式,即在每次嘗試失敗後等待的時間越來越長。

對於這兩種策略的性能英國利茲大學專門做過性能測試,相比指數退避策略,斐波那契退避策略可能性能更好,吞吐量可能也更好。

重試監聽器

當重試發生時,如果需要額外做一些動作,比如發送郵件通知之類的,可以通過RetryListener,Guava Retryer在每次重試之後會自動回調監聽器,並且支持註冊多個監聽。

@Slf4j
class DiyRetryListener<Boolean> implements RetryListener {
    @Override
    public <Boolean> void onRetry(Attempt<Boolean> attempt) {
        log.info("重試次數:{}",attempt.getAttemptNumber());
        log.info("距離第一次重試的延遲:{}",attempt.getDelaySinceFirstAttempt());
        if(attempt.hasException()){
            log.error("異常原因:",attempt.getExceptionCause());
        }else {
            System.out.println("正常處理結果:{}" + attempt.getResult());
        }
    }
}

定義監聽器之後,需要在Retryer中進行註冊。

        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfResult(Predicates.<Boolean>isNull()) // callable返回null時重試
                .retryIfExceptionOfType(IOException.class) // callable拋出IOException重試
                .retryIfRuntimeException() // callable拋出RuntimeException重試
                .withStopStrategy(StopStrategies.stopAfterAttempt(3)) // 重試3次後停止
                .withRetryListener(new DiyRetryListener<Boolean>()) // 註冊監聽器
                .build();

小結

Guava Retryer不光在重試策略上支持多種選擇,並且將業務邏輯的處理放在Callable中,和重試處理邏輯分開,實現了解耦,這比小黑自己去寫循環處理要優秀太多啦,Guava確實強大。

Tags: