Resilience4j usage

Posted xiaobin-hlj80

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Resilience4j usage相关的知识,希望对你有一定的参考价值。

 

1. pom

1) normal

        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-circuitbreaker</artifactId>
            <version>0.13.2</version>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-retry</artifactId>
            <version>0.13.2</version>
        </dependency>

 

2) spring boot

 

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

 

 

 

3) spring boot2

 

        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>0.13.2</version>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-retry</artifactId>
            <version>0.13.2</version>
        </dependency>

 

 

 

 

2. create CB

1) CircuitBreakerRegistry

(1)default

CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();

// Get a CircuitBreaker from the CircuitBreakerRegistry with the global default configuration
CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("myCB2");

 

(2)custom

// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
    .failureRateThreshold(50)
    .waitDurationInOpenState(Duration.ofMillis(1000))
    .ringBufferSizeInHalfOpenState(2)
    .ringBufferSizeInClosedState(2)
    .build();

// Create a CircuitBreakerRegistry with a custom global configuration
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);


// Get a CircuitBreaker from the CircuitBreakerRegistry with a custom configuration
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("myCB1", circuitBreakerConfig);

 

2) directly

(1) default

CircuitBreaker defaultCircuitBreaker = CircuitBreaker.ofDefaults("myCB2");

// Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms
Retry retry = Retry.ofDefaults("myCB2");

 

(2) custom

// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
    .failureRateThreshold(50)
    .waitDurationInOpenState(Duration.ofMillis(1000))
    .ringBufferSizeInHalfOpenState(2)
    .ringBufferSizeInClosedState(2)
    .build();

CircuitBreaker customCircuitBreaker = CircuitBreaker.of("myCB1", circuitBreakerConfig);

// Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms
Retry retry = Retry.ofDefaults("myCB1");

 

 

3. decorate

1) backend

// Simulates a Backend Service
public interface BackendService {
    String doSomething();
}

 

2) Attached

// 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);

 

3. Exec

1) vavr

// Execute the decorated supplier and recover from any exception
String result = Try.ofSupplier(decoratedSupplier)
    .recover(throwable -> "Hello from Recovery").get();

System.out.println(result);

 

2) lambda expression

// When you don‘t want to decorate your lambda expression,
// but just execute it and protect the call by a CircuitBreaker.
String result = circuitBreaker.executeSupplier(backendService::doSomething);

System.out.println(result);

 

 

Reference:

    1. Achieving Fault Tolerance With Resilience4j

    2. resilience4j - circuit breaker

 

以上是关于Resilience4j usage的主要内容,如果未能解决你的问题,请参考以下文章

Resilience4j 源码解析:简介及调试环境搭建

[linux][c/c++]代码片段01

使用 Resilience4j 框架实现重试机制

你好Resilience4j一:Resilience4j之初体验

你好Resilience4j一:Resilience4j之初体验

服务治理-Resilience4j(熔断)