Comments (4)
搭了个环境验证了,和我的猜想一致,当前版本1.1.1由于hashTag设置有误,集群下是不能正确运行的。我fork改好了,但是改动有点多,改了些不需要改的东西,所以就不merge回来了吧...说下主要修改项吧
- 配置redisSerializer
@Bean
@ConditionalOnMissingBean
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
}
2.RedisScript泛型设置为String(返回值被序列化为String,用Long接收报错,也可以想其他办法解决,但没必要)
private RedisScript<String> redisScript;
public RedisRateLimiterCounterImpl(RedisScript<String> redisScript){
this.redisScript=redisScript;
}
3.lua脚本修改,除了key以外其他参数用ARGV接收,lastClearTimeKey设置hashTag
local key = KEYS[1];
local limit = tonumber(ARGV[1]);
local step = tonumber(ARGV[2]);
local interval = tonumber(ARGV[3]);
local nowTime = tonumber(ARGV[4]);
local lastClearTimeKey='syj-rateLimiter-lastClearTime'..'{'..key..'}'
......
return '1';
4.传入参数修改,指定redisSerializer,防止redisTemplate被外层覆盖而影响到序列化逻辑
@Override
public void consume(String key, long limit, long refreshInterval, long tokenBucketStepNum, long tokenBucketTimeInterval) {
List<Object> keyList = Arrays.asList(key);
Object[] argv = {String.valueOf(limit), String.valueOf(tokenBucketStepNum),
String.valueOf(tokenBucketTimeInterval), String.valueOf(Instant.now().getEpochSecond())};
String result = (String) redisTemplate.execute(redisScript, stringRedisSerializer, stringRedisSerializer, keyList, argv);
if(Const.REDIS_ERROR.equals(result)){
throw new RateLimitException(RateLimitErrorEnum.TOO_MANY_REQUESTS);
}
}
from syj-ratelimit.
追评: 我最初测试的时候是直接调用consume方法测的,没有使用注解模拟完整调用链。后来发现使用注解的情况下key是从RateLimiterUtil拼接而成的,上面有hashTag,所以1.1.1版本的代码,是可以正确运行的。大佬,对不起!
但是,把hashTag写死成常量是不合理的,这样所有的key都会路由到同一个slot,造成集群数据倾斜。通常的做法是把hashTag设置成其中一个key,离散度取决于这个key,本例取决于用户输入。
from syj-ratelimit.
多谢提出问题,现已升级为1.1.2版本.现解决方案为:
如果当前key中存在hashTag则使用key
如果不存在则依据这个key添加
from syj-ratelimit.
1.1.2的lua脚本貌似没有及时更新
from syj-ratelimit.
Related Issues (7)
- 计算令牌数取整 HOT 1
- redis适用于集群部署 HOT 1
- 开源登记
- 令牌桶算法问题 HOT 1
- TokenBucketAlgorithmImpl这个类里面的rateLimiter这个对象是怎么注入进去的 HOT 4
- 令牌通算法脚本的BUG HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from syj-ratelimit.