{}", count, keys);
if (count != null && count == 0) {
log.debug("令牌桶={}, 获取令牌失效,接口触发限流", key);
throw new RuntimeException("code:10X, redisLimit.msg()");
}
}
}
}
lua脚本代码
注意:脚本代码是放在 resources 文件下的,它的类型是 txt,名称后缀是lua。如果你不想改名称,就使用我写好的全名--> rateLimiter.lua
点击查看脚本代码
--获取KEY
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local curentLimit = tonumber(redis.call('get', key) or "0")
if curentLimit + 1 > limit
then return 0
else
-- 自增长 1
redis.call('INCRBY', key, 1)
-- 设置过期时间
redis.call('EXPIRE', key, ARGV[2])
return curentLimit + 1
end
最后为了照顾纯小白,给大家看一下我的目录结构
|