Springboot基于Guava+自定义注解实现限流功能
之前写过Springboot使用AOP+自定义注解方式实现日志记录、使用Guava中RateLimiter进行限流,那么我们能不能基于Guava+自定义注解实现限流功能呢?
关于实现限流其实还有很多解决方法,如使用redis来实现。这里我不想让项目依赖于redis来增加项目的复杂度,后面有时间的话可以在写一下基于redis的方式。
实现步骤
1、定义一个方法级别的@LimitAspect注解
package cn.pconline.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Description 基于Guava限流注解
* @Author jie.zhao
* @Date 2019/8/19 9:49
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Limit {
//资源名称
String name() default "";
// 限制每秒访问次数,默认最大即不限制
double perSecond() default Double.MAX_VALUE;
}
2、定义切面和切点
package cn.pconline.common.aspect;
import cn.pconline.common.annotation.Limit;
import cn.pconline.common.exeception.LimitAccessException;
import com.google.common.util.concurrent.RateLimiter;
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.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
/**
* @Description 基于Guava限流
* @Author jie.zhao
* @Date 2019/8/19 9:55
*/
@Slf4j
@Aspect
@Component
public class LimitAspect {
RateLimiter rateLimiter = RateLimiter.create(Double.MAX_VALUE);
@Pointcut("@annotation(cn.pconline.common.annotation.Limit)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
//获取目标方法
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
Limit limit = method.getAnnotation(Limit.class);
rateLimiter.setRate(limit.perSecond());
// 获取令牌桶中的一个令牌,最多等待1秒
if (rateLimiter.tryAcquire(1, 1, TimeUnit.SECONDS)) {
return point.proceed();
} else {
log.error(limit.name() + " 接口并发量过大执行限流");
throw new LimitAccessException("网络异常,请稍后重试!");
}
}
}
3、自定义异常
package cn.pconline.common.exeception;
/**
* @Description 限流自定义异常
* @Author jie.zhao
* @Date 2019/8/7 16:01
*/
public class LimitAccessException extends RuntimeException {
private static final long serialVersionUID = -3608667856397125671L;
public LimitAccessException(String message) {
super(message);
}
}
4、使用方法
@GetMapping("/index")
@Limit(name = "测试限流每秒3个请求", perSecond = 3)
public Object index() {
return "SUCCESS !";
}