Hystrix斷路器的原理

上篇文章我主要講的是官方文檔對Hystrix的說明,闡述了在微服中Hystrix擔(dān)任的角色,以及它是如何達到這樣的作用的。當(dāng)然具體如何使用Hystrix我并沒有詳細說明,因為網(wǎng)上關(guān)于Hystrix的使用的教程網(wǎng)上已經(jīng)很多了,大家隨便百度一下就能找到,這篇文章主要是能幫助大家更深入的理解Hystrix的實現(xiàn)原理。

我對Hystrix的理解是,它的核心其實就兩個,一個是"斷路器" ,另一個是"依賴隔離",能把這兩個核心理解透,就把Hystrix理解透了。"依賴隔離"后面如果有時間我們再講,今天我們先來說說"斷路器"的原理。如果對Hystrix的概念比較陌生,可以先看看這邊文章Hystrix的正確理解方式

首先我們肯定先去看官方文檔,先別一腦袋扎進源碼里這樣很難找到重點,正確的方式是根據(jù)官方文檔的說明再結(jié)合源碼這樣才能最高效的找到我們需要找到東西。官方文檔關(guān)于”斷路器“原理的說明在這 https://github.com/Netflix/Hystrix/wiki/How-it-Works,下圖是官方文檔中對”熔斷器“原理說明的流程圖。

circuit-breaker-640.png

看到這個流程圖可能會有點蒙,不急,我們先來看看文檔中對這張流程圖的說明:

The following diagram shows how aHystrixCommand orHystrixObservableCommandinteracts with aHystrixCircuitBreaker and its flow of logic and decision-making, including how the counters behave in the circuit breaker
下面的流程圖展示的是HystrixCommand或HystrixObservableCommand與HystrixCircuitBreaker之間是如何交互的,以及其中的邏輯流程和判斷邏輯,還包括計數(shù)器在熔斷器的中作用。

上面是文檔中的原始內(nèi)容,下面是我的翻譯,再Hystrix的正確理解方式這邊文章中我說過,Hystrix的實現(xiàn)使用的設(shè)計模式是”命令模式“,再Hystrix中微服請求依賴的微服是通過HystrixCommand或是HystrixObservableCommand實現(xiàn)的,所以“斷路”和“隔離”邏輯其實就是在Command中實現(xiàn)的,而上面提到的HystrixCircuitBreaker就是正真實現(xiàn)”斷路器“邏輯的類。所以我們把HystrixCircuitBreaker搞明白了就明白了”斷路器“的原理。下面我們就根據(jù)HystrixCircuitBreaker源碼和上面的流程圖來說說”斷路器“中的邏輯(具體的說明我寫在下面代碼的注釋中)。

HystrixCircuitBreaker
 /**
 * 熔斷邏輯掛在HystrixCommand中執(zhí)行如果請求失敗次數(shù)超過規(guī)定的閥值,它將會定制請求的執(zhí)行。開啟熔斷后,
 * 它允許在一段時間的休眠后執(zhí)行一次請求,如果請求成功則關(guān)閉熔斷器,網(wǎng)絡(luò)請求被執(zhí)行。 
 */
public interface HystrixCircuitBreaker {

    /**
     * 每個Hystrix命令的請求都通過這個方法判斷是否執(zhí)行請求
     */
    boolean allowRequest();

    /**
     * 返回當(dāng)前斷路器是否打開的狀態(tài)
     */
    boolean isOpen();

    /**
     * 處于半開狀態(tài)時,如果嘗試請求成功,就調(diào)用這個方法(斷路器關(guān)閉在這個方法實現(xiàn)的)
     */
    void markSuccess();

    /**
     * 處于半開狀態(tài)時,如果嘗試請求成功,就調(diào)用這個方法(斷路器開啟在這個方法實現(xiàn)的)
     */
    void markNonSuccess();

    /**
     * 在命令執(zhí)行開始時調(diào)用以嘗試執(zhí)行。 這是非冪等的 - 它可能會修改內(nèi)部
     */
    boolean attemptExecution();

HystrixCircuitBreaker是一個抽象接口,包含上面5個抽象方法,其實并不復(fù)雜。
HystrixCircuitBreaker中還包含3個內(nèi)部類分別是:

1. Factory
   //這個里面存放的是ConcurrentHashMap<String, HystrixCircuitBreaker> ,看到這個接口大
    //家應(yīng)該能夠知道這個類使用來作什么的了,沒錯它就是用來管理這個微服務(wù)中所有斷路
    //器的工廠,每個依賴其他微服的接口都需要有對應(yīng)的斷路器。
    class Factory{
          //代碼省略
    }
2. HystrixCircuitBreakerImpl

斷路器接口HystrixCircuitBreaker的實現(xiàn)類,斷路器的主要業(yè)務(wù)邏輯都在這。

/* package */class HystrixCircuitBreakerImpl implements HystrixCircuitBreaker {
    //斷路器相關(guān)的配置參數(shù)
    private final HystrixCommandProperties properties;
    斷路器最核心的東西,通過時間窗的形式,記錄一段時間范圍內(nèi)(默認是10秒)的接口請求的健康狀況,
    并得到對應(yīng)的度量指標(biāo)(請求次數(shù),錯誤率),如果這個指標(biāo)不符合條件則斷路器打開。這塊邏輯比較復(fù)雜
    這里就不細說,想了解具體如何實現(xiàn)的可以看看對應(yīng)的源碼。
    private final HystrixCommandMetrics metrics;

    斷路器的三個狀態(tài) :OPEN  CLOSED 沒什么好講的,主要是這個HALF_OPEN狀態(tài),這個狀態(tài)在什么情況下出現(xiàn)呢,
    當(dāng)斷路器打開后,對應(yīng)接口的請求會有段休眠期,這個休眠期內(nèi)接口請求不會被正真的執(zhí)行,但是如果休眠期時間過了,
    這個時候斷路器的狀態(tài)就到了HALF_OPEN狀態(tài),這個時候斷路器允許一次真實的接口請求,如果這次請求失敗,則斷路
    器打開(OPEN),循環(huán)上面的動作,如果請求成功則斷路器關(guān)閉(CLOSED)。
    enum Status {
        CLOSED, OPEN, HALF_OPEN;
    }
    
    記錄斷路器的狀態(tài),默認是關(guān)閉的
    private final AtomicReference<Status> status = new AtomicReference<Status>(Status.CLOSED);
    記錄最近一次斷路器開啟的時間,用于判斷休眠期的結(jié)束時間
    private final AtomicLong circuitOpened = new AtomicLong(-1);
    這個是通過Rxjava實現(xiàn)的對HystrixCommandMetrics結(jié)果的觀察者對象,當(dāng)HystrixCommandMetrics值發(fā)生變化時會通知觀察者。
    private final AtomicReference<Subscription> activeSubscription = new AtomicReference<Subscription>(null);

    protected HystrixCircuitBreakerImpl(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, final HystrixCommandProperties properties, HystrixCommandMetrics metrics) {
        this.properties = properties;
        this.metrics = metrics;

        //On a timer, this will set the circuit between OPEN/CLOSED as command executions occur
        Subscription s = subscribeToStream();
        activeSubscription.set(s);
    }

    private Subscription subscribeToStream() {
        /*
         * This stream will recalculate the OPEN/CLOSED status on every onNext from the health stream
         */
        return metrics.getHealthCountsStream()
                .observe()
                .subscribe(new Subscriber<HealthCounts>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }
  這個就是上面說的觀察者,當(dāng)HystrixCommandMetrics的度量指標(biāo)發(fā)生變化時,觀察者實現(xiàn)的業(yè)務(wù)邏輯
                    @Override
                    public void onNext(HealthCounts hc) {
                        首先校驗的時在時間窗范圍內(nèi)的請求次數(shù),如果低于閾值(默認是20),不做處理,如果高于閾值,則去判斷接口請求的錯誤率
                        if (hc.getTotalRequests() < properties.circuitBreakerRequestVolumeThreshold().get()) {
                            // we are not past the minimum volume threshold for the stat window,
                            // so no change to circuit status.
                            // if it was CLOSED, it stays CLOSED
                            // if it was half-open, we need to wait for a successful command execution
                            // if it was open, we need to wait for sleep window to elapse
                        } else {
                      判斷接口請求的錯誤率(閾值默認是50),如果高于這個值,則斷路器打開
                            if (hc.getErrorPercentage() < properties.circuitBreakerErrorThresholdPercentage().get()) {
                                //we are not past the minimum error threshold for the stat window,
                                // so no change to circuit status.
                                // if it was CLOSED, it stays CLOSED
                                // if it was half-open, we need to wait for a successful command execution
                                // if it was open, we need to wait for sleep window to elapse
                            } else {
                               打開斷路器,同時記錄斷路器開啟時間
                                if (status.compareAndSet(Status.CLOSED, Status.OPEN)) {
                                    circuitOpened.set(System.currentTimeMillis());
                                }
                            }
                        }
                    }
                });
    }
    
    半開狀態(tài),嘗試請求接口成功
    @Override
    public void markSuccess() {
        關(guān)閉斷路器
        if (status.compareAndSet(Status.HALF_OPEN, Status.CLOSED)) {
            重置時間窗健康度量指標(biāo)
            metrics.resetStream();
            Subscription previousSubscription = activeSubscription.get();
            注銷觀察者
            if (previousSubscription != null) {
                previousSubscription.unsubscribe();
            }
            設(shè)置新的觀察者
            Subscription newSubscription = subscribeToStream();
            activeSubscription.set(newSubscription);
            還原斷路器開啟時間
            circuitOpened.set(-1L);
        }
    }
    
    半開狀態(tài),嘗試請求接口失敗
    @Override
    public void markNonSuccess() {
        斷路器打開
        if (status.compareAndSet(Status.HALF_OPEN, Status.OPEN)) {
           更新最新的斷路器開啟時間
            circuitOpened.set(System.currentTimeMillis());
        }
    }

    @Override
    public boolean isOpen() {
       強制開啟斷路器
        if (properties.circuitBreakerForceOpen().get()) {
            return true;
        }
       強制關(guān)閉斷路器(斷路器可以通過配置強制關(guān)閉或開啟)
        if (properties.circuitBreakerForceClosed().get()) {
            return false;
        }
        根據(jù)斷路器開啟時間判斷斷路器的開啟狀態(tài)
        return circuitOpened.get() >= 0;
    }
    
    判斷是否允許請求接口(每次請求接口都會判斷)
    @Override
    public boolean allowRequest() {
        if (properties.circuitBreakerForceOpen().get()) {
            return false;
        }
        if (properties.circuitBreakerForceClosed().get()) {
            return true;
        }
        if (circuitOpened.get() == -1) {
            return true;
        } else {
            if (status.get().equals(Status.HALF_OPEN)) {
                return false;
            } else {
                return isAfterSleepWindow();
            }
        }
    }

    判斷時間有沒有過休眠期
    private boolean isAfterSleepWindow() {
        final long circuitOpenTime = circuitOpened.get();
        final long currentTime = System.currentTimeMillis();
        final long sleepWindowTime = properties.circuitBreakerSleepWindowInMilliseconds().get();
        return currentTime > circuitOpenTime + sleepWindowTime;
    }

    嘗試執(zhí)行接口請求
    @Override
    public boolean attemptExecution() {
        if (properties.circuitBreakerForceOpen().get()) {
            return false;
        }
        if (properties.circuitBreakerForceClosed().get()) {
            return true;
        }
        if (circuitOpened.get() == -1) {
            return true;
        } else {
            if (isAfterSleepWindow()) {
                if (status.compareAndSet(Status.OPEN, Status.HALF_OPEN)) {
                    //only the first request after sleep window should execute
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
}

在上面的代碼中的注釋應(yīng)該就能理解斷路器整體的實現(xiàn)邏輯了。

3.NoOpCircuitBreaker

什么都沒做的HystrixCircuitBreaker實現(xiàn),允許所有請求,斷路器始終是關(guān)閉的。

    /* package */static class NoOpCircuitBreaker implements HystrixCircuitBreaker {

    @Override
    public boolean allowRequest() {
        return true;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public void markSuccess() {

    }

    @Override
    public void markNonSuccess() {

    }

    @Override
    public boolean attemptExecution() {
        return true;
    }
}
總結(jié)

這篇文章從源碼角度解釋了Hystrix斷路器的原理,看完HystrixCircuitBreaker的邏輯后,再去看文章開始貼的那張斷路器的流程圖,就能很好的理解這個流程了。希望這篇文章能對大家有所幫助,歡迎點贊大賞!文章中有錯誤的地方歡迎評論指出。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • (git上的源碼:https://gitee.com/rain7564/spring_microservices_...
    sprainkle閱讀 9,566評論 13 33
  • 老朋友們依然習(xí)慣喊他“小滿”,但“小滿”已悄然變成了“滿叔”。 千帆過盡,歲月的江流沖刷走了很多熟悉的印象,奔騰不...
    枕邊音樂哦閱讀 741評論 0 0
  • 她是一位女生。她長得十分普通,圓圓的臉蛋上有一雙圓圓的眼睛。她視力特別好,經(jīng)常把好位置讓給同學(xué)坐,她時常坐在最后一...
    喜歡廚房的人閱讀 432評論 5 5

友情鏈接更多精彩內(nèi)容