execution - 在 Resilience4j 中集成断路器、重试和时间限制器

标签 execution circuit-breaker resilience4j time-limiting resilience4j-retry

我正在尝试使用 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/

相关文章:

python - 区分脚本何时运行 'directly' 或 Python 可执行文件

java - 增加 Hystrix 断路器超时?

c# - 断路器模式中异常过滤的一些实现是什么?

project-reactor - Resilience4j 速率限制器在项目 Reactor 中无法正常工作?

architecture - 每条指令未命中和计算机执行时间的未命中惩罚有何不同?

python - ipython:exit() 语句未按预期工作

java - Hystrix 回退方法不运行

java - 如何从resilience4j TimeLimitter 抛出自定义异常?

java - 弹性4j - 请求超时

JavaScript 方法执行中断