日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Resilience4j-轻量级熔断框架

發(fā)布時(shí)間:2025/3/21 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Resilience4j-轻量级熔断框架 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Resilience4j

簡介

Resilience4j是一款輕量級,易于使用的容錯(cuò)庫,其靈感來自于Netflix Hystrix,但是專為Java 8和函數(shù)式編程而設(shè)計(jì)。輕量級,因?yàn)閹熘皇褂昧?strong>Vavr,它沒有任何其他外部依賴下。相比之下,Netflix HystrixArchaius具有編譯依賴性,Archaius具有更多的外部庫依賴性,例如GuavaApache Commons Configuration

要使用Resilience4j,不需要引入所有依賴,只需要選擇你需要的。

Resilience4j提供了以下的核心模塊和拓展模塊:

核心模塊:

  • resilience4j-circuitbreaker: Circuit breaking
  • resilience4j-ratelimiter: Rate limiting
  • resilience4j-bulkhead: Bulkheading
  • resilience4j-retry: Automatic retrying (sync and async)
  • resilience4j-cache: Result caching
  • resilience4j-timelimiter: Timeout handling

Circuitbreaker

簡介

CircuitBreaker通過具有三種正常狀態(tài)的有限狀態(tài)機(jī)實(shí)現(xiàn):CLOSEDOPENHALF_OPEN以及兩個(gè)特殊狀態(tài)DISABLEDFORCED_OPEN。當(dāng)熔斷器關(guān)閉時(shí),所有的請求都會通過熔斷器。如果失敗率超過設(shè)定的閾值,熔斷器就會從關(guān)閉狀態(tài)轉(zhuǎn)換到打開狀態(tài),這時(shí)所有的請求都會被拒絕。當(dāng)經(jīng)過一段時(shí)間后,熔斷器會從打開狀態(tài)轉(zhuǎn)換到半開狀態(tài),這時(shí)僅有一定數(shù)量的請求會被放入,并重新計(jì)算失敗率,如果失敗率超過閾值,則變?yōu)榇蜷_狀態(tài),如果失敗率低于閾值,則變?yōu)殛P(guān)閉狀態(tài)。

Circuitbreaker狀態(tài)機(jī)

Resilience4j記錄請求狀態(tài)的數(shù)據(jù)結(jié)構(gòu)和Hystrix不同,Hystrix是使用滑動窗口來進(jìn)行存儲的,而Resilience4j采用的是Ring Bit Buffer(環(huán)形緩沖區(qū))。Ring Bit Buffer在內(nèi)部使用BitSet這樣的數(shù)據(jù)結(jié)構(gòu)來進(jìn)行存儲,BitSet的結(jié)構(gòu)如下圖所示:

環(huán)形緩沖區(qū)

每一次請求的成功或失敗狀態(tài)只占用一個(gè)bit位,與boolean數(shù)組相比更節(jié)省內(nèi)存。BitSet使用long[]數(shù)組來存儲這些數(shù)據(jù),意味著16個(gè)值(64bit)的數(shù)組可以存儲1024個(gè)調(diào)用狀態(tài)。

計(jì)算失敗率需要填滿環(huán)形緩沖區(qū)。例如,如果環(huán)形緩沖區(qū)的大小為10,則必須至少請求滿10次,才會進(jìn)行故障率的計(jì)算,如果僅僅請求了9次,即使9個(gè)請求都失敗,熔斷器也不會打開。但是CLOSE狀態(tài)下的緩沖區(qū)大小設(shè)置為10并不意味著只會進(jìn)入10個(gè) 請求,在熔斷器打開之前的所有請求都會被放入。

當(dāng)故障率高于設(shè)定的閾值時(shí),熔斷器狀態(tài)會從由CLOSE變?yōu)?strong>OPEN。這時(shí)所有的請求都會拋出CallNotPermittedException異常。當(dāng)經(jīng)過一段時(shí)間后,熔斷器的狀態(tài)會從OPEN變?yōu)?strong>HALF_OPEN,HALF_OPEN狀態(tài)下同樣會有一個(gè)Ring Bit Buffer,用來計(jì)算HALF_OPEN狀態(tài)下的故障率,如果高于配置的閾值,會轉(zhuǎn)換為OPEN,低于閾值則裝換為CLOSE。與CLOSE狀態(tài)下的緩沖區(qū)不同的地方在于,HALF_OPEN狀態(tài)下的緩沖區(qū)大小會限制請求數(shù),只有緩沖區(qū)大小的請求數(shù)會被放入。

除此以外,熔斷器還會有兩種特殊狀態(tài):DISABLED(始終允許訪問)和FORCED_OPEN(始終拒絕訪問)。這兩個(gè)狀態(tài)不會生成熔斷器事件(除狀態(tài)裝換外),并且不會記錄事件的成功或者失敗。退出這兩個(gè)狀態(tài)的唯一方法是觸發(fā)狀態(tài)轉(zhuǎn)換或者重置熔斷器。

熔斷器關(guān)于線程安全的保證措施有以下幾個(gè)部分:

  • 熔斷器的狀態(tài)使用AtomicReference保存的
  • 更新熔斷器狀態(tài)是通過無狀態(tài)的函數(shù)或者原子操作進(jìn)行的
  • 更新事件的狀態(tài)用synchronized關(guān)鍵字保護(hù)

意味著同一時(shí)間只有一個(gè)線程能夠修改熔斷器狀態(tài)或者記錄事件的狀態(tài)。

可配置參數(shù)

配置參數(shù)默認(rèn)值描述
failureRateThreshold50熔斷器關(guān)閉狀態(tài)和半開狀態(tài)使用的同一個(gè)失敗率閾值
ringBufferSizeInHalfOpenState10熔斷器半開狀態(tài)的緩沖區(qū)大小,會限制線程的并發(fā)量,例如緩沖區(qū)為10則每次只會允許10個(gè)請求調(diào)用后端服務(wù)
ringBufferSizeInClosedState100熔斷器關(guān)閉狀態(tài)的緩沖區(qū)大小,不會限制線程的并發(fā)量,在熔斷器發(fā)生狀態(tài)轉(zhuǎn)換前所有請求都會調(diào)用后端服務(wù)
waitDurationInOpenState60(s)熔斷器從打開狀態(tài)轉(zhuǎn)變?yōu)榘腴_狀態(tài)等待的時(shí)間
automaticTransitionFromOpenToHalfOpenEnabledfalse如果置為true,當(dāng)?shù)却龝r(shí)間結(jié)束會自動由打開變?yōu)榘腴_,若置為false,則需要一個(gè)請求進(jìn)入來觸發(fā)熔斷器狀態(tài)轉(zhuǎn)換
recordExceptionsempty需要記錄為失敗的異常列表
ignoreExceptionsempty需要忽略的異常列表
recordFailurethrowable -> true自定義的謂詞邏輯用于判斷異常是否需要記錄或者需要忽略,默認(rèn)所有異常都進(jìn)行記錄

測試前準(zhǔn)備

pom.xml

測試使用的IDEidea,使用的springboot進(jìn)行學(xué)習(xí)測試,首先引入maven依賴:

?

<dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-spring-boot</artifactId><version>0.9.0</version> </dependency>

resilience4j-spring-boot集成了circuitbeakerretrybulkheadratelimiter幾個(gè)模塊,因?yàn)楹罄m(xù)還要學(xué)習(xí)其他模塊,就直接引入resilience4j-spring-boot依賴。

application.yml配置

?

resilience4j:circuitbreaker:configs:default:ringBufferSizeInClosedState: 5 # 熔斷器關(guān)閉時(shí)的緩沖區(qū)大小ringBufferSizeInHalfOpenState: 2 # 熔斷器半開時(shí)的緩沖區(qū)大小waitDurationInOpenState: 10000 # 熔斷器從打開到半開需要的時(shí)間failureRateThreshold: 60 # 熔斷器打開的失敗閾值eventConsumerBufferSize: 10 # 事件緩沖區(qū)大小registerHealthIndicator: true # 健康監(jiān)測automaticTransitionFromOpenToHalfOpenEnabled: false # 是否自動從打開到半開,不需要觸發(fā)recordFailurePredicate: com.example.resilience4j.exceptions.RecordFailurePredicate # 謂詞設(shè)置異常是否為失敗recordExceptions: # 記錄的異常- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAExceptionignoreExceptions: # 忽略的異常- com.example.resilience4j.exceptions.BusinessAExceptioninstances:backendA:baseConfig: defaultwaitDurationInOpenState: 5000failureRateThreshold: 20backendB:baseConfig: default

可以配置多個(gè)熔斷器實(shí)例,使用不同配置或者覆蓋配置。

需要保護(hù)的后端服務(wù)

以一個(gè)查找用戶列表的后端服務(wù)為例,利用熔斷器保護(hù)該服務(wù)。

?

interface RemoteService {List<User> process() throws TimeoutException, InterruptedException; }

連接器調(diào)用該服務(wù)

這是調(diào)用遠(yuǎn)端服務(wù)的連接器,我們通過調(diào)用連接器中的方法來調(diào)用后端服務(wù)。

?

public RemoteServiceConnector{public List<User> process() throws TimeoutException, InterruptedException {List<User> users;users = remoteServic.process();return users;} }

用于監(jiān)控熔斷器狀態(tài)及事件的工具類

要想學(xué)習(xí)各個(gè)配置項(xiàng)的作用,需要獲取特定時(shí)候的熔斷器狀態(tài),寫一個(gè)工具類:

?

@Log4j2 public class CircuitBreakerUtil {/*** @Description: 獲取熔斷器的狀態(tài)*/public static void getCircuitBreakerStatus(String time, CircuitBreaker circuitBreaker){CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();// Returns the failure rate in percentage.float failureRate = metrics.getFailureRate();// Returns the current number of buffered calls.int bufferedCalls = metrics.getNumberOfBufferedCalls();// Returns the current number of failed calls.int failedCalls = metrics.getNumberOfFailedCalls();// Returns the current number of successed calls.int successCalls = metrics.getNumberOfSuccessfulCalls();// Returns the max number of buffered calls.int maxBufferCalls = metrics.getMaxNumberOfBufferedCalls();// Returns the current number of not permitted calls.long notPermittedCalls = metrics.getNumberOfNotPermittedCalls();log.info(time + "state=" +circuitBreaker.getState() + " , metrics[ failureRate=" + failureRate +", bufferedCalls=" + bufferedCalls +", failedCalls=" + failedCalls +", successCalls=" + successCalls +", maxBufferCalls=" + maxBufferCalls +", notPermittedCalls=" + notPermittedCalls +" ]");}/*** @Description: 監(jiān)聽熔斷器事件*/public static void addCircuitBreakerListener(CircuitBreaker circuitBreaker){circuitBreaker.getEventPublisher().onSuccess(event -> log.info("服務(wù)調(diào)用成功:" + event.toString())).onError(event -> log.info("服務(wù)調(diào)用失敗:" + event.toString())).onIgnoredError(event -> log.info("服務(wù)調(diào)用失敗,但異常被忽略:" + event.toString())).onReset(event -> log.info("熔斷器重置:" + event.toString())).onStateTransition(event -> log.info("熔斷器狀態(tài)改變:" + event.toString())).onCallNotPermitted(event -> log.info(" 熔斷器已經(jīng)打開:" + event.toString()));}

調(diào)用方法

CircuitBreaker目前支持兩種方式調(diào)用,一種是程序式調(diào)用,一種是AOP使用注解的方式調(diào)用。

程序式的調(diào)用方法

CircuitService中先注入注冊器,然后用注冊器通過熔斷器名稱獲取熔斷器。如果不需要使用降級函數(shù),可以直接調(diào)用熔斷器的executeSupplier方法或executeCheckedSupplier方法:

?

public class CircuitBreakerServiceImpl{@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;public List<User> circuitBreakerNotAOP() throws Throwable {CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行開始前:", circuitBreaker);circuitBreaker.executeCheckedSupplier(remotServiceConnector::process);} }

如果需要使用降級函數(shù),則要使用decorate包裝服務(wù)的方法,再使用Try.of().recover()進(jìn)行降級處理,同時(shí)也可以根據(jù)不同的異常使用不同的降級方法:

?

public class CircuitBreakerServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;public List<User> circuitBreakerNotAOP(){// 通過注冊器獲取熔斷器的實(shí)例CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行開始前:", circuitBreaker);// 使用熔斷器包裝連接器的方法CheckedFunction0<List<User>> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, remoteServiceConnector::process);// 使用Try.of().recover()調(diào)用并進(jìn)行降級處理Try<List<User>> result = Try.of(checkedSupplier).recover(CallNotPermittedException.class, throwable -> {log.info("熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~");CircuitBreakerUtil.getCircuitBreakerStatus("熔斷器打開中:", circuitBreaker);List<User> users = new ArrayList();return users;}).recover(throwable -> {log.info(throwable.getLocalizedMessage() + ",方法被降級了~~");CircuitBreakerUtil.getCircuitBreakerStatus("降級方法中:",circuitBreaker);List<User> users = new ArrayList();return users;});CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行結(jié)束后:", circuitBreaker);return result.get();} }

AOP式的調(diào)用方法

首先在連接器方法上使用@CircuitBreaker(name="",fallbackMethod="")注解,其中name是要使用的熔斷器的名稱,fallbackMethod是要使用的降級方法,降級方法必須和原方法放在同一個(gè)類中,且降級方法的返回值需要和原方法相同,輸入?yún)?shù)需要添加額外的exception參數(shù),類似這樣:

?

public RemoteServiceConnector{@CircuitBreaker(name = "backendA", fallbackMethod = "fallBack")public List<User> process() throws TimeoutException, InterruptedException {List<User> users;users = remoteServic.process();return users;}private List<User> fallBack(Throwable throwable){log.info(throwable.getLocalizedMessage() + ",方法被降級了~~");CircuitBreakerUtil.getCircuitBreakerStatus("降級方法中:", circuitBreakerRegistry.circuitBreaker("backendA"));List<User> users = new ArrayList();return users;}private List<User> fallBack(CallNotPermittedException e){log.info("熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~");CircuitBreakerUtil.getCircuitBreakerStatus("熔斷器打開中:", circuitBreakerRegistry.circuitBreaker("backendA"));List<User> users = new ArrayList();return users;}}

可使用多個(gè)降級方法,保持方法名相同,同時(shí)滿足的條件的降級方法會觸發(fā)最接近的一個(gè)(這里的接近是指類型的接近,先會觸發(fā)離它最近的子類異常),例如如果process()方法拋出CallNotPermittedException,將會觸發(fā)fallBack(CallNotPermittedException e)方法而不會觸發(fā)fallBack(Throwable throwable)方法。

之后直接調(diào)用方法就可以了:

?

public class CircuitBreakerServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;public List<User> circuitBreakerAOP() throws TimeoutException, InterruptedException {CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行開始前:",circuitBreakerRegistry.circuitBreaker("backendA"));List<User> result = remoteServiceConnector.process();CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行結(jié)束后:", circuitBreakerRegistry.circuitBreaker("backendA"));return result;} }

使用測試

接下來進(jìn)入測試,首先我們定義了兩個(gè)異常,異常A同時(shí)在黑白名單中,異常B只在黑名單中:

?

recordExceptions: # 記錄的異常- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAException ignoreExceptions: # 忽略的異常- com.example.resilience4j.exceptions.BusinessAException

然后對被保護(hù)的后端接口進(jìn)行如下的實(shí)現(xiàn):

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() {int num = count.getAndIncrement();log.info("count的值 = " + num);if (num % 4 == 1){throw new BusinessAException("異常A,不需要被記錄");}if (num % 4 == 2 || num % 4 == 3){throw new BusinessBException("異常B,需要被記錄");}log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫的正常查詢r(jià)eturn repository.findAll();} }

使用CircuitBreakerServiceImpl中的AOP或者程序式調(diào)用方法進(jìn)行單元測試,循環(huán)調(diào)用10次:

?

public class CircuitBreakerServiceImplTest{@Autowiredprivate CircuitBreakerServiceImpl circuitService;@Testpublic void circuitBreakerTest() {for (int i=0; i<10; i++){// circuitService.circuitBreakerAOP();circuitService.circuitBreakerNotAOP();}} }

看下運(yùn)行結(jié)果:

?

執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=0, failedCalls=0, successCalls=0, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 1 異常A,不需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 2 異常B,需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=1, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=1, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=1, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 3 異常B,需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=2, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=2, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=2, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 4 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=4, failedCalls=2, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=4, failedCalls=2, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 5 異常A,不需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=4, failedCalls=2, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=4, failedCalls=2, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=4, failedCalls=2, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 6 異常B,需要被記錄,方法被降級了~~ 降級方法中:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~ 熔斷器打開中:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=1 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=1 ]

注意到異常A發(fā)生的前后bufferedCallsfailedCallssuccessCalls三個(gè)參數(shù)的值都沒有沒有發(fā)生變化,說明白名單的優(yōu)先級高于黑名單,源碼中也有提到Ignoring an exception has priority over recording an exception

?

/** * @see #ignoreExceptions(Class[]) ). Ignoring an exception has priority over recording an exception. * <p> * Example: * recordExceptions(Throwable.class) and ignoreExceptions(RuntimeException.class) * would capture all Errors and checked Exceptions, and ignore unchecked * <p> */

同時(shí)也可以看出白名單所謂的忽略,是指不計(jì)入緩沖區(qū)中(即不算成功也不算失敗),有降級方法會調(diào)用降級方法,沒有降級方法會拋出異常,和其他異常無異。

?

執(zhí)行開始前:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~ 熔斷器打開中:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=1 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=1 ]

當(dāng)環(huán)形緩沖區(qū)大小被填滿時(shí)會計(jì)算失敗率,這時(shí)請求會被拒絕獲取不到count的值,且notPermittedCalls會增加。


接下來我們實(shí)驗(yàn)一下多線程下熔斷器關(guān)閉和熔斷器半開兩種情況下緩沖環(huán)的區(qū)別,我們先開15個(gè)線程進(jìn)行調(diào)用測試熔斷器關(guān)閉時(shí)的緩沖環(huán),熔斷之后等10s再開15個(gè)線程進(jìn)行調(diào)用測試熔斷器半開時(shí)的緩沖環(huán):

?

public class CircuitBreakerServiceImplTest{@Autowiredprivate CircuitBreakerServiceImpl circuitService;@Testpublic void circuitBreakerThreadTest() throws InterruptedException {ExecutorService pool = Executors.newCachedThreadPool();for (int i=0; i<15; i++){pool.submit(// circuitService::circuitBreakerAOPcircuitService::circuitBreakerNotAOP);}pool.shutdown();while (!pool.isTerminated());Thread.sleep(10000);log.info("熔斷器狀態(tài)已轉(zhuǎn)為半開");pool = Executors.newCachedThreadPool();for (int i=0; i<15; i++){pool.submit(// circuitService::circuitBreakerAOPcircuitService::circuitBreakerNotAOP);}pool.shutdown();while (!pool.isTerminated());for (int i=0; i<10; i++){}} }

15個(gè)線程都通過了熔斷器,由于正常返回需要查數(shù)據(jù)庫,所以會慢很多,失敗率很快就達(dá)到了100%,而且觀察到如下的記錄:

?

異常B,需要被記錄,方法被降級了~~ 降級方法中:state=OPEN , metrics[ failureRate=100.0, bufferedCalls=5, failedCalls=5, successCalls=0, maxBufferCalls=5, notPermittedCalls=0 ]

可以看出,雖然熔斷器已經(jīng)打開了,可是異常B還是進(jìn)入了降級方法,拋出的異常不是notPermittedCalls數(shù)量為0,說明在熔斷器轉(zhuǎn)換成打開之前所有請求都通過了熔斷器,緩沖環(huán)不會控制線程的并發(fā)。

?

執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=80.0, bufferedCalls=5, failedCalls=4, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=60.0, bufferedCalls=5, failedCalls=3, successCalls=2, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=40.0, bufferedCalls=5, failedCalls=2, successCalls=3, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=OPEN , metrics[ failureRate=20.0, bufferedCalls=5, failedCalls=1, successCalls=4, maxBufferCalls=5, notPermittedCalls=0 ]

同時(shí)以上幾條正常執(zhí)行的服務(wù)完成后,熔斷器的失敗率在下降,說明熔斷器打開狀態(tài)下還是會計(jì)算失敗率,由于環(huán)形緩沖區(qū)大小為5,初步推斷成功的狀態(tài)會依次覆蓋最開始的幾個(gè)狀態(tài),所以得到了上述結(jié)果。

接下來分析后15個(gè)線程的結(jié)果

?

熔斷器狀態(tài)已轉(zhuǎn)為半開 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 16 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行開始前:state=OPEN , metrics[ failureRate=0.0, bufferedCalls=5, failedCalls=0, successCalls=5, maxBufferCalls=5, notPermittedCalls=0 ] 熔斷器狀態(tài)改變:2019-07-29T17:19:19.959+08:00[Asia/Shanghai]: CircuitBreaker 'backendA' changed state from OPEN to HALF_OPEN count的值 = 18 count的值 = 17 服務(wù)正常運(yùn)行,獲取用戶列表 count的值 = 19 count的值 = 15

熔斷器狀態(tài)從打開到半開我設(shè)置的是5s,前15個(gè)線程調(diào)用之后我等待了10s,熔斷器應(yīng)該已經(jīng)變?yōu)榘腴_了,但是執(zhí)行開始前熔斷器的狀態(tài)卻是OPEN,這是因?yàn)槟J(rèn)的配置項(xiàng)automaticTransitionFromOpenToHalfOpenEnabled=false,時(shí)間到了也不會自動轉(zhuǎn)換,需要有新的請求來觸發(fā)熔斷器的狀態(tài)轉(zhuǎn)換。同時(shí)我們發(fā)現(xiàn),好像狀態(tài)改變后還是進(jìn)了超過4個(gè)請求,似乎半開狀態(tài)的環(huán)并不能限制線程數(shù)?這是由于這些進(jìn)程是在熔斷器打開時(shí)一起進(jìn)來的。為了更好的觀察環(huán)半開時(shí)候環(huán)大小是否限制線程數(shù),我們修改一下配置:

?

resilience4j:circuitbreaker:configs:myDefault:automaticTransitionFromOpenToHalfOpenEnabled: true # 是否自動從打開到半開

我們再試一次:

?

熔斷器狀態(tài)已轉(zhuǎn)為半開 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=0, failedCalls=0, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=0, failedCalls=0, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=0, failedCalls=0, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] count的值 = 15 count的值 = 16 服務(wù)正常運(yùn)行,獲取用戶列表異常B,需要被記錄,方法被降級了~~ 降級方法中:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=1, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=1, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] count的值 = 17 異常A,不需要被記錄,方法被降級了~~ 降級方法中:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=2, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=2, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] count的值 = 18 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=2, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 異常B,需要被記錄,方法被降級了~~ 降級方法中:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=3, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=3, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 熔斷器已經(jīng)打開:2019-07-29T17:36:14.189+08:00[Asia/Shanghai]: CircuitBreaker 'backendA' recorded a call which was not permitted. 執(zhí)行開始前:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=2, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=HALF_OPEN , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=2, successCalls=0, maxBufferCalls=4, notPermittedCalls=0 ] 熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~

結(jié)果只有4個(gè)請求進(jìn)去了,可以看出雖然熔斷器狀態(tài)還是半開,但是已經(jīng)熔斷了,說明在半開狀態(tài)下,超過環(huán)大小的請求會被直接拒絕。

綜上,circuitbreaker的機(jī)制已經(jīng)被證實(shí),且十分清晰,以下為幾個(gè)需要注意的點(diǎn):

  • 失敗率的計(jì)算必須等環(huán)裝滿才會計(jì)算
  • 白名單優(yōu)先級高于黑名單且白名單上的異常會被忽略,不會占用緩沖環(huán)位置,即不會計(jì)入失敗率計(jì)算
  • 熔斷器打開時(shí)同樣會計(jì)算失敗率,當(dāng)狀態(tài)轉(zhuǎn)換為半開時(shí)重置為-1
  • 只要出現(xiàn)異常都可以調(diào)用降級方法,不論是在白名單還是黑名單
  • 熔斷器的緩沖環(huán)有兩個(gè),一個(gè)關(guān)閉時(shí)的緩沖環(huán),一個(gè)打開時(shí)的緩沖環(huán)
  • 熔斷器關(guān)閉時(shí),直至熔斷器狀態(tài)轉(zhuǎn)換前所有請求都會通過,不會受到限制
  • 熔斷器半開時(shí),限制請求數(shù)為緩沖環(huán)的大小,其他請求會等待
  • 熔斷器從打開到半開的轉(zhuǎn)換默認(rèn)還需要請求進(jìn)行觸發(fā),也可通過automaticTransitionFromOpenToHalfOpenEnabled=true設(shè)置為自動觸發(fā)

TimeLimiter

簡介

Hystrix不同,Resilience4j將超時(shí)控制器從熔斷器中獨(dú)立出來,成為了一個(gè)單獨(dú)的組件,主要的作用就是對方法調(diào)用進(jìn)行超時(shí)控制。實(shí)現(xiàn)的原理和Hystrix相似,都是通過調(diào)用Futureget方法來進(jìn)行超時(shí)控制。

可配置參數(shù)

配置參數(shù)默認(rèn)值描述
timeoutDuration1(s)超時(shí)時(shí)間限定
cancelRunningFuturetrue當(dāng)超時(shí)時(shí)是否關(guān)閉取消線程

測試前準(zhǔn)備

pom.xml

?

<dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-timelimiter</artifactId><version>0.16.0</version> </dependency>

TimeLimiter沒有整合進(jìn)resilience4j-spring-boot中,需要單獨(dú)添加依賴

application.yml配置

?

timelimiter:timeoutDuration: 3000 # 超時(shí)時(shí)長cancelRunningFuture: true # 發(fā)生異常是否關(guān)閉線程

TimeLimiter沒有配置自動注入,需要自己進(jìn)行注入,寫下面兩個(gè)文件進(jìn)行配置自動注入:

TimeLimiterProperties

用于將application.yml中的配置轉(zhuǎn)換為TimeLimiterProperties對象:

?

@Data @Component @ConfigurationProperties(prefix = "resilience4j.timelimiter") public class TimeLimiterProperties {private Duration timeoutDuration;private boolean cancelRunningFuture; }

TimeLimiterConfiguration

TimeLimiterProperties對象寫入到TimeLimiter的配置中:

?

@Configuration public class TimeLimiterConfiguration {@Autowiredprivate TimeLimiterProperties timeLimiterProperties;@Beanpublic TimeLimiter timeLimiter(){return TimeLimiter.of(timeLimiterConfig());}private TimeLimiterConfig timeLimiterConfig(){return TimeLimiterConfig.custom().timeoutDuration(timeLimiterProperties.getTimeoutDuration()).cancelRunningFuture(timeLimiterProperties.isCancelRunningFuture()).build();} }

調(diào)用方法

還是以之前查詢用戶列表的后端服務(wù)為例。TimeLimiter目前僅支持程序式調(diào)用,還不能使用AOP的方式調(diào)用。

因?yàn)?strong>TimeLimiter通常與CircuitBreaker聯(lián)合使用,很少單獨(dú)使用,所以直接介紹聯(lián)合使用的步驟。

TimeLimiter沒有注冊器,所以通過@Autowired注解自動注入依賴直接使用,因?yàn)?strong>TimeLimter是基于Futureget方法的,所以需要?jiǎng)?chuàng)建線程池,然后通過線程池的submit方法獲取Future對象:

?

public class CircuitBreakerServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;@Autowiredprivate TimeLimiter timeLimiter;public List<User> circuitBreakerTimeLimiter(){// 通過注冊器獲取熔斷器的實(shí)例CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行開始前:", circuitBreaker);// 創(chuàng)建單線程的線程池ExecutorService pool = Executors.newSingleThreadExecutor();//將被保護(hù)方法包裝為能夠返回Future的supplier函數(shù)Supplier<Future<List<User>>> futureSupplier = () -> pool.submit(remoteServiceConnector::process);// 先用限時(shí)器包裝,再用熔斷器包裝Callable<List<User>> restrictedCall = TimeLimiter.decorateFutureSupplier(timeLimiter, futureSupplier);Callable<List<User>> chainedCallable = CircuitBreaker.decorateCallable(circuitBreaker, restrictedCall);// 使用Try.of().recover()調(diào)用并進(jìn)行降級處理Try<List<User>> result = Try.of(chainedCallable::call).recover(CallNotPermittedException.class, throwable ->{log.info("熔斷器已經(jīng)打開,拒絕訪問被保護(hù)方法~");CircuitBreakerUtil.getCircuitBreakerStatus("熔斷器打開中", circuitBreaker);List<User> users = new ArrayList();return users;}).recover(throwable -> {log.info(throwable.getLocalizedMessage() + ",方法被降級了~~");CircuitBreakerUtil.getCircuitBreakerStatus("降級方法中:",circuitBreaker);List<User> users = new ArrayList();return users;});CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行結(jié)束后:", circuitBreaker);return result.get();} }

使用測試

異常ABapplication.yml文件中沒有修改:

?

recordExceptions: # 記錄的異常- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAException ignoreExceptions: # 忽略的異常- com.example.resilience4j.exceptions.BusinessAException

使用另一個(gè)遠(yuǎn)程服務(wù)接口的實(shí)現(xiàn),將num%4==3的情況讓線程休眠5s,大于我們TimeLimiter的限制時(shí)間:

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() {int num = count.getAndIncrement();log.info("count的值 = " + num);if (num % 4 == 1){throw new BusinessAException("異常A,不需要被記錄");}if (num % 4 == 2){throw new BusinessBException("異常B,需要被記錄");}if (num % 4 == 3){Thread.sleep(5000);}log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫的正常查詢r(jià)eturn repository.findAll();} }

把調(diào)用方法進(jìn)行單元測試,循環(huán)10遍:

?

public class CircuitBreakerServiceImplTest{@Autowiredprivate CircuitBreakerServiceImpl circuitService;@Testpublic void circuitBreakerTimeLimiterTest() {for (int i=0; i<10; i++){circuitService.circuitBreakerTimeLimiter();}} }

看下運(yùn)行結(jié)果:

?

執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=0, failedCalls=0, successCalls=0, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 1 com.example.resilience4j.exceptions.BusinessAException: 異常A,不需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 2 com.example.resilience4j.exceptions.BusinessBException: 異常B,需要被記錄,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行開始前:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 3 null,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ]

發(fā)現(xiàn)熔斷器任何異常和超時(shí)都沒有失敗。。完全不會觸發(fā)熔斷,這是為什么呢?我們把異常toString()看一下:

?

java.util.concurrent.ExecutionException: com.example.resilience4j.exceptions.BusinessBException: 異常B,需要被記錄,方法被降級了~~ java.util.concurrent.TimeoutException,方法被降級了~~

這下原因就很明顯了,線程池會將線程中的任何異常包裝為ExecutionException,而熔斷器沒有把異常解包,由于我們設(shè)置了黑名單,而熔斷器又沒有找到黑名單上的異常,所以失效了。這是一個(gè)已知的bug,會在下個(gè)版本(0.16.0之后)中修正,目前來說如果需要同時(shí)使用TimeLimiterCircuitBreaker的話,黑白名單的設(shè)置是不起作用的,需要自定義自己的謂詞邏輯,并在test()方法中將異常解包進(jìn)行判斷,比如像下面這樣:

?

public class RecordFailurePredicate implements Predicate<Throwable> {@Overridepublic boolean test(Throwable throwable) {if (throwable.getCause() instanceof BusinessAException) return false;else return true;} }

然后在application.yml文件中指定這個(gè)類作為判斷類:

?

circuitbreaker:configs:default:recordFailurePredicate: com.example.resilience4j.predicate.RecordFailurePredicate

就能自定義自己的黑白名單了,我們再運(yùn)行一次試試:

?

java.util.concurrent.TimeoutException,方法被降級了~~ 降級方法中:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=2, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] 執(zhí)行結(jié)束后:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=3, failedCalls=2, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ]

可以看出,TimeLimiter已經(jīng)生效了,同時(shí)CircuitBreaker也正常工作。

Note:

最新版0.17.0,該bug已經(jīng)修復(fù),黑白名單可以正常使用。

Retry

簡介

同熔斷器一樣,重試組件也提供了注冊器,可以通過注冊器獲取實(shí)例來進(jìn)行重試,同樣可以跟熔斷器配合使用。

可配置參數(shù)

配置參數(shù)默認(rèn)值描述
maxAttempts3最大重試次數(shù)
waitDuration500[ms]固定重試間隔
intervalFunctionnumberOfAttempts -> waitDuration用來改變重試時(shí)間間隔,可以選擇指數(shù)退避或者隨機(jī)時(shí)間間隔
retryOnResultPredicateresult -> false自定義結(jié)果重試規(guī)則,需要重試的返回true
retryOnExceptionPredicatethrowable -> true自定義異常重試規(guī)則,需要重試的返回true
retryExceptionsempty需要重試的異常列表
ignoreExceptionsempty需要忽略的異常列表

測試前準(zhǔn)備

pom.xml

不需要引入新的依賴,已經(jīng)集成在resilience4j-spring-boot中了

application.yml配置

?

resilience4j:retry:configs:default:maxRetryAttempts: 3waitDuration: 10senableExponentialBackoff: true # 是否允許使用指數(shù)退避算法進(jìn)行重試間隔時(shí)間的計(jì)算expontialBackoffMultiplier: 2 # 指數(shù)退避算法的乘數(shù)enableRandomizedWait: false # 是否允許使用隨機(jī)的重試間隔randomizedWaitFactor: 0.5 # 隨機(jī)因子resultPredicate: com.example.resilience4j.predicate.RetryOnResultPredicate retryExceptionPredicate: com.example.resilience4j.predicate.RetryOnExceptionPredicateretryExceptions:- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAException- io.github.resilience4j.circuitbreaker.CallNotPermittedExceptionignoreExceptions:- io.github.resilience4j.circuitbreaker.CallNotPermittedExceptioninstances:backendA:baseConfig: defaultwaitDuration: 5sbackendB:baseConfig: defaultmaxRetryAttempts: 2

application.yml可以配置的參數(shù)多出了幾個(gè)enableExponentialBackoffexpontialBackoffMultiplierenableRandomizedWaitrandomizedWaitFactor,分別代表是否允許指數(shù)退避間隔時(shí)間,指數(shù)退避的乘數(shù)、是否允許隨機(jī)間隔時(shí)間、隨機(jī)因子,注意指數(shù)退避和隨機(jī)間隔不能同時(shí)啟用。

用于監(jiān)控重試組件狀態(tài)及事件的工具類

同樣為了監(jiān)控重試組件,寫一個(gè)工具類:

?

@Log4j2 public class RetryUtil {/*** @Description: 獲取重試的狀態(tài)*/public static void getRetryStatus(String time, Retry retry){Retry.Metrics metrics = retry.getMetrics();long failedRetryNum = metrics.getNumberOfFailedCallsWithRetryAttempt();long failedNotRetryNum = metrics.getNumberOfFailedCallsWithoutRetryAttempt();long successfulRetryNum = metrics.getNumberOfSuccessfulCallsWithRetryAttempt();long successfulNotyRetryNum = metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt();log.info(time + "state=" + " metrics[ failedRetryNum=" + failedRetryNum +", failedNotRetryNum=" + failedNotRetryNum +", successfulRetryNum=" + successfulRetryNum +", successfulNotyRetryNum=" + successfulNotyRetryNum +" ]");}/*** @Description: 監(jiān)聽重試事件*/public static void addRetryListener(Retry retry){retry.getEventPublisher().onSuccess(event -> log.info("服務(wù)調(diào)用成功:" + event.toString())).onError(event -> log.info("服務(wù)調(diào)用失敗:" + event.toString())).onIgnoredError(event -> log.info("服務(wù)調(diào)用失敗,但異常被忽略:" + event.toString())).onRetry(event -> log.info("重試:第" + event.getNumberOfRetryAttempts() + "次"));} }

調(diào)用方法

還是以之前查詢用戶列表的服務(wù)為例。Retry支持AOP和程序式兩種方式的調(diào)用.

程序式的調(diào)用方法

CircuitBreaker的調(diào)用方式差不多,和熔斷器配合使用有兩種調(diào)用方式,一種是先用重試組件裝飾,再用熔斷器裝飾,這時(shí)熔斷器的失敗需要等重試結(jié)束才計(jì)算,另一種是先用熔斷器裝飾,再用重試組件裝飾,這時(shí)每次調(diào)用服務(wù)都會記錄進(jìn)熔斷器的緩沖環(huán)中,需要注意的是,第二種方式需要把CallNotPermittedException放進(jìn)重試組件的白名單中,因?yàn)槿蹟嗥鞔蜷_時(shí)重試是沒有意義的:

?

public class CircuitBreakerServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;@Autowiredprivate RetryRegistry retryRegistry;public List<User> circuitBreakerRetryNotAOP(){// 通過注冊器獲取熔斷器的實(shí)例CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");// 通過注冊器獲取重試組件實(shí)例Retry retry = retryRegistry.retry("backendA");CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行開始前:", circuitBreaker);// 先用重試組件包裝,再用熔斷器包裝CheckedFunction0<List<User>> checkedSupplier = Retry.decorateCheckedSupplier(retry, remoteServiceConnector::process);CheckedFunction0<List<User>> chainedSupplier = CircuitBreaker .decorateCheckedSupplier(circuitBreaker, checkedSupplier);// 使用Try.of().recover()調(diào)用并進(jìn)行降級處理Try<List<User>> result = Try.of(chainedSupplier).recover(CallNotPermittedException.class, throwable -> {log.info("已經(jīng)被熔斷,停止重試");return new ArrayList<>();}).recover(throwable -> {log.info("重試失敗: " + throwable.getLocalizedMessage());return new ArrayList<>();});RetryUtil.getRetryStatus("執(zhí)行結(jié)束: ", retry);CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行結(jié)束:", circuitBreaker);return result.get();} }

AOP式的調(diào)用方法

首先在連接器方法上使用@Retry(name="",fallbackMethod="")注解,其中name是要使用的重試器實(shí)例的名稱,fallbackMethod是要使用的降級方法:

?

public RemoteServiceConnector{@CircuitBreaker(name = "backendA", fallbackMethod = "fallBack")@Retry(name = "backendA", fallbackMethod = "fallBack")public List<User> process() throws TimeoutException, InterruptedException {List<User> users;users = remoteServic.process();return users;} }

要求和熔斷器一致,但是需要注意同時(shí)注解重試組件和熔斷器的話,是按照第二種方案來的,即每一次請求都會被熔斷器記錄。

之后直接調(diào)用方法:

?

public class CircuitBreakerServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate CircuitBreakerRegistry circuitBreakerRegistry;@Autowiredprivate RetryRegistry retryRegistry;public List<User> circuitBreakerRetryAOP() throws TimeoutException, InterruptedException {List<User> result = remoteServiceConnector.process();RetryUtil.getRetryStatus("執(zhí)行結(jié)束:", retryRegistry.retry("backendA"));CircuitBreakerUtil.getCircuitBreakerStatus("執(zhí)行結(jié)束:", circuitBreakerRegistry.circuitBreaker("backendA"));return result;} }

使用測試

異常ABapplication.yml文件中設(shè)定為都需要重試,因?yàn)槭褂玫谝环N方案,所以不需要將CallNotPermittedException設(shè)定在重試組件的白名單中,同時(shí)為了測試重試過程中的異常是否會被熔斷器記錄,將異常A從熔斷器白名單中去除:

?

recordExceptions: # 記錄的異常- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAException ignoreExceptions: # 忽略的異常 # - com.example.resilience4j.exceptions.BusinessAException # ... resultPredicate: com.example.resilience4j.predicate.RetryOnResultPredicate retryExceptions:- com.example.resilience4j.exceptions.BusinessBException- com.example.resilience4j.exceptions.BusinessAException- io.github.resilience4j.circuitbreaker.CallNotPermittedException ignoreExceptions: # - io.github.resilience4j.circuitbreaker.CallNotPermittedException

使用另一個(gè)遠(yuǎn)程服務(wù)接口的實(shí)現(xiàn),將num%4==2的情況返回null,測試根據(jù)返回結(jié)果進(jìn)行重試的功能:

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() {int num = count.getAndIncrement();log.info("count的值 = " + num);if (num % 4 == 1){throw new BusinessAException("異常A,需要重試");}if (num % 4 == 2){return null;}if (num % 4 == 3){throw new BusinessBException("異常B,需要重試");}log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫的正常查詢r(jià)eturn repository.findAll();} }

同時(shí)添加一個(gè)類自定義哪些返回值需要重試,設(shè)定為返回值為空就進(jìn)行重試,這樣num % 4 == 2時(shí)就可以測試不拋異常,根據(jù)返回結(jié)果進(jìn)行重試了:

?

public class RetryOnResultPredicate implements Predicate {@Overridepublic boolean test(Object o) {return o == null ? true : false;} }

使用CircuitBreakerServiceImpl中的AOP或者程序式調(diào)用方法進(jìn)行單元測試,循環(huán)調(diào)用10次:

?

public class CircuitBreakerServiceImplTest{@Autowiredprivate CircuitBreakerServiceImpl circuitService;@Testpublic void circuitBreakerRetryTest() {for (int i=0; i<10; i++){// circuitService.circuitBreakerRetryAOP();circuitService.circuitBreakerRetryNotAOP();}} }

看一下運(yùn)行結(jié)果:

?

count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: state= metrics[ failedRetryNum=0, failedNotRetryNum=0, successfulRetryNum=0, successfulNotyRetryNum=1 ] 執(zhí)行結(jié)束:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 1 重試:第1次 count的值 = 2 重試:第2次 count的值 = 3 服務(wù)調(diào)用失敗:2019-07-09T19:06:59.705+08:00[Asia/Shanghai]: Retry 'backendA' recorded a failed retry attempt. Number of retry attempts: '3', Last exception was: 'com.example.resilience4j.exceptions.BusinessBException: 異常B,需要重試'. 重試失敗: 異常B,需要重試 執(zhí)行結(jié)束: state= metrics[ failedRetryNum=1, failedNotRetryNum=0, successfulRetryNum=0, successfulNotyRetryNum=1 ] 執(zhí)行結(jié)束:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=2, failedCalls=1, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ]

這部分結(jié)果可以看出來,重試最大次數(shù)設(shè)置為3結(jié)果其實(shí)只重試了2次,服務(wù)共執(zhí)行了3次,重試3次后熔斷器只記錄了1次。而且返回值為null時(shí)也確實(shí)進(jìn)行重試了。

?

服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: state= metrics[ failedRetryNum=2, failedNotRetryNum=0, successfulRetryNum=0, successfulNotyRetryNum=3 ] 執(zhí)行結(jié)束:state=OPEN , metrics[ failureRate=40.0, bufferedCalls=5, failedCalls=2, successCalls=3, maxBufferCalls=5, notPermittedCalls=0 ] 已經(jīng)被熔斷,停止重試 執(zhí)行結(jié)束: state= metrics[ failedRetryNum=2, failedNotRetryNum=0, successfulRetryNum=0, successfulNotyRetryNum=3 ] 執(zhí)行結(jié)束:state=OPEN , metrics[ failureRate=40.0, bufferedCalls=5, failedCalls=2, successCalls=3, maxBufferCalls=5, notPermittedCalls=1 ]

當(dāng)熔斷之后不會再進(jìn)行重試。

接下來我修改一下調(diào)用服務(wù)的實(shí)現(xiàn):

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() {int num = count.getAndIncrement();log.info("count的值 = " + num);if (num % 4 == 1){throw new BusinessAException("異常A,需要重試");}if (num % 4 == 3){return null;}if (num % 4 == 2){throw new BusinessBException("異常B,需要重試");}log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫的正常查詢r(jià)eturn repository.findAll();} }

num%4==2變成異常Bnum%4==3變成返回null,看一下最后一次重試返回值為null屬于重試成功還是重試失敗。

運(yùn)行結(jié)果如下:

?

count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: state= metrics[ failedRetryNum=0, failedNotRetryNum=0, successfulRetryNum=0, successfulNotyRetryNum=1 ] 執(zhí)行結(jié)束:state=CLOSED , metrics[ failureRate=-1.0, bufferedCalls=1, failedCalls=0, successCalls=1, maxBufferCalls=5, notPermittedCalls=0 ] count的值 = 1 重試:第1次 count的值 = 2 重試:第2次 count的值 = 3 服務(wù)調(diào)用成功:2019-07-09T19:17:35.836+08:00[Asia/Shanghai]: Retry 'backendA' recorded a successful retry attempt. Number of retry attempts: '3', Last exception was: 'com.example.resilience4j.exceptions.BusinessBException: 異常B,需要重試'.

如上可知如果最后一次重試不拋出異常就算作重試成功,不管結(jié)果是否需要繼續(xù)重試。

Bulkhead

簡介

Resilence4jBulkhead提供兩種實(shí)現(xiàn),一種是基于信號量的,另一種是基于有等待隊(duì)列的固定大小的線程池的,由于基于信號量的Bulkhead能很好地在多線程和I/O模型下工作,所以選擇介紹基于信號量的Bulkhead的使用。

可配置參數(shù)

配置參數(shù)默認(rèn)值描述
maxConcurrentCalls25可允許的最大并發(fā)線程數(shù)
maxWaitDuration0嘗試進(jìn)入飽和艙壁時(shí)應(yīng)阻止線程的最大時(shí)間

測試前準(zhǔn)備

pom.xml

不需要引入新的依賴,已經(jīng)集成在resilience4j-spring-boot中了

application.yml配置

?

resilience4j:bulkhead:configs:default:maxConcurrentCalls: 10maxWaitDuration: 1000instances:backendA:baseConfig: defaultmaxConcurrentCalls: 3backendB:baseConfig: defaultmaxWaitDuration: 100

CircuitBreaker差不多,都是可以通過繼承覆蓋配置設(shè)定實(shí)例的。

用于監(jiān)控Bulkhead狀態(tài)及事件的工具類

同樣為了監(jiān)控Bulkhead組件,寫一個(gè)工具類:

?

@Log4j2 public class BulkhdadUtil {/*** @Description: 獲取bulkhead的狀態(tài)*/public static void getBulkheadStatus(String time, Bulkhead bulkhead){Bulkhead.Metrics metrics = bulkhead.getMetrics();// Returns the number of parallel executions this bulkhead can support at this point in time.int availableConcurrentCalls = metrics.getAvailableConcurrentCalls();// Returns the configured max amount of concurrent callsint maxAllowedConcurrentCalls = metrics.getMaxAllowedConcurrentCalls();log.info(time + ", metrics[ availableConcurrentCalls=" + availableConcurrentCalls +", maxAllowedConcurrentCalls=" + maxAllowedConcurrentCalls + " ]");}/*** @Description: 監(jiān)聽bulkhead事件*/public static void addBulkheadListener(Bulkhead bulkhead){bulkhead.getEventPublisher().onCallFinished(event -> log.info(event.toString())).onCallPermitted(event -> log.info(event.toString())).onCallRejected(event -> log.info(event.toString()));} }

調(diào)用方法

還是以之前查詢用戶列表的服務(wù)為例。Bulkhead支持AOP和程序式兩種方式的調(diào)用。

程序式的調(diào)用方法

調(diào)用方法都類似,裝飾方法之后用Try.of().recover()來執(zhí)行:

?

public class BulkheadServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate BulkheadRegistry bulkheadRegistry;public List<User> bulkheadNotAOP(){// 通過注冊器獲得Bulkhead實(shí)例Bulkhead bulkhead = bulkheadRegistry.bulkhead("backendA");BulkhdadUtil.getBulkheadStatus("開始執(zhí)行前: ", bulkhead);// 通過Try.of().recover()調(diào)用裝飾后的服務(wù)Try<List<User>> result = Try.of(Bulkhead.decorateCheckedSupplier(bulkhead, remoteServiceConnector::process)).recover(BulkheadFullException.class, throwable -> {log.info("服務(wù)失敗: " + throwable.getLocalizedMessage());return new ArrayList();});BulkhdadUtil.getBulkheadStatus("執(zhí)行結(jié)束: ", bulkhead);return result.get();} }

AOP式的調(diào)用方法

首先在連接器方法上使用@Bulkhead(name="", fallbackMethod="", type="")注解,其中name是要使用的Bulkhead實(shí)例的名稱,fallbackMethod是要使用的降級方法,type是選擇信號量或線程池的Bulkhead

?

public RemoteServiceConnector{@Bulkhead(name = "backendA", fallbackMethod = "fallback", type = Bulkhead.Type.SEMAPHORE)public List<User> process() throws TimeoutException, InterruptedException {List<User> users;users = remoteServic.process();return users;}private List<User> fallback(BulkheadFullException e){log.info("服務(wù)失敗: " + e.getLocalizedMessage());return new ArrayList();} }

如果RetryCircuitBreakerBulkhead同時(shí)注解在方法上,默認(rèn)的順序是Retry>CircuitBreaker>Bulkhead,即先控制并發(fā)再熔斷最后重試,之后直接調(diào)用方法:

?

public class BulkheadServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate BulkheadRegistry bulkheadRegistry;public List<User> bulkheadAOP() throws TimeoutException, InterruptedException {List<User> result = remoteServiceConnector.process();BulkheadUtil.getBulkheadStatus("執(zhí)行結(jié)束:", bulkheadRegistry.retry("backendA"));return result;} }

使用測試

application.yml文件中將backenA線程數(shù)限制為1,便于觀察,最大等待時(shí)間為1s,超過1s的會走降級方法:

?

instances:backendA:baseConfig: defaultmaxConcurrentCalls: 1

使用另一個(gè)遠(yuǎn)程服務(wù)接口的實(shí)現(xiàn),不拋出異常,當(dāng)做正常服務(wù)進(jìn)行:

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() {int num = count.getAndIncrement();log.info("count的值 = " + num);log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫正常查詢r(jià)eturn repository.findAll();} }

用線程池調(diào)5個(gè)線程去請求服務(wù):

?

public class BulkheadServiceImplTest{@Autowiredprivate BulkheadServiceImpl bulkheadService;@Autowiredprivate BulkheadRegistry bulkheadRegistry;@Testpublic void bulkheadTest() {BulkhdadUtil.addBulkheadListener(bulkheadRegistry.bulkhead("backendA"));ExecutorService pool = Executors.newCachedThreadPool();for (int i=0; i<5; i++){pool.submit(() -> {// bulkheadService.bulkheadAOP();bulkheadService.bulkheadNotAOP();});}pool.shutdown();while (!pool.isTerminated());}} }

看一下運(yùn)行結(jié)果:

?

開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' permitted a call. count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 開始執(zhí)行前: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' rejected a call. Bulkhead 'backendA' rejected a call. Bulkhead 'backendA' rejected a call. Bulkhead 'backendA' rejected a call. 服務(wù)失敗: Bulkhead 'backendA' is full and does not permit further calls 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] 服務(wù)失敗: Bulkhead 'backendA' is full and does not permit further calls 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] 服務(wù)失敗: Bulkhead 'backendA' is full and does not permit further calls 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] 服務(wù)失敗: Bulkhead 'backendA' is full and does not permit further calls 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' has finished a call. 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ]

由上可以看出,5個(gè)請求只有一個(gè)進(jìn)入,其余觸發(fā)rejected事件,然后自動進(jìn)入降級方法。接下來我們把等待時(shí)間稍微加長一些:

?

instances:backendA:baseConfig: defaultmaxConcurrentCalls: 1maxWaitDuration: 5000

再運(yùn)行一次:

?

開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] 開始執(zhí)行前: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' permitted a call. count的值 = 0 服務(wù)正常運(yùn)行,獲取用戶列表 Bulkhead 'backendA' permitted a call. count的值 = 1 Bulkhead 'backendA' has finished a call. 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=0, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' has finished a call. 執(zhí)行結(jié)束: , metrics[ availableConcurrentCalls=1, maxAllowedConcurrentCalls=1 ] Bulkhead 'backendA' permitted a call.

前面的線程沒有馬上被拒絕,而是等待了一段時(shí)間再執(zhí)行。

RateLimiter

簡介

高頻控制是可以限制服務(wù)調(diào)用頻率,Resilience4jRateLimiter可以對頻率進(jìn)行納秒級別的控制,在每一個(gè)周期刷新可以調(diào)用的次數(shù),還可以設(shè)定線程等待權(quán)限的時(shí)間。

可配置參數(shù)

配置參數(shù)默認(rèn)值描述
timeoutDuration5[s]線程等待權(quán)限的默認(rèn)等待時(shí)間
limitRefreshPeriod500[ns]權(quán)限刷新的時(shí)間,每個(gè)周期結(jié)束后,RateLimiter將會把權(quán)限計(jì)數(shù)設(shè)置為limitForPeriod的值
limiteForPeriod50一個(gè)限制刷新期間的可用權(quán)限數(shù)

測試前準(zhǔn)備

pom.xml

不需要引入新的依賴,已經(jīng)集成在resilience4j-spring-boot中了

application.yml配置

?

resilience4j:ratelimiter:configs:default:limitForPeriod: 5limitRefreshPeriod: 1stimeoutDuration: 5sinstances:backendA:baseConfig: defaultlimitForPeriod: 1backendB:baseConfig: defaulttimeoutDuration: 0s

用于監(jiān)控RateLimiter狀態(tài)及事件的工具類

同樣為了監(jiān)控RateLimiter組件,寫一個(gè)工具類:

?

@Log4j2 public class RateLimiterUtil {/*** @Description: 獲取rateLimiter的狀態(tài)*/public static void getRateLimiterStatus(String time, RateLimiter rateLimiter){RateLimiter.Metrics metrics = rateLimiter.getMetrics();// Returns the number of availablePermissions in this duration.int availablePermissions = metrics.getAvailablePermissions();// Returns the number of WaitingThreadsint numberOfWaitingThreads = metrics.getNumberOfWaitingThreads();log.info(time + ", metrics[ availablePermissions=" + availablePermissions +", numberOfWaitingThreads=" + numberOfWaitingThreads + " ]");}/*** @Description: 監(jiān)聽rateLimiter事件*/public static void addRateLimiterListener(RateLimiter rateLimiter){rateLimiter.getEventPublisher().onSuccess(event -> log.info(event.toString())).onFailure(event -> log.info(event.toString()));} }

調(diào)用方法

還是以之前查詢用戶列表的服務(wù)為例。RateLimiter支持AOP和程序式兩種方式的調(diào)用。

程序式的調(diào)用方法

調(diào)用方法都類似,裝飾方法之后用Try.of().recover()來執(zhí)行:

?

public class RateLimiterServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate RateLimiterRegistry rateLimiterRegistry;public List<User> ratelimiterNotAOP(){// 通過注冊器獲得RateLimiter實(shí)例RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("backendA");RateLimiterUtil.getRateLimiterStatus("開始執(zhí)行前: ", rateLimiter);// 通過Try.of().recover()調(diào)用裝飾后的服務(wù)Try<List<User>> result = Try.of(Bulkhead.decorateCheckedSupplier(rateLimiter, remoteServiceConnector::process)).recover(BulkheadFullException.class, throwable -> {log.info("服務(wù)失敗: " + throwable.getLocalizedMessage());return new ArrayList();});RateLimiterUtil.getRateLimiterStatus("執(zhí)行結(jié)束: ", rateLimiter);return result.get();} }

AOP式的調(diào)用方法

首先在連接器方法上使用@RateLimiter(name="", fallbackMethod="")注解,其中name是要使用的RateLimiter實(shí)例的名稱,fallbackMethod是要使用的降級方法:

?

public RemoteServiceConnector{@RateLimiter(name = "backendA", fallbackMethod = "fallback")public List<User> process() throws TimeoutException, InterruptedException {List<User> users;users = remoteServic.process();return users;}private List<User> fallback(BulkheadFullException e){log.info("服務(wù)失敗: " + e.getLocalizedMessage());return new ArrayList();} }

如果RetryCircuitBreakerBulkheadRateLimiter同時(shí)注解在方法上,默認(rèn)的順序是Retry>CircuitBreaker>RateLimiter>Bulkhead,即先控制并發(fā)再限流然后熔斷最后重試

接下來直接調(diào)用方法:

?

public class RateLimiterServiceImpl {@Autowiredprivate RemoteServiceConnector remoteServiceConnector;@Autowiredprivate RateLimiterRegistry rateLimiterRegistry;public List<User> rateLimiterAOP() throws TimeoutException, InterruptedException {List<User> result = remoteServiceConnector.process();BulkheadUtil.getBulkheadStatus("執(zhí)行結(jié)束:", rateLimiterRegistry.retry("backendA"));return result;} }

使用測試

application.yml文件中將backenA設(shè)定為20s只能處理1個(gè)請求,為便于觀察,刷新時(shí)間設(shè)定為20s,等待時(shí)間設(shè)定為5s

?

configs:default:limitForPeriod: 5limitRefreshPeriod: 20stimeoutDuration: 5sinstances:backendA:baseConfig: defaultlimitForPeriod: 1

使用另一個(gè)遠(yuǎn)程服務(wù)接口的實(shí)現(xiàn),不拋出異常,當(dāng)做正常服務(wù)進(jìn)行,為了讓結(jié)果明顯一些,讓方法sleep 5秒:

?

public class RemoteServiceImpl implements RemoteService {private static AtomicInteger count = new AtomicInteger(0);public List<User> process() throws InterruptedException {int num = count.getAndIncrement();log.info("count的值 = " + num);Thread.sleep(5000);log.info("服務(wù)正常運(yùn)行,獲取用戶列表");// 模擬數(shù)據(jù)庫正常查詢r(jià)eturn repository.findAll();} }

用線程池調(diào)5個(gè)線程去請求服務(wù):

?

public class RateLimiterServiceImplTest{@Autowiredprivate RateLimiterServiceImpl rateLimiterService;@Autowiredprivate RateLimiterRegistry rateLimiterRegistry;@Testpublic void rateLimiterTest() {RateLimiterUtil.addRateLimiterListener(rateLimiterRegistry.rateLimiter("backendA"));ExecutorService pool = Executors.newCachedThreadPool();for (int i=0; i<5; i++){pool.submit(() -> {// rateLimiterService.rateLimiterAOP();rateLimiterService.rateLimiterNotAOP();});}pool.shutdown();while (!pool.isTerminated());}} }

看一下測試結(jié)果:

?

開始執(zhí)行前: , metrics[ availablePermissions=1, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=1, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=1, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=1, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=0, numberOfWaitingThreads=0 ] RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T17:06:15.735+08:00[Asia/Shanghai]} count的值 = 0 RateLimiterEvent{type=FAILED_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T17:06:20.737+08:00[Asia/Shanghai]} RateLimiterEvent{type=FAILED_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T17:06:20.739+08:00[Asia/Shanghai]} RateLimiterEvent{type=FAILED_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T17:06:20.740+08:00[Asia/Shanghai]} 服務(wù)失敗: RateLimiter 'backendA' does not permit further calls 服務(wù)失敗: RateLimiter 'backendA' does not permit further calls 執(zhí)行結(jié)束: , metrics[ availablePermissions=0, numberOfWaitingThreads=1 ] 執(zhí)行結(jié)束: , metrics[ availablePermissions=0, numberOfWaitingThreads=1 ] RateLimiterEvent{type=FAILED_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T17:06:20.745+08:00[Asia/Shanghai]} 服務(wù)正常運(yùn)行,獲取用戶列表 服務(wù)失敗: RateLimiter 'backendA' does not permit further calls 執(zhí)行結(jié)束: , metrics[ availablePermissions=0, numberOfWaitingThreads=0 ] 服務(wù)失敗: RateLimiter 'backendA' does not permit further calls 執(zhí)行結(jié)束: , metrics[ availablePermissions=0, numberOfWaitingThreads=0 ] 執(zhí)行結(jié)束: , metrics[ availablePermissions=1, numberOfWaitingThreads=0 ]

只有一個(gè)服務(wù)調(diào)用成功,其他都執(zhí)行失敗了。現(xiàn)在我們把刷新時(shí)間調(diào)成1s

?

configs:default:limitForPeriod: 5limitRefreshPeriod: 1stimeoutDuration: 5sinstances:backendA:baseConfig: defaultlimitForPeriod: 1

重新執(zhí)行,結(jié)果如下:

?

開始執(zhí)行前: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 開始執(zhí)行前: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T18:25:18.894+08:00[Asia/Shanghai]}count的值 = 0 RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T18:25:18.894+08:00[Asia/Shanghai]} count的值 = 1 RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T18:25:19.706+08:00[Asia/Shanghai]} count的值 = 2 RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T18:25:19.706+08:00[Asia/Shanghai]} count的值 = 3 RateLimiterEvent{type=SUCCESSFUL_ACQUIRE, rateLimiterName='backendA', creationTime=2019-07-10T18:25:20.703+08:00[Asia/Shanghai]} count的值 = 4 服務(wù)正常運(yùn)行,獲取用戶列表 服務(wù)正常運(yùn)行,獲取用戶列表 服務(wù)正常運(yùn)行,獲取用戶列表 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 執(zhí)行結(jié)束: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 執(zhí)行結(jié)束: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ]執(zhí)行結(jié)束: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ] 服務(wù)正常運(yùn)行,獲取用戶列表 執(zhí)行結(jié)束: , metrics[ availablePermissions=2, numberOfWaitingThreads=0 ]

可以看出,幾個(gè)服務(wù)都被放入并正常執(zhí)行了,即使上個(gè)服務(wù)還沒完成,依然可以放入,只與時(shí)間有關(guān),而與線程無關(guān)。


?

總結(jié)

以上是生活随笔為你收集整理的Resilience4j-轻量级熔断框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

人人爽人人插 | 亚洲影音先锋 | 超碰免费观看 | 九九免费在线视频 | 国产一区二区高清视频 | 美女网站黄在线观看 | 久久人人爽爽人人爽人人片av | 成人午夜黄色 | 韩国av免费观看 | 精品国产aⅴ一区二区三区 在线直播av | 婷婷激情综合 | 国产一级二级在线播放 | 午夜精品一区二区三区免费 | 天天干天天做 | 午夜三级影院 | 亚洲国产福利视频 | 黄色小说免费在线观看 | 五月天六月色 | 奇米影视777四色米奇影院 | 国产 日韩 欧美 中文 在线播放 | 又黄又刺激又爽的视频 | 亚洲区另类春色综合小说校园片 | 丁香婷婷成人 | 一区二区在线影院 | www.国产在线 | 91av视频在线播放 | 亚洲jizzjizz日本少妇 | 五月天综合网站 | 依人成人综合网 | 久久在现视频 | 不卡中文字幕av | 午夜精品久久久久久久爽 | 亚洲三级黄色 | 久草在线一免费新视频 | 国产精品久久久一区二区 | 一区二区三区在线播放 | 97超碰人人澡人人 | 久久爱导航 | 500部大龄熟乱视频使用方法 | 色妞色视频一区二区三区四区 | 九九热免费精品视频 | 欧美激情片在线观看 | 欧美极品少妇xxxxⅹ欧美极品少妇xxxx亚洲精品 | 免费h精品视频在线播放 | 日本一区二区免费在线观看 | 中文字幕黄色网 | 亚洲黄色一级大片 | 青青久草在线 | a视频在线观看免费 | 啪啪av在线| 日韩视频一区二区三区在线播放免费观看 | 亚欧日韩成人h片 | 亚洲美女精品视频 | 美女福利视频 | 国产黄色一级片在线 | 成人免费视频视频在线观看 免费 | 成人av av在线| 一级电影免费在线观看 | 中文一区二区三区在线观看 | 国产黄色精品在线 | 亚洲精品视频在线播放 | 成人在线观看资源 | 热久久这里只有精品 | 99热官网| 久久久黄色| 国产精品乱码一区二三区 | 色丁香婷婷 | 人人澡超碰碰97碰碰碰软件 | 日韩精品一区二区免费视频 | 国产精品久久久久久久av大片 | 天天操天天怕 | 欧美a视频 | 精品一区电影 | 国产 日韩 在线 亚洲 字幕 中文 | 黄污网站在线 | 久久午夜电影网 | 亚洲美女精品区人人人人 | 美女久久久久久久 | 精品在线不卡 | 亚洲天天综合网 | 国产91学生粉嫩喷水 | av日韩中文 | 久久久国产精品免费 | 在线看不卡av | 国产夫妻自拍av | 国产精品va在线观看入 | 亚洲一区二区三区在线看 | 亚洲综合在线一区二区三区 | 高清av不卡 | 日韩av成人在线观看 | 国产综合精品一区二区三区 | 国产午夜亚洲精品 | 精品久久久久久亚洲综合网 | 天天天天射 | 99夜色| 亚洲国产精品va在线看 | 日韩国产精品久久 | 亚洲一区免费在线 | 毛片视频电影 | 在线综合 亚洲 欧美在线视频 | 欧美日韩亚洲在线观看 | 天天综合在线观看 | 中文字幕一区二区三区在线视频 | 欧美日韩亚洲在线观看 | 久久久久久免费毛片精品 | 婷婷六月天综合 | 丁香花在线观看视频在线 | 成人午夜电影在线 | 综合色中色 | 五月婷婷在线视频观看 | 97碰在线 | 国产精美视频 | 高清不卡一区二区三区 | 日本视频网 | 亚洲精品国产品国语在线 | 久草com | 亚洲成人精品久久 | 色小说av | 丁香激情网| 国产不卡在线视频 | se婷婷| 日韩三级视频在线看 | 午夜影院一级 | 九九九九九九精品任你躁 | 天天综合网久久 | 8x8x在线观看视频 | 中文字幕在线播放av | 99麻豆久久久国产精品免费 | 99精品区 | 中文理论片 | 日韩精品一区二区三区免费观看视频 | 97免费| 国产午夜三级一二三区 | 91夫妻视频 | 国产精品正在播放 | 在线a视频 | 精品夜夜嗨av一区二区三区 | 激情综合网五月激情 | 免费高清无人区完整版 | 在线观看亚洲电影 | 国产精品免费观看国产网曝瓜 | 亚洲无吗av| 97免费在线观看视频 | 久久精品99北条麻妃 | 操少妇视频| 日本资源中文字幕在线 | av网址aaa| 久久精品免费电影 | 91传媒在线播放 | 亚洲精品xxxx | 夜夜躁日日躁狠狠躁 | 欧美精品一区二区三区一线天视频 | 国产99久久久久 | 超碰九九 | 久久综合九色综合久久久精品综合 | 五月天色丁香 | 日韩3区| 久久夜色精品国产欧美乱极品 | 欧美日韩国产一区二区三区在线观看 | 中文字幕一区二区三 | 五月激情站 | 毛片的网址 | 国产裸体视频bbbbb | 国产伦精品一区二区三区免费 | 国产破处在线播放 | 人人人爽| 日本精品视频一区 | 久久露脸国产精品 | 热久久免费视频精品 | 久草在线播放视频 | 久久久国产精品麻豆 | 日韩网站视频 | 亚洲婷婷网 | 免费99精品国产自在在线 | 久久久免费精品视频 | 亚洲一区二区三区在线看 | 亚洲精品456在线播放第一页 | 中文字幕丝袜美腿 | 免费在线中文字幕 | 国产视频一区精品 | 免费看91的网站 | 亚洲国产免费看 | 最新国产福利 | 天天插天天 | 97超碰人人澡 | av在线等| 手机av网站 | 美女黄视频免费看 | 日韩欧美在线观看一区 | 久久99国产精品自在自在app | 手机成人在线 | 99久免费精品视频在线观看 | 日韩二区精品 | 国产高清中文字幕 | 婷婷在线资源 | 成在线播放 | 国产打女人屁股调教97 | 偷拍视频一区 | 激情av资源网 | 国产精品久久久99 | 成年人视频免费在线 | 国产精品嫩草影院123 | 国产精品二区在线观看 | 免费色视频网站 | 在线观看免费国产小视频 | 久久99在线视频 | 视频一区二区在线观看 | 亚洲天堂精品视频在线观看 | 激情婷婷综合网 | 亚洲高清视频在线观看 | 午夜在线免费观看视频 | 色吧久久 | 九九视频在线观看视频6 | 碰超在线97人人 | 免费网站在线观看人 | av一级久久 | 在线观看日韩精品 | 在线精品观看国产 | 国产精品久久久区三区天天噜 | 久久综合五月天婷婷伊人 | 日韩a在线播放 | 黄色com| 狠狠久久 | mm1313亚洲精品国产 | 婷婷色在线播放 | 欧美福利在线播放 | 国产精品九九久久久久久久 | 97国产精品一区二区 | 成人一级在线观看 | 国产123区在线观看 国产精品麻豆91 | 日韩高清一二区 | 日韩高清观看 | 国产精品一区二 | 超碰97人人干 | 美女免费黄网站 | 免费看的黄网站 | 最近中文字幕国语免费高清6 | 亚洲精品影院在线观看 | 亚洲午夜精品久久久久久久久 | 99精品免费视频 | 亚洲免费资源 | 国产中文字幕91 | 涩涩在线 | 国产艹b视频 | 麻豆精品视频在线观看免费 | 在线成人看片 | 久久在现| 一级黄色在线视频 | 午夜精品一区二区三区在线播放 | 91精品一区在线观看 | 久久午夜鲁丝片 | avwww在线观看 | 久久久久国产精品午夜一区 | 免费看片网站91 | 欧美 日韩 性 | 热re99久久精品国产99热 | 欧美一区三区四区 | 玖玖视频在线 | 亚洲精品中文字幕在线 | 999视频网站 | 午夜影院一级 | 久久久91精品国产 | 久久亚洲私人国产精品va | av 在线观看 | 四虎在线免费观看 | 久久久久久毛片 | 精品国产视频在线观看 | 欧美另类一二三四区 | .精品久久久麻豆国产精品 亚洲va欧美 | 天天干夜夜夜操天 | 91香蕉视频好色先生 | 中文字幕在线播放视频 | 日韩大片在线免费观看 | 在线免费高清一区二区三区 | 狠狠色丁香婷婷综合欧美 | 99久久超碰中文字幕伊人 | 麻豆94tv免费版 | 综合色亚洲 | 久久成人国产精品免费软件 | 久艹视频在线观看 | 日韩色在线 | 综合婷婷丁香 | 日日夜夜天天 | 国产黄色一级片在线 | 国产一级大片在线观看 | 日韩午夜在线观看 | 国产福利av在线 | 国产精品免费观看在线 | 国产伦精品一区二区三区四区视频 | 91视频在线免费下载 | 欧美极度另类 | 天天爽天天爽天天爽 | 97在线精品视频 | 五月天激情视频在线观看 | 成人午夜黄色影院 | 欧美久久九九 | 欧美日韩一区二区免费在线观看 | 日韩在线观看视频网站 | 在线观看成人福利 | 亚洲国产精品电影在线观看 | 亚洲高清视频在线 | 午夜精品一区二区三区视频免费看 | 欧美日韩免费一区二区 | 国产亚洲成av人片在线观看桃 | 午夜少妇av | 婷婷丁香社区 | 中文字幕久久精品 | 亚洲精品免费视频 | 日韩激情小视频 | 欧美性生活小视频 | 日韩国产欧美视频 | 911国产精品 | 日本中文字幕在线观看 | 婷婷色在线资源 | 黄a在线观看| 国产成人99久久亚洲综合精品 | 一区二区三区高清在线观看 | 日韩在线观看网站 | 999毛片| 国产精品高潮呻吟久久av无 | 蜜桃视频日本 | 久久综合五月天婷婷伊人 | 国产91九色视频 | 亚洲成人av在线 | 中文字幕在线观看国产 | 亚洲第一区在线观看 | 人人狠狠 | 91在线观看视频 | 亚洲美女精品区人人人人 | 最近中文字幕高清字幕在线视频 | 国产午夜三级 | 91亚洲国产成人 | 亚洲国产一二三 | 日韩免费观看视频 | 国产精品久久久久久久久久久久冷 | 999国产| 国产麻豆剧果冻传媒视频播放量 | 91精品欧美| 97视频亚洲 | 久草资源在线 | av电影 一区二区 | 超碰在线公开 | 国产精品原创视频 | 久久这里只有精品9 | 五月在线 | 欧美视屏一区二区 | www.com在线观看 | 在线观看mv的中文字幕网站 | 国产精品国产三级国产aⅴ入口 | 日韩精品播放 | 日韩在线观看视频一区二区三区 | 日韩欧美精品在线观看视频 | 网站在线观看你们懂的 | 成人亚洲精品国产www | 91传媒免费在线观看 | 午夜免费久久看 | 狠狠色丁香婷婷综合橹88 | 国产精品综合在线观看 | 国产在线观看xxx | 蜜臀av在线一区二区三区 | 亚洲免费成人 | 丁香久久婷婷 | 国产一区二区三区 在线 | 免费看黄的视频 | 一区二区视频在线免费观看 | 香蕉影视 | 国产精品美女久久久免费 | 欧美日本中文字幕 | 久在线| 久草在线观看视频免费 | 欧美狠狠色 | 久久96| 麻豆一二三精选视频 | 日本黄色大片免费 | 久久婷婷影视 | 免费人做人爱www的视 | 国产一区二区成人 | 久久久性 | 国产黄a三级三级 | 国产精品久久一区二区无卡 | 久久久久久久影视 | 99久久综合国产精品二区 | 国内精品亚洲 | 亚洲欧美激情插 | 国产一区黄色 | 黄色一级免费 | 亚洲精品资源在线 | 日韩欧美91 | 日韩中文字幕免费在线观看 | 久草精品视频 | 免费成人黄色av | www.久久免费 | 91理论片午午伦夜理片久久 | 99中文视频在线 | 最新av在线播放 | 欧美成人在线免费 | 99在线热播精品免费 | 欧美精品久久久久久久久久丰满 | 久久国内精品视频 | 欧美午夜性生活 | 91精品国产福利 | 久久毛片高清国产 | 国产+日韩欧美 | 在线精品在线 | 国产涩涩在线观看 | 一区二区激情视频 | 成年人免费看的视频 | 欧洲视频一区 | 13日本xxxxxⅹxxx20 | 国产精品美女久久久久久2018 | 久草视频免费在线观看 | 亚州视频在线 | 91av中文字幕 | 久久理论电影 | 国产免费一区二区三区最新6 | 国产在线不卡一区 | 亚洲手机av | 日韩电影一区二区在线 | 国产一二三区av | 国产精品破处视频 | 噜噜色官网 | 国产一区二区三区午夜 | 亚洲无吗天堂 | 九九免费观看全部免费视频 | 黄色视屏在线免费观看 | 亚洲国产人午在线一二区 | 国产清纯在线 | 欧美成人按摩 | 国产精品一区二区三区免费视频 | 色婷婷六月天 | 91成人精品一区在线播放69 | 婷香五月 | 激情在线网址 | 99热9 | 天操夜夜操 | 亚洲精品久久久久久久蜜桃 | 国产视频久久久久 | 国产精品久久久亚洲 | 亚洲日本va午夜在线影院 | 中文字幕精品www乱入免费视频 | 久久中文字幕导航 | 久久不射电影网 | 激情五月婷婷丁香 | 欧美日本日韩aⅴ在线视频 插插插色综合 | 久久婷婷网 | 日韩精品久久久 | 久久人91精品久久久久久不卡 | 日韩欧美在线影院 | 人人澡人人干 | 成人一区二区在线观看 | 江苏妇搡bbbb搡bbbb | 国产欧美三级 | 欧美日韩高清国产 | 在线免费av播放 | 精品在线视频一区 | 免费在线观看午夜视频 | 特级毛片爽www免费版 | 国产一级淫片在线观看 | 国产99精品 | 久草在线免费电影 | 国产精品成人一区二区三区 | 久久久国产影视 | 久久97久久97精品免视看 | 丝袜护士aⅴ在线白丝护士 天天综合精品 | 91日韩精品 | 麻豆视频在线免费观看 | 丰满少妇一级片 | 中文字幕精品一区久久久久 | 激情 婷婷| 91成人精品国产刺激国语对白 | 色噜噜狠狠色综合中国 | 黄色一级在线视频 | 成 人 黄 色 视频免费播放 | 99视频精品| 亚洲综合在线发布 | 国产一区二区在线观看视频 | 夜色成人av | 人人爽人人做 | 99热 精品在线 | 欧美日韩中文视频 | mm1313亚洲精品国产 | 国产专区欧美专区 | 天天躁天天狠天天透 | 久久精品99国产精品亚洲最刺激 | 97超碰人人在线 | 国产伦理久久精品久久久久_ | 久久久免费 | 午夜久久 | av国产在线观看 | 成人福利在线 | 在线天堂中文在线资源网 | 国产精品6999成人免费视频 | 丁香花中文在线免费观看 | 亚洲综合在线发布 | 欧美精品首页 | 久久久久免费观看 | 五月天久久综合网 | 欧美少妇xxxxxx | 久久久精品国产免费观看同学 | 国产精品美女久久久久久 | 一色av| 中文字幕av播放 | 日本一区二区三区免费观看 | 国产精品久久久久久久久毛片 | av看片在线 | 欧美色道 | 免费黄a大片 | 一区二区中文字幕在线观看 | 三级性生活视频 | 国产 一区二区三区 在线 | 97精品国自产拍在线观看 | 国产精品久久久久久久7电影 | 日韩av电影免费在线观看 | 欧美福利视频一区 | av久久在线| 手机av电影在线 | 色综合久久88色综合天天人守婷 | 碰碰影院| 天天干夜夜干 | 国产免费人人看 | 国产又粗又猛又色 | 伊人婷婷网 | 午夜电影一区 | 91超级碰碰 | 色综合久久网 | 国产精品视频在线观看 | 国产成人精品一区二三区 | 久久国产美女 | 91福利区一区二区三区 | 日韩在线首页 | www.91成人| 黄a在线观看 | 九九九九九国产 | 国产v在线观看 | 天天干,天天草 | 久久久久久久久免费 | 亚洲精品视 | 久99久在线 | 日韩免费播放 | 久久久久久久久久毛片 | 欧美少妇xxx | 97在线视频免费看 | 亚洲免费国产 | 欧美一级免费 | 国产精品久久久久影院 | 亚洲精品大全 | 久久免费试看 | 日韩三级一区 | 久久久香蕉视频 | 亚洲国产一区二区精品专区 | 国产精品久久久久久久免费观看 | 92精品国产成人观看免费 | 免费看黄的视频 | 精品久久在线 | 欧美天堂视频在线 | 亚洲欧洲成人 | jizz欧美性9 国产一区高清在线观看 | 一本到在线 | 免费欧美精品 | 黄色大片日本免费大片 | 日韩久久久久久久 | 岛国精品一区二区 | 精品国产一区二区三区四区vr | 国产涩涩在线观看 | a级国产乱理伦片在线播放 久久久久国产精品一区 | av在线短片| 中文字幕在线播放av | av福利在线看 | 午夜三级毛片 | 国产男女爽爽爽免费视频 | 午夜视频在线观看一区二区三区 | 国产精品99久久久精品 | 日韩欧美国产激情在线播放 | 91黄色小视频 | 色婷婷av国产精品 | 久久99精品国产99久久6尤 | 欧美精品免费在线 | 999久久久免费精品国产 | 国产亚洲小视频 | 国产丝袜| 一级淫片在线观看 | 国产小视频免费在线观看 | 久久免费在线观看 | 天天色天天干天天色 | 色综合天天色综合 | 免费人成在线观看网站 | 97天天干 | 日韩中文字幕亚洲一区二区va在线 | 欧美大片在线观看一区 | 色综合激情网 | 国产精品中文字幕在线观看 | 久久视 | 91麻豆看国产在线紧急地址 | 五月婷婷激情网 | 国产亚洲在线观看 | 亚洲精品高清在线 | 国产午夜精品一区二区三区嫩草 | 欧美午夜精品久久久久久孕妇 | 美女中文字幕 | 在线观看久 | 丁香五月缴情综合网 | 国产视频在线播放 | 国产99免费视频 | 欧美国产亚洲精品久久久8v | 欧美日韩亚洲第一 | 日日干夜夜草 | 国产一区在线播放 | 狠狠色丁香久久婷婷综合丁香 | 天天se天天cao天天干 | 高潮毛片无遮挡高清免费 | 国产视频一| 色婷婷精品大在线视频 | 久草在线高清视频 | 欧美黄污视频 | 91香蕉视频污在线 | 精品国产一区二区三区久久久蜜臀 | 91av蜜桃 | 日产乱码一二三区别免费 | 国产91精品一区二区麻豆亚洲 | 黄色毛片视频免费观看中文 | 亚洲天天摸日日摸天天欢 | 日韩网站在线看片你懂的 | 国产亚洲欧美一区 | 国产精品激情在线观看 | 一级α片免费看 | 亚洲欧美成人在线 | 日韩在线播放av | 日本精品久久久一区二区三区 | 一级全黄毛片 | 国产精品久久久久久久毛片 | 欧美极品少妇xxxx | 久久激情视频免费观看 | 中文字幕在线观看视频一区二区三区 | 人人射人人 | 久久综合狠狠综合 | 成人午夜片av在线看 | 97在线观看免费高清完整版在线观看 | 在线免费观看的av网站 | 超碰人人在 | 亚洲婷婷网 | 二区中文字幕 | 黄色免费在线看 | 国产免费作爱视频 | 亚洲国产精品成人综合 | 色午夜影院 | 成人国产亚洲 | 99中文字幕在线观看 | 91在线影视| 欧美在线视频第一页 | 亚洲欧美视频一区二区三区 | 一本一本久久a久久 | 亚洲不卡在线 | 亚洲精品成人在线 | 久久理论电影网 | 亚洲精品乱码久久久久久蜜桃动漫 | 色狠狠综合 | 黄色一级动作片 | 精品播放 | 黄色av网站在线观看 | 久久综合五月婷婷 | 九九久久免费 | 日韩av成人在线观看 | a天堂在线看 | 色欲综合视频天天天 | 国产一区二区不卡视频 | 天天做天天干 | bbb搡bbb爽爽爽 | 日本精品视频在线观看 | 天天天色| 91视频免费 | 精品国产伦一区二区三区观看方式 | 97国产在线 | 欧美日韩有码 | av色影院 | 欧美激情精品一区 | 一区二区三区在线免费观看视频 | 国产午夜一区 | 国产精品一级在线 | 国产在线97 | 女人18精品一区二区三区 | 九色最新网址 | 亚洲成a人片在线观看中文 中文字幕在线视频第一页 狠狠色丁香婷婷综合 | av网站免费线看精品 | 8x成人免费视频 | 五月天亚洲激情 | 91爱爱免费观看 | 久草久视频 | 91在线国产观看 | 99产精品成人啪免费网站 | 成年人天堂com | 国内视频 | 国产精品视频地址 | 在线免费黄色毛片 | 蜜臀久久99静品久久久久久 | 91视频在线网址 | 亚洲成a人片在线观看中文 中文字幕在线视频第一页 狠狠色丁香婷婷综合 | 在线 精品 国产 | 免费看片网址 | 精品久久久久免费极品大片 | 国产精品午夜在线观看 | 91干干干 | 久久人人艹 | 天天看天天干 | 国产69精品久久久久99 | 97av影院 | 日女人免费视频 | 91视频啪 | 中文字幕资源网 国产 | 岛国大片免费视频 | www.夜夜草 | 国产一区二区三区视频在线 | 日韩欧美黄色网址 | 免费网站黄 | 国产精品自产拍 | av电影一区二区 | 99久久99久久精品国产片果冰 | 顶级bbw搡bbbb搡bbbb | 国产成人精品免费在线观看 | 欧美色综合 | 国产视频69| 久久精品直播 | avove黑丝| 国产视频91在线 | 黄色av成人在线观看 | 亚洲日本在线一区 | 911国产 | 97在线观看免费高清 | 丁香高清视频在线看看 | 亚洲精品欧洲精品 | 欧美精品小视频 | 国产日韩在线视频 | 国产第一二区 | 色综久久| 亚洲电影第一页av | 亚洲第五色综合网 | 免费在线观看av的网站 | 国产字幕在线观看 | 中文字幕在线精品 | 91精品导航 | 免费色黄 | 免费久久网 | 91影视成人 | 久草在线观看视频免费 | 91黄色在线看 | 欧美激情综合五月色丁香小说 | 国产一级免费观看视频 | 国产精品一区二区三区在线 | 黄色小说网站在线 | 九九九九热精品免费视频点播观看 | 狠狠干激情| 欧美另类高清 videos | 久久99久久99久久 | 久久国产精品一国产精品 | 国产黄色一级片在线 | 丁香五香天综合情 | 91福利试看| 国产一性一爱一乱一交 | 中日韩免费视频 | 国产美女无遮挡永久免费 | 久久全国免费视频 | 日韩国产欧美在线播放 | 成人精品电影 | 色的网站在线观看 | 亚洲欧美国产精品 | 色狠狠综合 | 国产专区第一页 | 探花视频免费在线观看 | 亚洲成a人片77777潘金莲 | 99婷婷| 国产精品午夜在线观看 | 久久97久久97精品免视看 | 激情综合网色播五月 | 天天综合中文 | 制服丝袜一区二区 | 天天操天天操天天操天天操天天操 | 国产在线视频一区二区 | 免费看久久久 | 久草爱视频 | 免费在线中文字幕 | 日本久久综合网 | 在线免费黄网站 | 久久久综合九色合综国产精品 | 久久黄色成人 | 久久久久久久久久久高潮一区二区 | 精品国产一区二区三区免费 | 亚洲婷婷在线 | 色婷婷综合久色 | 一区二区三区四区不卡 | 久久久鲁 | 国产午夜精品福利视频 | 五月在线| 久久久久免费精品国产 | 国产淫片免费看 | 久久久精品国产一区二区电影四季 | 美女福利视频 | 国产99爱| 久久艹国产视频 | 久久国产精品免费 | 黄色av一区二区三区 | 在线观看中文 | 天天狠狠 | 午夜精品福利一区二区三区蜜桃 | 深爱激情开心 | 久在线观看视频 | 激情网在线观看 | 日本黄色免费在线观看 | 亚洲精品456在线播放乱码 | 国产国语在线 | 日本不卡久久 | 亚洲无毛专区 | 免费的黄色的网站 | 国产精品成人在线 | 97超碰超碰 | 奇米影视999 | 99综合电影在线视频 | 天天插天天色 | 日日夜夜狠狠 | av大全在线 | 久久激五月天综合精品 | 久久福利 | 伊人伊成久久人综合网小说 | 久久精品视 | 日韩免费在线视频 | 97超在线 | 国产一区二区三区四区在线 | 欧美日韩免费一区二区 | 国产精品一区二区三区在线看 | 日日久视频 | 色瓜| 久久精品国产精品亚洲 | 天天se天天cao天天干 | 国产欧美久久久精品影院 | 国产精品久久嫩一区二区免费 | 最近中文字幕在线播放 | 在线日韩精品视频 | 亚洲精品中文字幕视频 | 中文字幕av电影下载 | 黄色大全在线观看 | 91丨九色丨丝袜 | 日韩videos高潮hd | 91你懂的| 黄色免费大片 | 最近中文国产在线视频 | 国产成人精品一区二区在线观看 | 中文字幕视频免费观看 | 精品视频久久久 | 国产不卡视频在线播放 | 天天操天天摸天天爽 | 免费成人av在线看 | 欧美一级在线 | 日韩在线中文字幕视频 | 国产在线欧美 | 99精品免费久久久久久久久 | 色吊丝在线永久观看最新版本 | 日韩av一区二区三区 | 在线观看激情av | 国产一卡在线 | 欧美成人区 | 精品一区二区三区四区在线 | 在线免费观看黄色小说 | 91精品麻豆 | 久久日本视频 | www亚洲一区 | 久久怡红院 | 午夜精品久久久久久久久久久 | 亚洲最新合集 | 91视频国产高清 | 欧美日韩视频一区二区三区 | 国产原创91 | 91丨九色丨高潮丰满 | 国产黄色大片免费看 | 伊人在线视频 | 久久精品com| 久草在线免费电影 | 麻豆国产精品永久免费视频 | 久久综合婷婷国产二区高清 | 黄色字幕网 | 91精品一区在线观看 | 日韩欧美高清在线观看 | 亚洲麻豆精品 | 国产在线精品视频 | 在线视频你懂得 | 久久精品一区二区三区国产主播 | 亚洲精品一区二区三区新线路 | 97看片| 狠狠色丁香久久婷婷综 | 国产在线不卡 | 色综合久久久久综合体桃花网 | 韩日精品在线观看 | 四虎亚洲精品 | 99精品热视频只有精品10 | 久久精品激情 | 久久综合九色综合欧美就去吻 | 中文字幕在线免费看线人 | 免费在线黄网 | 综合精品久久 | 91av视频在线观看 | 免费中午字幕无吗 | 在线一区av | 亚洲男人天堂a | 三级黄在线 | 欧美一级免费高清 | 日韩免费不卡视频 | 色综合天天爱 | 日韩精品在线一区 | 婷婷四房综合激情五月 | 精品国产一区二区三区不卡 | 国产在线理论片 | 丁香视频 | 天天插狠狠插 | 久久麻豆视频 | 国产午夜免费视频 | 免费又黄又爽视频 | av激情五月| 欧美日韩免费在线视频 | 国产自制av | 天天草天天草 | 黄色影院在线观看 | 91色欧美| 狠狠干五月天 | 91在线观看欧美日韩 | a精品视频| 亚洲天堂网站视频 | 国产精品久久 | 亚洲视频456 | 91桃花视频 | 国产一区网址 | 五月婷婷激情六月 | av网站免费线看精品 | 久久五月网 | 中文字幕一区二区三区久久 | 国产视频精品久久 | 久久99国产精品免费 | 精品亚洲国产视频 | 日韩一区二区三区高清免费看看 | 97精品国产91久久久久久久 | 9在线观看免费 | 中文字幕在线视频网站 | 激情av综合| 激情视频一区 | 在线a视频 | 日韩二区精品 | 99视频+国产日韩欧美 | 91成人在线视频 | av在线最新 | 亚洲精品小区久久久久久 | 免费国产一区二区 | 91av在线国产 | 在线视频一二三 | 狠狠色丁香婷婷综合橹88 | 五月综合在线观看 | 91精品国自产在线观看欧美 | 国产伦精品一区二区三区… | 午夜 久久 tv | 中文av网 | www.人人草 | 免费裸体视频网 | 亚洲小视频在线观看 | 96精品在线| 国产精品久久久一区二区三区网站 | 国产精品剧情 | 免费看v片 | 久久久久免费精品国产小说色大师 | 久久亚洲私人国产精品va | 午夜精品视频福利 | 韩国中文三级 | 久久成人高清视频 | 婷婷激情网站 | 日韩首页 | 天天干,天天插 | 久久99久久99精品免视看婷婷 | 久久99精品久久久久久 | 亚洲乱码中文字幕综合 | 免费看av片网站 | 国产精品女同一区二区三区久久夜 | 香蕉网在线观看 | 日本精品中文字幕在线观看 | www亚洲国产 | 国产五月色婷婷六月丁香视频 | 中文在线免费视频 | 91人网站| 视频在线日韩 | 久 久久影院 | 久久av一区二区三区亚洲 | 国产综合精品久久 | 免费试看一区 | 欧美精品九九 | 欧美性一级观看 | 一级黄色片在线观看 | 中文字幕美女免费在线 | 激情综合色播五月 | 亚洲精品一区二区在线观看 | 久久经典国产视频 | 视频91在线 | av电影在线免费 | 色综合网在线 | 精品国产乱码久久久久久天美 | 欧美日韩一二三四区 |