一、基於springboot項目pom.xml添加以下依賴:java
<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>
二、建立自定義運行時註解:redis
@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; }
三、建立aop切面進行環繞通知:spring
@Aspect @Component public class LxRateLimitAspect { private final static Logger logger = LoggerFactory.getLogger(LxRateLimitAspect.class); private RateLimiter rateLimiter = RateLimiter.create(Double.MAX_VALUE); /** * 定義切點 * 一、經過掃包切入 * 二、帶有指定註解切入 */ // @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中使用自定義註解springboot
@RequestMapping("/testAnnotation") @LxRateLimit(perSecond = 1.0, timeOut = 500) public String testAnnotation() { return "get token success"; }
當接口QPS大於1的時候就會返回 「服務器繁忙,請稍後再試!」服務器