api接口限流是高并发系统的重要保护措施之一
开发步骤:
1、自定义一个AccessLimit注解,主要有两个变量maxCount和second
import java.lang.annotation.*;
@Inherited
@Documented
@Target({
ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessLimit {
/**
* 指定时间内API请求次数
* @return
*/
int maxCount() default 5;
/**
* 请求次数的指定时间间隔(redis数据过期时间)
* @return
*/
int second() default 60;
}
2、定义一个类实现HandlerInterceptor的preHandle方法对标注解的方法进行拦截处理