" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用AOP+redis+lua做方法限流的實(shí)現(xiàn)

 更新時(shí)間:2022年04月29日 16:09:10   作者:竹子竹枝  
本文主要介紹了使用AOP+redis+lua做方法限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

需求

公司里使用OneByOne的方式刪除數(shù)據(jù),為了防止一段時(shí)間內(nèi)刪除數(shù)據(jù)過多,讓我這邊做一個(gè)接口限流,超過一定閾值后報(bào)異常,終止刪除操作。

實(shí)現(xiàn)方式

創(chuàng)建自定義注解 @limit 讓使用者在需要的地方配置 count(一定時(shí)間內(nèi)最多訪問次數(shù))period(給定的時(shí)間范圍),也就是訪問頻率。然后通過LimitInterceptor攔截方法的請求, 通過 redis+lua 腳本的方式,控制訪問頻率。

源碼

Limit 注解

用于配置方法的訪問頻率count、period

import javax.validation.constraints.Min;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Limit {
    /**
     * key
     */
    String key() default "";
    /**
     * Key的前綴
     */
    String prefix() default "";
    /**
     * 一定時(shí)間內(nèi)最多訪問次數(shù)
     */
    @Min(1)
    int count();
    /**
     * 給定的時(shí)間范圍 單位(秒)
     */
    @Min(1)
    int period();
    /**
     * 限流的類型(用戶自定義key或者請求ip)
     */
    LimitType limitType() default LimitType.CUSTOMER;
}

LimitKey

用于標(biāo)記參數(shù),作為redis key值的一部分

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitKey {
}

LimitType

枚舉,redis key值的類型,支持自定義key和ip、methodName中獲取key

public enum LimitType {
    /**
     * 自定義key
     */
    CUSTOMER,
    /**
     * 請求者IP
     */
    IP,
    /**
     * 方法名稱
     */
    METHOD_NAME;
}

RedisLimiterHelper

初始化一個(gè)限流用到的redisTemplate Bean

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisLimiterHelper {
    @Bean
    public RedisTemplate<String, Serializable> limitRedisTemplate(@Qualifier("defaultStringRedisTemplate") StringRedisTemplate redisTemplate) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisTemplate.getConnectionFactory());
        return template;
    }
}

LimitInterceptor

使用 aop 的方式來攔截請求,控制訪問頻率

import com.google.common.collect.ImmutableList;
import com.yxt.qida.api.bean.service.xxv2.openapi.anno.Limit;
import com.yxt.qida.api.bean.service.xxv2.openapi.anno.LimitKey;
import com.yxt.qida.api.bean.service.xxv2.openapi.anno.LimitType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@Slf4j
@Aspect
@Configuration
public class LimitInterceptor {
    private static final String UNKNOWN = "unknown";
    private final RedisTemplate<String, Serializable> limitRedisTemplate;
    @Autowired
    public LimitInterceptor(RedisTemplate<String, Serializable> limitRedisTemplate) {
        this.limitRedisTemplate = limitRedisTemplate;
    }
    @Around("execution(public * *(..)) && @annotation(com.yxt.qida.api.bean.service.xxv2.openapi.anno.Limit)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Limit limitAnnotation = method.getAnnotation(Limit.class);
        LimitType limitType = limitAnnotation.limitType();
        int limitPeriod = limitAnnotation.period();
        int limitCount = limitAnnotation.count();
        /**
         * 根據(jù)限流類型獲取不同的key ,如果不傳我們會(huì)以方法名作為key
         */
        String key;
        switch (limitType) {
            case IP:
                key = getIpAddress();
                break;
            case CUSTOMER:
                key = limitAnnotation.key();
                break;
            case METHOD_NAME:
                String methodName = method.getName();
                key = StringUtils.upperCase(methodName);
                break;
            default:
                throw new RuntimeException("limitInterceptor - 無效的枚舉值");
        }
        /**
         * 獲取注解標(biāo)注的 key,這個(gè)是優(yōu)先級最高的,會(huì)覆蓋前面的 key 值
         */
        Object[] args = pjp.getArgs();
        Annotation[][] paramAnnoAry = method.getParameterAnnotations();
        for (Annotation[] item : paramAnnoAry) {
            int paramIndex = ArrayUtils.indexOf(paramAnnoAry, item);
            for (Annotation anno : item) {
                if (anno instanceof LimitKey) {
                    Object arg = args[paramIndex];
                    if (arg instanceof String && StringUtils.isNotBlank((String) arg)) {
                        key = (String) arg;
                        break;
                    }
                }
            }
        }
        if (StringUtils.isBlank(key)) {
            throw new RuntimeException("limitInterceptor - key值不能為空");
        }
        String prefix = limitAnnotation.prefix();
        String[] keyAry = StringUtils.isBlank(prefix) ? new String[]{"limit", key} : new String[]{"limit", prefix, key};
        ImmutableList<String> keys = ImmutableList.of(StringUtils.join(keyAry, "-"));
        try {
            String luaScript = buildLuaScript();
            RedisScript<Number> redisScript = new DefaultRedisScript<Number>(luaScript, Number.class);
            Number count = limitRedisTemplate.execute(redisScript, keys, limitCount, limitPeriod);
            if (count != null && count.intValue() <= limitCount) {
                return pjp.proceed();
            } else {
                String classPath = method.getDeclaringClass().getName() + "." + method.getName();
                throw new RuntimeException("limitInterceptor - 限流被觸發(fā):"
                        + "class:" + classPath
                        + ", keys:" + keys
                        + ", limitcount:" + limitCount
                        + ", limitPeriod:" + limitPeriod + "s");
            }
        } catch (Throwable e) {
            if (e instanceof RuntimeException) {
                throw new RuntimeException(e.getLocalizedMessage());
            }
            throw new RuntimeException("limitInterceptor - 限流服務(wù)異常");
        }
    }
    /**
     * lua 腳本,為了保證執(zhí)行 redis 命令的原子性
     */
    public String buildLuaScript() {
        StringBuilder lua = new StringBuilder();
        lua.append("local c");
        lua.append("\nc = redis.call('get',KEYS[1])");
        // 調(diào)用不超過最大值,則直接返回
        lua.append("\nif c and tonumber(c) > tonumber(ARGV[1]) then");
        lua.append("\nreturn c;");
        lua.append("\nend");
        // 執(zhí)行計(jì)算器自加
        lua.append("\nc = redis.call('incr',KEYS[1])");
        lua.append("\nif tonumber(c) == 1 then");
        // 從第一次調(diào)用開始限流,設(shè)置對應(yīng)鍵值的過期
        lua.append("\nredis.call('expire',KEYS[1],ARGV[2])");
        lua.append("\nend");
        lua.append("\nreturn c;");
        return lua.toString();
    }
    public String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

TestService

使用方式示例

    @Limit(period = 10, count = 10)
    public String delUserByUrlTest(@LimitKey String token, String thirdId, String url) throws IOException {
        return "success";
    }

到此這篇關(guān)于使用AOP+redis+lua做方法限流的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)AOP+redis+lua限流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • RedisTemplate常用操作方法總結(jié)(set、hash、list、string等)

    RedisTemplate常用操作方法總結(jié)(set、hash、list、string等)

    本文主要介紹了RedisTemplate常用操作方法總結(jié),主要包括了6種常用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 淺談Redis跟MySQL的雙寫問題解決方案

    淺談Redis跟MySQL的雙寫問題解決方案

    項(xiàng)目中有遇到這個(gè)問題,跟MySQL中的數(shù)據(jù)不一致,記錄一下,本文主要介紹了Redis跟MySQL的雙寫問題解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 動(dòng)態(tài)添加Redis密碼認(rèn)證的方法

    動(dòng)態(tài)添加Redis密碼認(rèn)證的方法

    本篇文章主要介紹了動(dòng)態(tài)添加Redis密碼認(rèn)證的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Redis Cluster的圖文講解

    Redis Cluster的圖文講解

    今天小編就為大家分享一篇關(guān)于Redis Cluster的圖文講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 如何高效使用Redis作為LRU緩存

    如何高效使用Redis作為LRU緩存

    這篇文章主要介紹了如何高效使用Redis作為LRU緩存,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Redis集群服務(wù)器的實(shí)現(xiàn)(圖文步驟)

    Redis集群服務(wù)器的實(shí)現(xiàn)(圖文步驟)

    本文介紹了Redis集群服務(wù)器的優(yōu)勢,為讀者提供了全面的Redis集群服務(wù)器知識和使用技巧,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • redis的hash類型操作方法

    redis的hash類型操作方法

    Hash 是一個(gè) String 類型的 field(字段) 和 value(值) 的映射表,hash 特別適合用于存儲(chǔ)對象,這篇文章主要介紹了redis的hash類型的詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • Redis源碼設(shè)計(jì)剖析之事件處理示例詳解

    Redis源碼設(shè)計(jì)剖析之事件處理示例詳解

    這篇文章主要為大家介紹了Redis源碼設(shè)計(jì)剖析之事件處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Redis之RedisTemplate配置方式(序列和反序列化)

    Redis之RedisTemplate配置方式(序列和反序列化)

    這篇文章主要介紹了Redis之RedisTemplate配置方式(序列和反序列化),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Redis教程(六):Sorted-Sets數(shù)據(jù)類型

    Redis教程(六):Sorted-Sets數(shù)據(jù)類型

    這篇文章主要介紹了Redis教程(六):Sorted-Sets數(shù)據(jù)類型,本文講解了Sorted-Sets數(shù)據(jù)類型概述、相關(guān)命令列表、命令使用示例、應(yīng)用范圍等內(nèi)容,需要的朋友可以參考下
    2015-04-04

最新評論