本文主要研究下resilience4j的基本功能git
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-circuitbreaker</artifactId> <version>0.13.0</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-ratelimiter</artifactId> <version>0.13.0</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-retry</artifactId> <version>0.13.0</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-bulkhead</artifactId> <version>0.13.0</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-circularbuffer</artifactId> <version>0.13.0</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-timelimiter</artifactId> <version>0.13.0</version> </dependency>
@Test public void testCircuitBreaker(){ // Create a CircuitBreaker (use default configuration) CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig .custom() .enableAutomaticTransitionFromOpenToHalfOpen() .build(); CircuitBreaker circuitBreaker = CircuitBreaker .of("backendName",circuitBreakerConfig); String result = circuitBreaker.executeSupplier(() -> backendService.doSomethingWithArgs("world")); System.out.println(result); }
CircuitBreaker主要是實現針對接口異常的斷路統計以及斷路處理
@Test public void testTimelimiter(){ ExecutorService executorService = Executors.newSingleThreadExecutor(); TimeLimiterConfig config = TimeLimiterConfig.custom() .timeoutDuration(Duration.ofMillis(600)) .cancelRunningFuture(true) .build(); TimeLimiter timeLimiter = TimeLimiter.of(config); Supplier<Future<String>> futureSupplier = () -> { return executorService.submit(backendService::doSomethingThrowException); }; Callable<String> restrictedCall = TimeLimiter.decorateFutureSupplier(timeLimiter,futureSupplier); Try.of(restrictedCall::call) .onFailure(throwable -> System.out.println("We might have timed out or the circuit breaker has opened.")); }
主要是實現超時的控制
/** * A Bulkhead can be used to limit the amount of parallel executions */ @Test public void testBulkhead(){ Bulkhead bulkhead = Bulkhead.of("test", BulkheadConfig.custom() .maxConcurrentCalls(1) .build()); Supplier<String> decoratedSupplier = Bulkhead.decorateSupplier(bulkhead, backendService::doSomethingSlowly); IntStream.rangeClosed(1,2) .parallel() .forEach(i -> { String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get(); System.out.println(result); }); }
Bulkhead目前來看是用來控制並行(
parallel
)調用的次數
@Test public void testRateLimiter(){ // Create a custom RateLimiter configuration RateLimiterConfig config = RateLimiterConfig.custom() .timeoutDuration(Duration.ofMillis(100)) .limitRefreshPeriod(Duration.ofSeconds(1)) .limitForPeriod(1) .build(); // Create a RateLimiter RateLimiter rateLimiter = RateLimiter.of("backendName", config); // Decorate your call to BackendService.doSomething() Supplier<String> restrictedSupplier = RateLimiter .decorateSupplier(rateLimiter, backendService::doSomething); IntStream.rangeClosed(1,5) .parallel() .forEach(i -> { Try<String> aTry = Try.ofSupplier(restrictedSupplier); System.out.println(aTry.isSuccess()); }); }
用來作流控
@Test public void testFallback(){ // Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(() -> backendService.doSomethingThrowException()) .recover(throwable -> "Hello from Recovery").get(); System.out.println(result); } @Test public void testCircuitBreakerAndFallback(){ CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); Supplier<String> decoratedSupplier = CircuitBreaker .decorateSupplier(circuitBreaker, backendService::doSomethingThrowException); String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get(); System.out.println(result); }
fallback基本上是高可用操做的標配
@Test public void testRetry(){ CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms Retry retry = Retry.ofDefaults("backendName"); // Decorate your call to BackendService.doSomething() with a CircuitBreaker Supplier<String> decoratedSupplier = CircuitBreaker .decorateSupplier(circuitBreaker, backendService::doSomething); // Decorate your call with automatic retry decoratedSupplier = Retry .decorateSupplier(retry, decoratedSupplier); // Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get(); System.out.println(result); }
retry用來控制重試
resilience4j是一款受hystrix啓發的容錯組件,提供了以下幾款核心組件:github
這裏主要演示了關於circuitbreaker、ratelimiter、bulkhead、retry以及timelimiter。其特點就是使用裝飾者模式,能夠多個功能組合在一塊兒。其餘的話,timelimiter的使用比起hystrix稍微費勁些。async