使用RateLimiter实现简单的限流

1、pom依赖

<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
</dependency>

2、自定义注解 限流

import java.lang.annotation.*;

/**
 * 自定义注解  限流
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ServiceLimit {
    String description()  default "";
}

3、限流 AOP

import com.google.common.util.concurrent.RateLimiter;
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.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 限流 AOP
 * @author yf
 * @version V1.0.0
 * @title LimitAspect
 * @date 2019/4/26
 */
@Component
@Scope
@Aspect
public class LimitAspect {
    /**
     *  每秒只发出100个令牌,此处是单进程服务的限流,内部采用令牌捅算法实现
     */
    private static RateLimiter rateLimiter = RateLimiter.create(100.0);

    /**
     * service层切点限流
     * @title ServiceAspect
     * @author yf
     * @since v1.0.0
     * @return
     */
    @Pointcut("@annotation(com.yf.yfspringbootapi.annotation.ServiceLimit)")
    public void ServiceAspect() {

    }

    @Around("ServiceAspect()")
    public  Object around(ProceedingJoinPoint joinPoint) {
        //此方法判断是否可以从rateLimiter立即获得令牌
        Boolean flag = rateLimiter.tryAcquire();
        Object obj = null;
        try {
            if(flag){
                obj = joinPoint.proceed();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return obj;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32401031/article/details/89552240