我正在尝试使用 Resilience4j 功能。
我的用例是组合 3 个模块:
- 断路器
- 重试
- 时间限制器
我想组合所有这些模块并执行该方法仅一次。
代码
这是我尝试过的。
Supplier<R> supplier = this::doSomething;
timeLimiter.executeFutureSupplier(() -> CompletableFuture.supplyAsync(supplier));
return Decorators.ofSupplier(supplier)
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.withBulkhead(bulkhead)
.decorate();
问题
我的 doSomething()
方法执行了两次,而不是预期的一次。
有没有人之前看到过这个问题?
最佳答案
您正在使用 timeLimiter.executeFutureSupplier
执行 Future
而不是装饰它。
请严格按照以下顺序使用:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
Supplier<R> supplier = this::doSomething;
CompletableFuture<R> future = Decorators.ofSupplier(supplier)
.withThreadPoolBulkhead(threadPoolBulkhead)
.withTimeLimiter(timeLimiter, scheduledExecutorService)
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.get().toCompletableFuture();
关于execution - 在 Resilience4j 中集成断路器、重试和时间限制器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60216375/