设为首页 加入收藏

TOP

使用 redis 实现分布式接口限流注解 RedisLimit(二)
2023-09-23 15:44:14 】 浏览:113
Tags:使用 redis RedisLimit
{}", 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

最后为了照顾纯小白,给大家看一下我的目录结构

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇带有权重的随机算法 下一篇Service 层异常抛到 Controller ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目