上篇文章我主要講的是官方文檔對(duì)Hystrix的說明剪验,闡述了在微服中Hystrix擔(dān)任的角色远荠,以及它是如何達(dá)到這樣的作用的衡蚂。當(dāng)然具體如何使用Hystrix我并沒有詳細(xì)說明括细,因?yàn)榫W(wǎng)上關(guān)于Hystrix的使用的教程網(wǎng)上已經(jīng)很多了,大家隨便百度一下就能找到搓萧,這篇文章主要是能幫助大家更深入的理解Hystrix的實(shí)現(xiàn)原理杂数。
我對(duì)Hystrix的理解是,它的核心其實(shí)就兩個(gè)瘸洛,一個(gè)是"斷路器" 揍移,另一個(gè)是"依賴隔離",能把這兩個(gè)核心理解透反肋,就把Hystrix理解透了那伐。"依賴隔離"后面如果有時(shí)間我們?cè)僦v,今天我們先來說說"斷路器"的原理石蔗。如果對(duì)Hystrix的概念比較陌生罕邀,可以先看看這邊文章Hystrix的正確理解方式
首先我們肯定先去看官方文檔,先別一腦袋扎進(jìn)源碼里這樣很難找到重點(diǎn)养距,正確的方式是根據(jù)官方文檔的說明再結(jié)合源碼這樣才能最高效的找到我們需要找到東西诉探。官方文檔關(guān)于”斷路器“原理的說明在這 https://github.com/Netflix/Hystrix/wiki/How-it-Works,下圖是官方文檔中對(duì)”熔斷器“原理說明的流程圖。
看到這個(gè)流程圖可能會(huì)有點(diǎn)蒙棍厌,不急肾胯,我們先來看看文檔中對(duì)這張流程圖的說明:
The following diagram shows how aHystrixCommand
orHystrixObservableCommand
interacts with aHystrixCircuitBreaker
and its flow of logic and decision-making, including how the counters behave in the circuit breaker
下面的流程圖展示的是HystrixCommand或HystrixObservableCommand與HystrixCircuitBreaker之間是如何交互的,以及其中的邏輯流程和判斷邏輯耘纱,還包括計(jì)數(shù)器在熔斷器的中作用敬肚。
上面是文檔中的原始內(nèi)容,下面是我的翻譯揣炕,再Hystrix的正確理解方式這邊文章中我說過帘皿,Hystrix的實(shí)現(xiàn)使用的設(shè)計(jì)模式是”命令模式“,再Hystrix中微服請(qǐng)求依賴的微服是通過HystrixCommand或是HystrixObservableCommand實(shí)現(xiàn)的畸陡,所以“斷路”和“隔離”邏輯其實(shí)就是在Command中實(shí)現(xiàn)的,而上面提到的HystrixCircuitBreaker就是正真實(shí)現(xiàn)”斷路器“邏輯的類虽填。所以我們把HystrixCircuitBreaker搞明白了就明白了”斷路器“的原理丁恭。下面我們就根據(jù)HystrixCircuitBreaker源碼和上面的流程圖來說說”斷路器“中的邏輯(具體的說明我寫在下面代碼的注釋中)。
HystrixCircuitBreaker
/**
* 熔斷邏輯掛在HystrixCommand中執(zhí)行如果請(qǐng)求失敗次數(shù)超過規(guī)定的閥值斋日,它將會(huì)定制請(qǐng)求的執(zhí)行牲览。開啟熔斷后,
* 它允許在一段時(shí)間的休眠后執(zhí)行一次請(qǐng)求恶守,如果請(qǐng)求成功則關(guān)閉熔斷器第献,網(wǎng)絡(luò)請(qǐng)求被執(zhí)行贡必。
*/
public interface HystrixCircuitBreaker {
/**
* 每個(gè)Hystrix命令的請(qǐng)求都通過這個(gè)方法判斷是否執(zhí)行請(qǐng)求
*/
boolean allowRequest();
/**
* 返回當(dāng)前斷路器是否打開的狀態(tài)
*/
boolean isOpen();
/**
* 處于半開狀態(tài)時(shí),如果嘗試請(qǐng)求成功庸毫,就調(diào)用這個(gè)方法(斷路器關(guān)閉在這個(gè)方法實(shí)現(xiàn)的)
*/
void markSuccess();
/**
* 處于半開狀態(tài)時(shí)仔拟,如果嘗試請(qǐng)求成功,就調(diào)用這個(gè)方法(斷路器開啟在這個(gè)方法實(shí)現(xiàn)的)
*/
void markNonSuccess();
/**
* 在命令執(zhí)行開始時(shí)調(diào)用以嘗試執(zhí)行飒赃。 這是非冪等的 - 它可能會(huì)修改內(nèi)部
*/
boolean attemptExecution();
HystrixCircuitBreaker是一個(gè)抽象接口利花,包含上面5個(gè)抽象方法,其實(shí)并不復(fù)雜载佳。
HystrixCircuitBreaker中還包含3個(gè)內(nèi)部類分別是:
1. Factory
//這個(gè)里面存放的是ConcurrentHashMap<String, HystrixCircuitBreaker> ,看到這個(gè)接口大
//家應(yīng)該能夠知道這個(gè)類使用來作什么的了炒事,沒錯(cuò)它就是用來管理這個(gè)微服務(wù)中所有斷路
//器的工廠,每個(gè)依賴其他微服的接口都需要有對(duì)應(yīng)的斷路器蔫慧。
class Factory{
//代碼省略
}
2. HystrixCircuitBreakerImpl
斷路器接口HystrixCircuitBreaker的實(shí)現(xiàn)類挠乳,斷路器的主要業(yè)務(wù)邏輯都在這。
/* package */class HystrixCircuitBreakerImpl implements HystrixCircuitBreaker {
//斷路器相關(guān)的配置參數(shù)
private final HystrixCommandProperties properties;
斷路器最核心的東西姑躲,通過時(shí)間窗的形式欲侮,記錄一段時(shí)間范圍內(nèi)(默認(rèn)是10秒)的接口請(qǐng)求的健康狀況,
并得到對(duì)應(yīng)的度量指標(biāo)(請(qǐng)求次數(shù)肋联,錯(cuò)誤率)威蕉,如果這個(gè)指標(biāo)不符合條件則斷路器打開。這塊邏輯比較復(fù)雜
這里就不細(xì)說橄仍,想了解具體如何實(shí)現(xiàn)的可以看看對(duì)應(yīng)的源碼韧涨。
private final HystrixCommandMetrics metrics;
斷路器的三個(gè)狀態(tài) :OPEN CLOSED 沒什么好講的,主要是這個(gè)HALF_OPEN狀態(tài)侮繁,這個(gè)狀態(tài)在什么情況下出現(xiàn)呢虑粥,
當(dāng)斷路器打開后,對(duì)應(yīng)接口的請(qǐng)求會(huì)有段休眠期宪哩,這個(gè)休眠期內(nèi)接口請(qǐng)求不會(huì)被正真的執(zhí)行娩贷,但是如果休眠期時(shí)間過了,
這個(gè)時(shí)候斷路器的狀態(tài)就到了HALF_OPEN狀態(tài)锁孟,這個(gè)時(shí)候斷路器允許一次真實(shí)的接口請(qǐng)求彬祖,如果這次請(qǐng)求失敗,則斷路
器打開(OPEN)品抽,循環(huán)上面的動(dòng)作储笑,如果請(qǐng)求成功則斷路器關(guān)閉(CLOSED)。
enum Status {
CLOSED, OPEN, HALF_OPEN;
}
記錄斷路器的狀態(tài)圆恤,默認(rèn)是關(guān)閉的
private final AtomicReference<Status> status = new AtomicReference<Status>(Status.CLOSED);
記錄最近一次斷路器開啟的時(shí)間突倍,用于判斷休眠期的結(jié)束時(shí)間
private final AtomicLong circuitOpened = new AtomicLong(-1);
這個(gè)是通過Rxjava實(shí)現(xiàn)的對(duì)HystrixCommandMetrics結(jié)果的觀察者對(duì)象,當(dāng)HystrixCommandMetrics值發(fā)生變化時(shí)會(huì)通知觀察者。
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) {
}
這個(gè)就是上面說的觀察者羽历,當(dāng)HystrixCommandMetrics的度量指標(biāo)發(fā)生變化時(shí)焊虏,觀察者實(shí)現(xiàn)的業(yè)務(wù)邏輯
@Override
public void onNext(HealthCounts hc) {
首先校驗(yàn)的時(shí)在時(shí)間窗范圍內(nèi)的請(qǐng)求次數(shù),如果低于閾值(默認(rèn)是20)秕磷,不做處理诵闭,如果高于閾值,則去判斷接口請(qǐng)求的錯(cuò)誤率
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 {
判斷接口請(qǐng)求的錯(cuò)誤率(閾值默認(rèn)是50)跳夭,如果高于這個(gè)值涂圆,則斷路器打開
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 {
打開斷路器,同時(shí)記錄斷路器開啟時(shí)間
if (status.compareAndSet(Status.CLOSED, Status.OPEN)) {
circuitOpened.set(System.currentTimeMillis());
}
}
}
}
});
}
半開狀態(tài)币叹,嘗試請(qǐng)求接口成功
@Override
public void markSuccess() {
關(guān)閉斷路器
if (status.compareAndSet(Status.HALF_OPEN, Status.CLOSED)) {
重置時(shí)間窗健康度量指標(biāo)
metrics.resetStream();
Subscription previousSubscription = activeSubscription.get();
注銷觀察者
if (previousSubscription != null) {
previousSubscription.unsubscribe();
}
設(shè)置新的觀察者
Subscription newSubscription = subscribeToStream();
activeSubscription.set(newSubscription);
還原斷路器開啟時(shí)間
circuitOpened.set(-1L);
}
}
半開狀態(tài)润歉,嘗試請(qǐng)求接口失敗
@Override
public void markNonSuccess() {
斷路器打開
if (status.compareAndSet(Status.HALF_OPEN, Status.OPEN)) {
更新最新的斷路器開啟時(shí)間
circuitOpened.set(System.currentTimeMillis());
}
}
@Override
public boolean isOpen() {
強(qiáng)制開啟斷路器
if (properties.circuitBreakerForceOpen().get()) {
return true;
}
強(qiáng)制關(guān)閉斷路器(斷路器可以通過配置強(qiáng)制關(guān)閉或開啟)
if (properties.circuitBreakerForceClosed().get()) {
return false;
}
根據(jù)斷路器開啟時(shí)間判斷斷路器的開啟狀態(tài)
return circuitOpened.get() >= 0;
}
判斷是否允許請(qǐng)求接口(每次請(qǐng)求接口都會(huì)判斷)
@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();
}
}
}
判斷時(shí)間有沒有過休眠期
private boolean isAfterSleepWindow() {
final long circuitOpenTime = circuitOpened.get();
final long currentTime = System.currentTimeMillis();
final long sleepWindowTime = properties.circuitBreakerSleepWindowInMilliseconds().get();
return currentTime > circuitOpenTime + sleepWindowTime;
}
嘗試執(zhí)行接口請(qǐng)求
@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)該就能理解斷路器整體的實(shí)現(xiàn)邏輯了。
3.NoOpCircuitBreaker
什么都沒做的HystrixCircuitBreaker實(shí)現(xiàn)颈抚,允許所有請(qǐng)求踩衩,斷路器始終是關(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的邏輯后驱富,再去看文章開始貼的那張斷路器的流程圖,就能很好的理解這個(gè)流程了匹舞。希望這篇文章能對(duì)大家有所幫助褐鸥,歡迎點(diǎn)贊大賞!文章中有錯(cuò)誤的地方歡迎評(píng)論指出赐稽。