首页 > 解决方案 > 使用resilience4j 实现节流/去抖动

问题描述

我的问题是如何使用resilience4j 库实现(配置)throttlig。我尝试在下一个配置中使用 RateLimiter:

RateLimiter rateLimiter = RateLimiterRegistry.of(
                RateLimiterConfig.custom()
                        .limitRefreshPeriod(Duration.ofSeconds(10))
                        .limitForPeriod(1)
                        .timeoutDuration(Duration.ofSeconds(5))
                        .build()
        ).rateLimiter("default");

Runnable ratedCall = RateLimiter.decorateRunnable(rateLimiter, () -> { /*business logic here*/ });

// and then

try {
    ratedCall.run();
} catch (Exception e) {
    LOG.warn(e);
}

但它不会阻止(忽略)后续方法调用。我需要实现的是,我的特定方法不能在 10 秒内被调用超过一次,无论是否有任何可能的异常。

请指教!

谢谢!

标签: javathrottlingdebouncedebouncingresilience4j

解决方案


解决方案是将超时持续时间设置为 0:

timeoutDuration(Duration.ofSeconds(0))

在这里回答


推荐阅读