|
@@ -42,6 +42,10 @@ public class RedisRateLimitService implements RateLimitService, RedisConstant {
|
|
//设置了限流时间段,使用Redisson内置限流器
|
|
//设置了限流时间段,使用Redisson内置限流器
|
|
if (rule.getPeriod().toMillis() > 0) {
|
|
if (rule.getPeriod().toMillis() > 0) {
|
|
RRateLimiter limiter = redissonClient.getRateLimiter(key);
|
|
RRateLimiter limiter = redissonClient.getRateLimiter(key);
|
|
|
|
+ //Key已存在但配置不同需要先删除
|
|
|
|
+ if (limiter.isExists() && !compareConfig(limiter.getConfig(), rule)) {
|
|
|
|
+ limiter.delete();
|
|
|
|
+ }
|
|
limiter.trySetRate(RateType.OVERALL, rule.getCount(), rule.getPeriod().toMillis(),
|
|
limiter.trySetRate(RateType.OVERALL, rule.getCount(), rule.getPeriod().toMillis(),
|
|
RateIntervalUnit.MILLISECONDS);
|
|
RateIntervalUnit.MILLISECONDS);
|
|
//限流单元默认24小时后自动清理
|
|
//限流单元默认24小时后自动清理
|
|
@@ -58,4 +62,9 @@ public class RedisRateLimitService implements RateLimitService, RedisConstant {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private boolean compareConfig(RateLimiterConfig config, RateLimitRule rule) {
|
|
|
|
+ return config != null && config.getRateType().equals(RateType.OVERALL) && config.getRate() == rule.getCount()
|
|
|
|
+ && config.getRateInterval() == rule.getPeriod().toMillis();
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|