自定义注解+aop实现接口限流

自定义注解+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环绕通知
  1. 环绕通知中执行目标方法时执行顺序:

环绕通知开始执行------ 前置通知 -----目标方法开始执行----- 环绕通知结束执行----- 后置通知

  1. 环绕通知中不执行目标方法时执行顺序:

    (前置通知不执行) 环绕通知开始执行----- 环绕通知结束执行--------- 后置通知

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);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值