韧性,
在编译时生成。
在 Java 接口上标注重试、熔断器、超时等注解。编译时,注解处理器会生成实现这些策略的普通 Java 代码。没有代理,没有反射,不依赖框架。
- Java 21+
- 无需 Spring、CDI 或 AspectJ
- 同步、
CompletionStage、Mono和Flux - Apache 2.0
dependencies { implementation 'dev.resilience4cata:resilience4cata-all:1.1.0' annotationProcessor 'dev.resilience4cata:resilience4cata-processor:1.1.0' }
dependencies { implementation("dev.resilience4cata:resilience4cata-all:1.1.0") annotationProcessor("dev.resilience4cata:resilience4cata-processor:1.1.0") }
<dependency> <groupId>dev.resilience4cata</groupId> <artifactId>resilience4cata-all</artifactId> <version>1.1.0</version> </dependency> <!-- in the maven-compiler-plugin <configuration> --> <annotationProcessorPaths> <path> <groupId>dev.resilience4cata</groupId> <artifactId>resilience4cata-processor</artifactId> <version>1.1.0</version> </path> </annotationProcessorPaths>
Maven Central 上的 1.1.0 版本,自 1.0.0 起为稳定版本:在 1.x 内保持兼容。resilience4cata-all 包含 core、注解和配置;处理器只放在注解处理器路径上。安装指南
PaymentClient.javapublic interface PaymentClient { @Retry(maxAttempts = 3, delay = "200ms") @CircuitBreaker @Timeout("2s") Receipt charge(Order order); } // once, at startup ResilienceRegistry registry = ResilienceRegistry.builder().build(); PaymentClient client = Resilient.of( PaymentClient.class, new HttpPaymentClient(), registry); client.charge(order); // retried, guarded, timed out
PaymentClient$Resilient.java// Generated by the resilience4cata annotation processor. Do not edit. public final class PaymentClient$Resilient implements PaymentClient { private final PaymentClient delegate; private final RetryPolicy retry0; private final CircuitBreakerPolicy circuitBreaker0; private final TimeoutPolicy timeout0; // … constructor: each policy resolved once from the registry @Override public Receipt charge(Order p0) { final RetryPolicy r4c$retry = this.retry0; final long r4c$start = r4c$retry.start(); int r4c$attempts = 0; while (true) { try { return this.r4c$circuitBreaker0(p0); } catch (Throwable r4c$e) { final long r4c$delay = r4c$retry.onFailure(++r4c$attempts, r4c$e, r4c$start); if (r4c$delay < 0 || !r4c$retry.pause(r4c$delay)) { throw r4c$e; } } } } private Receipt r4c$circuitBreaker0(Order p0) { final CircuitBreakerPolicy r4c$cb = this.circuitBreaker0; final long r4c$token = r4c$cb.acquire(); if (r4c$token < 0) { throw r4c$cb.rejection(); } final long r4c$started = r4c$cb.startTimer(); try { final Receipt r4c$result = this.r4c$timeout0(p0); r4c$cb.onSuccess(r4c$token, r4c$started); return r4c$result; } catch (Throwable r4c$e) { r4c$cb.onFailure(r4c$token, r4c$started, r4c$e); throw r4c$e; } } private Receipt r4c$timeout0(Order p0) { final TimeoutPolicy r4c$timeout = this.timeout0; final TimeoutPolicy.Scope r4c$scope = r4c$timeout.enter(); final Receipt r4c$result; try { r4c$result = this.delegate.charge(p0); } catch (Throwable r4c$e) { r4c$timeout.failed(r4c$scope, r4c$e); throw r4c$e; } r4c$timeout.succeeded(r4c$scope); return r4c$result; } }
HttpPaymentClient.charge
有删节:实际文件使用全限定名。
保护逻辑是你能读懂的代码
Resilience4j 和 MicroProfile 在运行时用装饰器或代理包装你的调用。resilience4cata 在编译器中完成这项工作,所以运行的是普通 Java 代码。
用调试器单步进入
处理器把 PaymentClient$Resilient 生成在你自己的类旁边。每个策略都只是某个方法中的几行直线代码,你可以打开它、在其中设置断点,并在堆栈跟踪中读到它。
at HttpPaymentClient.charge at PaymentClient$Resilient.r4c$timeout0 at PaymentClient$Resilient.r4c$circuitBreaker0 at PaymentClient$Resilient.charge at CheckoutService.pay
同步调用成功时零内存分配
成功或被拒绝的同步调用分配 0 字节。每个 pull request 都会运行内存分配门禁的基准测试,一旦这一点改变,构建就会失败。异步调用会分配它们的 future,线程的第一次带超时调用会注册该线程一次:参见性能。
错误会让构建失败
错误的时长、签名不对的降级 (fallback) 方法、或标注在 final 方法上的注解,都是带有稳定错误码和修复建议的编译错误,而不是生产环境中的意外。
error: [R4C-ATR-001] @Retry.delay = "2 seconds"
is not a valid duration: … Use a number
followed by ns, us, ms, s, m, h or d …
@Retry(delay = "2 seconds")
^
每次调用的耗时,对数刻度;右侧比值为 Resilience4j 的平均值除以我们的平均值。越低越好。平均值仅在 ARM (Graviton3) 上测得。熔断器这几行是每次调用都成功的情形;一旦有调用失败,差距会缩小(四个线程、每 10 次调用失败 1 次时为 3.4×),参见性能。
以表格显示
| 场景 | 线程数 | resilience4cata | Resilience4j | 比值 |
|---|---|---|---|---|
| 重试,第一次尝试即成功 | 1 | 1.4 ns, 0 B | 22.9 ns, 0 B | 16× |
| 重试,第一次尝试即成功 | 4 | 1.3 ns, 0 B | 23.0 ns, 0 B | 18× |
| 熔断器,每次调用都成功 | 1 | 1.9 ns, 0 B | 89.9 ns, 0 B | 47× |
| 熔断器,每次调用都成功 | 4 | 1.9 ns, 0 B | 849.5 ns, 2 B | 447× |
| 同步超时,调用按时完成 | 1 | 23.6 ns, 0 B | 8 907.6 ns, 183 B | 377× |
| 同步超时,调用按时完成 | 4 | 23.6 ns, 0 B | 9 976.8 ns, 151 B | 423× |
| 有空闲槽位的舱壁隔离 | 1 | 17.3 ns, 0 B | 20.1 ns, 0 B | 1.2× |
| 有空闲槽位的舱壁隔离 | 4 | 192.3 ns, 0 B | 410.8 ns, 0 B | 2.1× |
八种策略,每种一个注解
可以标注在方法上,也可以标注在整个接口上。随意叠加:处理器会按一个精心选择的顺序嵌套它们,@PolicyOrder 可以改变这个顺序。
@Retry
使用指数退避和抖动再次尝试,可选总时间预算。
maxAttempts · delay · backoff · maxDuration@Hedge
当幂等的异步调用变慢时再发起一次相同调用,采用最先成功的结果。
delay · maxAttempts · hedgeOn · budgetPercent@CircuitBreaker
停止调用持续失败的依赖,之后放行少量探测调用。
failureRateThreshold · waitInOpen · halfOpenCalls@RateLimiter
每个周期只放行固定数量的调用,其余的被拒绝或等待。
permits · period · maxWait@AdaptiveLimit
用一个自行寻找合适值的上限限制并发调用:延迟稳定时上调,延迟升高时下调。
algorithm · minLimit · maxLimit@Timeout
在调用方自己的线程上中断超出时间预算的调用。
value@Bulkhead
限制同时运行的调用数量,避免一个缓慢的依赖占满所有线程。
maxConcurrent · maxWait@Fallback
在其他一切都失败时,通过同级方法或处理器类返回一个有用的结果。
value · method顺序,从最外层开始:Fallback、Retry 或 Hedge、CircuitBreaker、RateLimiter、AdaptiveLimit、Timeout、Bulkhead,然后是你的方法。它们如何组合
从添加依赖到第一次受保护的调用
添加处理器
core 在运行时使用,注解仅在编译时使用,处理器放在注解处理器路径上。
annotationProcessor 'dev.resilience4cata: resilience4cata-processor'
为接口添加注解
时长是
"200ms"这样的字符串,在编译时检查。@Retry(maxAttempts = 3) @Timeout("2s") Receipt charge(Order order);
包装你的实现
每个应用一个注册表 (registry),按名称共享熔断器和限流器的状态。
Resilient.of(PaymentClient.class, new HttpPaymentClient(), registry);
面向纯 Java 21+ 的注解驱动韧性库。Apache License 2.0。
dev.resilience4cata:resilience4cata-bom:1.1.0