1. 程式人生 > >Guava RateLimiter + AOP註解實現單機限流、統計QPS

Guava RateLimiter + AOP註解實現單機限流、統計QPS

1、基於springboot專案pom.xml新增如下依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
</dependency>

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的時候就會返回 “伺服器繁忙