当前位置:网站首页>Redis + lua implements distributed interface current limiting implementation scheme
Redis + lua implements distributed interface current limiting implementation scheme
2022-08-11 07:14:00 【CSDN cloud computing】
作者 | 步尔斯特
来源 | 步尔斯特
前言
redis + lua脚本已然成为了单体项目主流的限流方案.
redis凭借其特性成为了中间件的佼佼者,最新官方测试数据:
读的速度是110000次/s
写的速度是81000次/s.
lua:
减少网络开销:使用Lua脚本,无需向Redis 发送多次请求,执行一次即可,减少网络传输
原子操作:Redis 将整个Lua脚本作为一个命令执行,原子,无需担心并发
复用:Lua脚本一旦执行,会永久保存 Redis 中,,其他客户端可复用
操作
在需要限流的接口处添加如下注解(@RedisLimit),在原有基础上,无需添加任何依赖即可实现限流.
@RedisLimit(name = "订单秒杀", prefix = "seckill", key = "distributed", count = 1, period = 1, limitType = LimitType.IP, msg = "当前排队人数较多,请稍后再试!")
@GetMapping("/limit/distributed/{id}")
public ResponseEntity<Object> limitDistributed(@PathVariable("id") String id) {
return ResponseEntity.ok("成功购买:" + id + "个");
}
介绍
/**
* 资源名称
*/
String name() default "";
/**
* 前缀
*/
String prefix() default "";
/**
* 资源key
*/
String key() default "";
/**
* 最多访问次数
*/
int count();
/**
* 时间,秒级
*/
int period();
/**
* 类型
*/
LimitType limitType() default LimitType.CUSTOMER;
/**
* 提示信息
*/
String msg() default "系统繁忙,请稍后再试";
功能
默认根据全局接口的QPS作为流控指标
可在独立IP和全局接口自由切换
可自定义时间及规定时间内的QPS
可根据key值做后期的数据监控和统计
原理
以流量作为切点、滑动时间窗口作为核心算法.
lua脚本以保证其原子性操作
核心代码(部分展示)
lua脚本
redis.replicate_commands();
local listLen,time
listLen = redis.call('LLEN', KEYS[1])
if listLen and tonumber(listLen) < tonumber(ARGV[1]) then
local a = redis.call('TIME');
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
else
time = redis.call('LINDEX', KEYS[1], -1)
local a = redis.call('TIME');
if a[1]*1000000+a[2] - time < tonumber(ARGV[2])*1000000 then
return 0;
else
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
redis.call('LTRIM', KEYS[1], 0, tonumber(ARGV[1])-1)
end
end
return 1;
切点处理
Long number = redisTemplate.execute(SECKILL_SCRIPT, keys, count, period);
if(number != null && number.intValue() == 1){
return pjp.proceed();
}
往期推荐
一篇文章了解 Docker 的安装、Start up and how it works!
如何从 Docker Extract from mirror dockerfile!
点分享
点收藏
点点赞
点在看
边栏推荐
猜你喜欢
随机推荐
torch.cat()使用方法
HCIP experiments (pap, chap, HDLC, MGRE, RIP)
iptables nat
快速了解集成学习
ETCD Single-Node Fault Emergency Recovery
sql--Users who have purchased more than 3 times (inclusive) within 7 days (including the current day), and the purchase amount in the past 7 days exceeds 1,000
maxwell 概念
华为防火墙-3-应用过滤
Top20 bracket matching
iptables的状态
推荐一个好用的IDEA插件---Translation—中英互译
命令输出给变量
从mask-rcnn到shp
OA项目之项目简介&会议发布
华为防火墙-4-安全策略
daily sql - user retention rate for two days
【LeetCode】2034. 股票价格波动(思路+题解)双map
Pinduoduo api interface application example
Cobbleland 博览会 基础系列 1
自定义MVC增删改查