Resilience4j 是一个轻量级、易于使用的容错库,其
灵感来自 Netflix Hystrix,但专为 Java 8 和函数式编程而设计。轻量级,因为该库仅使用 Vavr,而 Vavr 没有任何其他外部库依赖项。相比之下,Netflix Hystrix 具有对 Archaius 的编译依赖项,Archaius 具有更多的外部库依赖项,例如 Guava 和 Apache Commons Configuration。
Resilience4j 提供高阶函数(装饰器),以使用断路器、速率限制器、重试或隔板来增强任何功能接口、lambda 表达式或方法引用。您可以在任何功能接口、lambda 表达式或方法引用上堆叠多个装饰器。优点是您可以选择所需的装饰器,而不是其他任何选择。
使用 Resilience4j,您不必全力以赴,您可以选择您需要的。
抢先预览
以下示例演示如何使用 CircuitBreaker 和 Retry 修饰 lambda 表达式,以便在发生异常时最多重试调用 3 次。
您可以配置重试之间的等待间隔,还可以配置自定义回退算法。
该示例使用 Vavr 的 monad 从异常中恢复,并在所有重试都失败时调用另一个 lambda 表达式作为回退。Try
JAVA
// Create a CircuitBreaker with default configuration
CircuitBreaker circuitBreaker = CircuitBreaker
.ofDefaults("backendService");
// Create a Retry with default configuration
// 3 retry attempts and a fixed time interval between retries of 500ms
Retry retry = Retry
.ofDefaults("backendService");
// Create a Bulkhead with default configuration
Bulkhead bulkhead = Bulkhead
.ofDefaults("backendService");
Supplier<String> supplier = () -> backendService
.doSomething(param1, param2)
// Decorate your call to backendService.doSomething()
// with a Bulkhead, CircuitBreaker and Retry
// **note: you will need the resilience4j-all dependency for this
Supplier<String> decoratedSupplier = Decorators.ofSupplier(supplier)
.withCircuitBreaker(circuitBreaker)
.withBulkhead(bulkhead)
.withRetry(retry)
.decorate();
// Execute the decorated supplier and recover from any exception
String result = Try.ofSupplier(decoratedSupplier)
.recover(throwable -> "Hello from Recovery").get();
// 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);
// You can also run the supplier asynchronously in a ThreadPoolBulkhead
ThreadPoolBulkhead threadPoolBulkhead = ThreadPoolBulkhead
.ofDefaults("backendService");
// The Scheduler is needed to schedule a timeout
// on a non-blocking CompletableFuture
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
CompletableFuture<String> future = Decorators.ofSupplier(supplier)
.withThreadPoolBulkhead(threadPoolBulkhead)
.withTimeLimiter(timeLimiter, scheduledExecutorService)
.withCircuitBreaker(circuitBreaker)
.withFallback(asList(TimeoutException.class,
CallNotPermittedException.class,
BulkheadFullException.class),
throwable -> "Hello from Recovery")
.get().toCompletableFuture();
模块化
使用 Resilience4j,您不必全力以赴,您可以选择您需要的。
Resilience4j 提供了几个核心模块和附加模块:
所有核心模块和 Decorators 类
- 韧性4J-ALL
核心模块
- resilience4j-circuitbreaker: 熔断
- resilience4j-ratelimiter:速率限制
- resilience4j-bulkhead: 隔板
- resilience4j-retry:自动重试(同步和异步)
- resilience4j-cache:结果缓存
- resilience4j-timelimiter:超时处理
附加模块
- resilience4j-retrofit: 改装适配器
- resilience4j-feign: Feign 适配器
- resilience4j-consumer:循环缓冲区事件使用者
- resilience4j-kotlin:Kotlin 协程支持
框架模块
- resilience4j-spring-boot: Spring Boot 启动器
- resilience4j-spring-boot2: Spring Boot 2 启动器
- resilience4j-ratpack: Ratpack 入门
- resilience4j-vertx: Vertx Future 装饰器
反应式模块
- resilience4j-rxjava2:自定义 RxJava2 运算符
- resilience4j-reactor:自定义 Spring Reactor 运算符
Metrics 模块
- resilience4j-micrometer: 千分尺度量导出器
- resilience4j-metrics: Dropwizard 指标导出器
- resilience4j-prometheus: Prometheus Metrics 导出器