欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Redis+Lua脚本+AOP+反射+自定义注解,打造我司内部基础架构限流组件

Redis+Lua脚本+AOP+反射+自定义注解,打造我司内部基础架构限流组件

2024/10/24 17:33:03 来源:https://blog.csdn.net/ZHUSHANGLIN/article/details/140610948  浏览:    关键词:Redis+Lua脚本+AOP+反射+自定义注解,打造我司内部基础架构限流组件

定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface RedisLimitAnnotation
{/*** 资源的key,唯一* 作用:不同的接口,不同的流量控制*/String key() default "";/*** 最多的访问限制次数*/long permitsPerSecond() default 3;/*** 过期时间(计算窗口时间),单位秒默认30*/long expire() default 30;/*** 默认温馨提示语*/String msg() default "default message:系统繁忙or你点击太快,请稍后再试,谢谢";
}

定义AOP

import com.atguigu.interview2.annotations.RedisLimitAnnotation;
import com.atguigu.interview2.exception.RedisLimitException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.stereotype.Component;
import org.springframework.core.io.ClassPathResource;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;@Slf4j
@Aspect
@Component
public class RedisLimitAop
{Object result = null;@Resourceprivate StringRedisTemplate stringRedisTemplate;private DefaultRedisScript<Long> redisLuaScript;@PostConstructpublic void init(){redisLuaScript = new DefaultRedisScript<>();redisLuaScript.setResultType(Long.class);redisLuaScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("rateLimiter.lua")));}@Around("@annotation(com.atguigu.interview2.annotations.RedisLimitAnnotation)")public Object around(ProceedingJoinPoint joinPoint){System.out.println("---------环绕通知1111111");MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();//拿到RedisLimitAnnotation注解,如果存在则说明需要限流,容器捞鱼思想RedisLimitAnnotation redisLimitAnnotation = method.getAnnotation(RedisLimitAnnotation.class);if (redisLimitAnnotation != null){//获取redis的keyString key = redisLimitAnnotation.key();String className = method.getDeclaringClass().getName();String methodName = method.getName();String limitKey = key +"\t"+ className+"\t" + methodName;log.info(limitKey);if (null == key){throw new RedisLimitException("it's danger,limitKey cannot be null");}long limit = redisLimitAnnotation.permitsPerSecond();long expire = redisLimitAnnotation.expire();List<String> keys = new ArrayList<>();keys.add(key);Long count = stringRedisTemplate.execute(redisLuaScript,keys,String.valueOf(limit),String.valueOf(expire));System.out.println("Access try count is "+count+" \t key= "+key);if (count != null && count == 0){System.out.println("启动限流功能key: "+key);return redisLimitAnnotation.msg();}}try {result = joinPoint.proceed();//放行} catch (Throwable e) {throw new RuntimeException(e);}System.out.println("---------环绕通知2222222");System.out.println();System.out.println();return result;}}

lua脚本

rateLimiter.lua放在resource目录下

--获取KEY,针对那个接口进行限流,Lua脚本中的数组索引默认是从1开始的而不是从零开始。
local key = KEYS[1]
--获取注解上标注的限流次数
local limit = tonumber(ARGV[1])local curentLimit = tonumber(redis.call('get', key) or "0")--超过限流次数直接返回零,否则再走else分支
if curentLimit + 1 > limit
then return 0
-- 首次直接进入
else-- 自增长 1redis.call('INCRBY', key, 1)-- 设置过期时间redis.call('EXPIRE', key, ARGV[2])return curentLimit + 1
end

接口使用

@Slf4j
@RestController
public class RedisLimitController
{/*** Redis+Lua脚本+AOP+反射+自定义注解,打造我司内部基础架构限流组件* 在redis中,假定一秒钟只能有3次访问,超过3次报错* key = redisLimit* Value = permitsPerSecond设置的具体值* 过期时间 = expire设置的具体值,* permitsPerSecond = 3, expire = 10* 表示本次10秒内最多支持3次访问,到了3次后开启限流,过完本次10秒钟后才解封放开,可以重新访问*/@GetMapping("/redis/limit/test")@RedisLimitAnnotation(key = "redisLimit", permitsPerSecond = 3, expire = 10, msg = "当前访问人数较多,请稍后再试,自定义提示!")public String redisLimit(){return "正常业务返回,订单流水:"+ IdUtil.fastUUID();}
}

测试效果

连续刷新几次调接口,第四次就会限流
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对于公司不能用第三方组件来限流的时候,这个方法很哇塞,下课!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com