1、基于springboot项目pom.xml添加如下依赖:
org.springframework.boot
spring-boot-starter-aop
com.google.guava
guava
18.0
org.springframework.data
spring-data-redis
2、创建自定义运行时注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LxRateLimit {
/**
*
* @return
*/
String value() default "";
/**
* 每秒向桶中放入令牌的数量 默认最大即不做限流
* @return
*/
double perSecond() default Double.MAX_VALUE;
/**
* 获取令牌的等待时间 默认0
* @return
*/
int timeOut() default 0;
/**
* 超时时间单位
* @return
*/
TimeUnit timeOutUnit() default TimeUnit.MILLISECONDS;
}
3、创建aop切面进行环绕通知:
@Aspect
@Component
public class LxRateLimitAspect {
private final static Logger logger = LoggerFactory.getLogger(LxRateLimitAspect.class);
private RateLimiter rateLimiter = RateLimiter.create(Double.MAX_VALUE);
/**
* 定义切点
* 1、通过扫包切入
* 2、带有指定注解切入
*/
// @Pointcut("execution(public * com.ycn.springcloud.*.*(..))")
@Pointcut("@annotation(com.ycn.springcloud.annotation.LxRateLimit)")
public void checkPointcut() { }
@ResponseBody
@Around(value = "checkPointcut()")
public Object aroundNotice(ProceedingJoinPoint pjp) throws Throwable {
logger.info("拦截到了{}方法...", pjp.getSignature().getName());
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
//获取目标方法
Method targetMethod = methodSignature.getMethod();
if (targetMethod.isAnnotationPresent(LxRateLimit.class)) {
//获取目标方法的@LxRateLimit注解
LxRateLimit lxRateLimit = targetMethod.getAnnotation(LxRateLimit.class);
rateLimiter.setRate(lxRateLimit.perSecond());
if (!rateLimiter.tryAcquire(lxRateLimit.timeOut(), lxRateLimit.timeOutUnit()))
return "服务器繁忙,请稍后再试!";
}
return pjp.proceed();
}
}
在ctroller中使用自定义注解
@RequestMapping("/testAnnotation")
@LxRateLimit(perSecond = 1.0, timeOut = 500)
public String testAnnotation() {
return "get token success";
}
当接口QPS大于1的时候就会返回 “服务器繁忙,请稍后再试!”