基于Redis、AOP、注解實(shí)現(xiàn)的單用戶限流

  • Redis簡(jiǎn)介
  1. 使用內(nèi)存存儲(chǔ),使用單線程,采用IO多路復(fù)用模型,性能極好
  2. 支持String,Hash,List,Set,Zset等多種數(shù)據(jù)類型
  3. 支持key定時(shí)失效
  4. 可采用AOF,RDB方式進(jìn)行持久化
  • AOP簡(jiǎn)介
  1. 定義:面向切面編程,將跟業(yè)務(wù)邏輯無關(guān)的重復(fù)并且縈繞在方法周圍的代碼進(jìn)行抽取,提高了代碼的可重用性
  2. 常用注解:


    切面.PNG
  • 注解簡(jiǎn)介
  1. 使用條件:定義注解,聲明注解的生命周期、作用域,注解實(shí)現(xiàn)體
  2. 定義在自定義注解上的元注解:
    @Target:注解的作用域,包含ElementType參數(shù)
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,//作用域在接口、類、枚舉、注解

    /** Field declaration (includes enum constants) */
    FIELD,//作用域在字段、枚舉的常量

    /** Method declaration */
    METHOD,//作用域在方法

    /** Formal parameter declaration */
    PARAMETER,//作用域在方法參數(shù)

    /** Constructor declaration */
    CONSTRUCTOR,//作用域在構(gòu)造器

    /** Local variable declaration */
    LOCAL_VARIABLE,//作用域在局部變量

    /** Annotation type declaration */
    ANNOTATION_TYPE,//作用域在注解

    /** Package declaration */
    PACKAGE,//作用域在包

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,//作用域在類型參數(shù)

    /**
     * Use of a type 
     *
     * @since 1.8
     */
    TYPE_USE//作用域在使用類型的任何地方
}

@Retention:注解的作用域,包含RetentionPolicy參數(shù)

/**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,//存活在源文件中

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,//存活在字節(jié)碼文件中

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME//存活在代碼運(yùn)行期間

@Inherited:允許子類繼承父類的注解
@Documented:此注解會(huì)包含在javadoc中

  • 代碼實(shí)現(xiàn)
    定義注解:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RestrictAccess {
    /**
     * 限制時(shí)間,單位是毫秒
     */
    long ttl() default 0;

    /**
     * 限制時(shí)間內(nèi)的訪問次數(shù)
     */
    int accessFrequency();
}

RedisTemplate對(duì)象的配置

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Integer> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        final RedisTemplate<String,Integer> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

切面類

/**
 * 單用戶限流切面
 */
/**
 * 單用戶限流切面
 */
@Aspect
@Component
@RequiredArgsConstructor
@Slf4j
public class RestrictAccessAspect {
    private final RedisTemplate<String,Integer> redisTemplate;
    private static Integer INIT_VALUE = 1;
    /**
     * 用戶登錄的令牌
     */
    private static final String USER_TOKEN = "token";

    @Around("@annotation(cn.juh.annocation.RestrictAccess)")
    public Object RestrictAccessFrequency(ProceedingJoinPoint point) throws Throwable {
        //獲取當(dāng)前線程的訪問對(duì)象
        HttpServletRequest request = WebUtils.getHttpServletRequest();
        //獲取訪問路徑
        String requestURI = request.getRequestURI();
        //獲取用戶token
        String token = request.getParameter(USER_TOKEN);
        //存在redis中的key
        String key = RedisConstant.KeyPrefix.RESTRICT_ACCESS.code() + requestURI + ":" + token;

        MethodSignature sign = (MethodSignature) point.getSignature();
        Method method = sign.getMethod();
        //獲取方法上的注解
        RestrictAccess annotation = method.getAnnotation(RestrictAccess.class);
        int accessFrequency = annotation.accessFrequency();
        long ttl = annotation.ttl();

        //從redis中獲取用戶再限定時(shí)間內(nèi)訪問接口的次數(shù)
        Integer value = redisTemplate.opsForValue().get(key);
        if (Objects.nonNull(value) && value >= accessFrequency){
            return "您的操作過于頻繁,請(qǐng)待會(huì)再試";
        }

        if (Objects.isNull(value)){
            //不存在key則設(shè)置初始值并且設(shè)置過期時(shí)間
            redisTemplate.opsForValue().set(key,INIT_VALUE,ttl, TimeUnit.MILLISECONDS);
        }else {
            //存在則將訪問次數(shù)+1
            redisTemplate.opsForValue().increment(key);
        }

        //執(zhí)行接口邏輯
        return point.proceed();
    }

}

獲取web對(duì)象工具

public class WebUtils {
    public static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 得到當(dāng)前線程的請(qǐng)求對(duì)象
     *
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        return getServletRequestAttributes().getRequest();
    }

    /**
     * 得到當(dāng)前線程的響應(yīng)對(duì)象
     *
     * @return
     */
    public static HttpServletResponse getHttpServletResponse() {
        return getServletRequestAttributes().getResponse();
    }

}

測(cè)試接口

/**
 * 控制器
 */
@RestController
public class TestController {
    @RequestMapping("/test")
    @RestrictAccess(ttl = 10000,accessFrequency = 1)
    public Object first(String token) {
        return "test";
    }
}

正常訪問:


正常訪問.PNG

限流:


限流.PNG
  • 代碼邏輯
    程序執(zhí)行邏輯.PNG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容