Spring中使用Hystrix實現(xiàn)熔斷詳解
Hystrix實現(xiàn)熔斷
在使用Hystrix實現(xiàn)熔斷的過程中遇到了兩個問題
1、在修改了熔斷配置之后不生效的問題
2、熔斷后不恢復的問題
對于第一個問題,查看hystrix源碼可以看到,如果有緩存配置是優(yōu)先使用的緩存的,因此如果配置更新,必須要更新緩存,不能使用Hystrix.reset()方法來更新緩存,這個方法清理全局緩存,會影響其他commandkey的熔斷狀態(tài)。
public static HystrixCircuitBreaker getInstance(HystrixCommandKey key, HystrixCommandGroupKey group, HystrixCommandProperties properties, HystrixCommandMetrics metrics) { // this should find it for all but the first time HystrixCircuitBreaker previouslyCached = circuitBreakersByCommand.get(key.name()); if (previouslyCached != null) { return previouslyCached; }
官方提供的方法是通過archaius動態(tài)更新
Hystrix uses Archaius for the default implementation of properties for configuration.
實現(xiàn)接口com.netflix.config.PolledConfigurationSource,將對應的commandkey通過形如下面的key來放入poll方法響應的map中,hystrix會根據這個key自動更新緩存
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
第二個問題經過debughystrix源碼發(fā)現(xiàn)是由于,使用了響應式的調用,hystrix使用的是V1版本的rxjava,而項目使用的是V3版本的rxjava,在V3轉V1后響應給hystrixcircuitbreaker時,hystrix沒有收到oncomplete事件,導致沒有在斷路器半開狀態(tài)時調用成功關閉斷路器。
在com.netflix.hystrix.AbstractCommand類中實現(xiàn)了斷路器的控制邏輯:
可以看到在onCompleted的觀察者邏輯中會將斷路器markSuccess
private Observable<R> executeCommandAndObserve(final AbstractCommand<R> _cmd) { final HystrixRequestContext currentRequestContext = HystrixRequestContext.getContextForCurrentThread(); final Action1<R> markEmits = new Action1<R>() { @Override public void call(R r) { if (shouldOutputOnNextEvents()) { executionResult = executionResult.addEvent(HystrixEventType.EMIT); eventNotifier.markEvent(HystrixEventType.EMIT, commandKey); } if (commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); circuitBreaker.markSuccess(); } } }; final Action0 markOnCompleted = new Action0() { @Override public void call() { if (!commandIsScalar()) { long latency = System.currentTimeMillis() - executionResult.getStartTimestamp(); eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList()); eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey); executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS); circuitBreaker.markSuccess(); } } };
目前想到的解決辦法是一種是在接收調用服務響應時blockingfirst(),但是這樣會導致異步調用變成同步,還有一種是在調用服務的Observable的onnext方法中添加onCompleted事件,來通知hystrix調用服務接收到了complete事件,但是這樣就只能接收到單個onNext事件,兩種都有缺點,還有想到更好的解決辦法。
到此這篇關于Spring中使用Hystrix實現(xiàn)熔斷詳解的文章就介紹到這了,更多相關Hystrix實現(xiàn)熔斷內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud升級最新Finchley版本的所有坑
這篇文章主要介紹了Spring Cloud升級最新Finchley版本的所有坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Java數(shù)據結構之優(yōu)先級隊列(堆)圖文詳解
優(yōu)先級隊列是比棧和隊列更專用的結構,在多數(shù)情況下都非常有用,下面這篇文章主要給大家介紹了關于Java數(shù)據結構之優(yōu)先級隊列(堆)的相關資料,需要的朋友可以參考下2022-03-03mybatis水平分表實現(xiàn)動態(tài)表名的項目實例
本文主要介紹了mybatis水平分表實現(xiàn)動態(tài)表名的項目實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07spring cloud gateway網關路由分配代碼實例解析
這篇文章主要介紹了spring cloud gateway網關路由分配代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01