自定义注解+aop实现接口限流
一、自定义注解
@Target(ElementType.METHOD) //指定注解在方法上
@Retention(RetentionPolicy.RUNTIME) //指定新注解保留到程序运行时期
@Inherited // 指定新注解标注在父类上时可以被子类继承
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimiterQry {
// 限流方法名称
String name() default "";
// 每秒允许访问的次数20,底层令牌
double token() default 20;
}
二、aop环绕通知
- 环绕通知中执行目标方法时执行顺序:
环绕通知开始执行------ 前置通知 -----目标方法开始执行----- 环绕通知结束执行----- 后置通知
-
环绕通知中不执行目标方法时执行顺序:
(前置通知不执行) 环绕通知开始执行----- 环绕通知结束执行--------- 后置通知
package com.lzs.annotationdemo.aop;
import com.google.common.util.concurrent.RateLimiter;
import com.lzs.annotationdemo.annotation.RateLimiterQry;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Aspect
public class CurrentLimitAop {
// 缓存RateLimiter对象
private ConcurrentHashMap<String, RateLimiter> map = new ConcurrentHashMap<>();
// 只要方法上有RateLimiterQry这个自定义限流注解,就执行aop前置通知(不会拦截)
@Before(value = "@annotation(com.lzs.annotationdemo.annotation.RateLimiterQry)")
public void before() {
System.out.println("前置通知");
}
// 只要方法上有RateLimiterQry这个自定义限流注解,就会执行aop后置通知(不会拦截)
@AfterReturning(value = "@annotation(com.lzs.annotationdemo.annotation.RateLimiterQry)")
public void after() {
System.out.println("后置通知");
}
// 只要方法上有RateLimiterQry这个自定义限流注解,就会被aop环绕通知拦截
@Around(value = "@annotation(com.lzs.annotationdemo.annotation.RateLimiterQry)")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
// 获取拦截方法名(get)
Signature signature = proceedingJoinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
System.out.println(methodSignature.getName()); // get
// 获取拦截方法上的注解
RateLimiterQry declaredAnnotation = methodSignature.getMethod()
.getDeclaredAnnotation(RateLimiterQry.class);
// 获取注解上面的值
double token = declaredAnnotation.token();
String name = declaredAnnotation.name();
RateLimiter rateLimiter = map.get(name);
if (rateLimiter == null) {
rateLimiter = RateLimiter.create(token);
map.put(name, rateLimiter);
}
boolean result = rateLimiter.tryAcquire();
if (!result) {
return "当前访问人数过多,请稍后重试";
}
try {
System.out.println("环绕通知开始执行");
// 执行目标方法
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕通知结束执行");
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
return "系统内部错误";
}
}
}
三、目标方法controller
package com.lzs.annotationdemo.day01;
import com.lzs.annotationdemo.annotation.RateLimiterQry;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AnnotationController {
@RequestMapping("/get")
@RateLimiterQry(name = "get", token = 1)
public String get() {
System.out.println("目标方法开始执行");
return "GET";
}
}
四、依赖
<!--谷歌限流依赖-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
五、总结
1.自定义注解,设置注解方法和默认值
2.定义aop环绕通知方法
// 拦截有该注解的方法
@Around(value = "@annotation(com.lzs.annotationdemo.annotation.RateLimiterQry)")
3.了解MethodSignature
// 获取拦截方法
Signature signature = proceedingJoinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
// 获取拦截方法上的注解
RateLimiterQry declaredAnnotation =
methodSignature.getMethod().getDeclaredAnnotation(RateLimiterQry.class);
4.了解谷歌限流api
RateLimiter rateLimiter= RateLimiter.create(1.0);