自定义注解实现接口限流
自定义注解实现接口限流
场景:限制验证码在单位时间内的访问次数
实现流程:自定义一个注解,注解内包含访问的次数与单位时间。通过AOP进行切面拦截,获取注解内的次数和时间,获取请求的uri与访问者ip。组成redis的key。
使用redis将key进行原子性自增1.如果返回的是1.则设置过期时间,之后的访问直接incr即可。但是这种方式存在问题,不是原子性的,所以需要采用原子性的设置key和过期时间。
Redis官方目前set命令已经支持了set的时候指定是nx,并且设置过期时间合并成了一条指令,保证了原子性。
因此采用setIfAbsent()API进行设置。如果返回的true。代表首次设置key。如果已经存在,则返回false。代表key已经存在。则使用incr进行自增即可。
代码实现
/**
* 处理多次请求问题,限制指定时间内只能访问的次数
*/
@Aspect
@Component
public class ReqLimitSAspect {
@Autowired
private RedisTemplate redisTemplate;
@Around("@annotation(com.tute.edu.planetcommunity.annotation.ReqLmit)")
public Object hanlderReqLimit(ProceedingJoinPoint point) {
MethodSignature methodSignature = (MethodSignature) point.getSignature();
ReqLmit annotation = methodSignature.getMethod().getAnnotation(ReqLmit.class);
long count = annotation.count();
int time = annotation.time();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//获取访问者的ip
//获取访问的uri
String requestURI = request.getRequestURI();
String ipAddr = IpUtils.getIpAddr(request);
String key = "req:limit:" + ipAddr + ":" + requestURI;
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, 1, time, TimeUnit.SECONDS);
Long countIncr=0L;
if (!aBoolean) {
countIncr = redisTemplate.opsForValue().increment(key);
}
if (countIncr > count) {
throw new CustomException("调用频繁,请稍后再试!");
}
try {
return point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
一万年太久,只争朝夕!
本文作者:暮雪超霸
本文链接:https://www.cnblogs.com/chaoba/p/16004004.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步