簡介
市面上很多介紹redis如何實現限流的,但是大部分都有一個缺點,就是只能實現單一的限流,比如1分鐘訪問1次或者60分鐘訪問10次這種,但是如果想一個接口兩種規則都需要滿足呢,我們的項目又是分布式項目,應該如何解決,下面就介紹一下redis實現分布式多規則限流的方式。
思考
如何一分鐘只能發送一次驗證碼,一小時只能發送10次驗證碼等等多種規則的限流
如何防止接口被惡意打擊(短時間內大量請求)
如何限制接口規定時間內訪問次數
解決方法
記錄某IP訪問次數
使用 String結構 記錄固定時間段內某用戶IP訪問某接口的次數
RedisKey = prefix : className : methodName
RedisVlue = 訪問次數
攔截請求:
初次訪問時設置 「[RedisKey] [RedisValue=1] [規定的過期時間]」
獲取 RedisValue 是否超過規定次數,超過則攔截,未超過則對 RedisKey 進行加1
分析: 規則是每分鐘訪問 1000 次
考慮并發問題
假設目前 RedisKey => RedisValue 為 999
目前大量請求進行到第一步( 獲取Redis請求次數 ),那么所有線程都獲取到了值為999,進行判斷都未超過限定次數則不攔截,導致實際次數超過 1000 次
「解決辦法:」
保證方法執行原子性(加鎖、lua)
考慮在臨界值進行訪問
思考下圖
代碼實現: 比較簡單,
Zset解決臨界值問題
使用 Zset 進行存儲,解決臨界值訪問問題
網上幾乎都有實現,這里就不過多介紹
實現多規則限流
先確定最終需要的效果
能實現多種限流規則
能實現防重復提交
通過以上要求設計注解(先想象出最終實現效果)
@RateLimiter( rules={ //60秒內只能訪問10次 @RateRule(count=10,time=60,timeUnit=TimeUnit.SECONDS), //120秒內只能訪問20次 @RateRule(count=20,time=120,timeUnit=TimeUnit.SECONDS) }, //防重復提交(5秒鐘只能訪問1次) preventDuplicate=true )
編寫注解(RateLimiter,RateRule)
編寫 RateLimiter 注解。
/** *@Description:請求接口限制 *@Author:yiFei */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Inherited public@interfaceRateLimiter{ /** *限流key */ Stringkey()defaultRedisKeyConstants.RATE_LIMIT_CACHE_PREFIX; /** *限流類型(默認Ip模式) */ LimitTypeEnumlimitType()defaultLimitTypeEnum.IP; /** *錯誤提示 */ ResultCodemessage()defaultResultCode.REQUEST_MORE_ERROR; /** *限流規則(規則不可變,可多規則) */ RateRule[]rules()default{}; /** *防重復提交值 */ booleanpreventDuplicate()defaultfalse; /** *防重復提交默認值 */ RateRulepreventDuplicateRule()default@RateRule(count=1,time=5); }
編寫RateRule注解
@Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited public@interfaceRateRule{ /** *限流次數 */ longcount()default10; /** *限流時間 */ longtime()default60; /** *限流時間單位 */ TimeUnittimeUnit()defaultTimeUnit.SECONDS; }
攔截注解 RateLimiter
確定redis存儲方式
RedisKey = prefix : className : methodName
RedisScore = 時間戳
RedisValue = 任意分布式不重復的值即可
編寫生成 RedisKey 的方法
/** *通過rateLimiter和joinPoint拼接prefix:ip/userId:classSimpleName-methodName * *@paramrateLimiter提供prefix *@paramjoinPoint提供classSimpleName:methodName *@return */ publicStringgetCombineKey(RateLimiterrateLimiter,JoinPointjoinPoint){ StringBufferkey=newStringBuffer(rateLimiter.key()); //不同限流類型使用不同的前綴 switch(rateLimiter.limitType()){ //XXX可以新增通過參數指定參數進行限流 caseIP: key.append(IpUtil.getIpAddr(((ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest())).append(":"); break; caseUSER_ID: SysUserDetailsuser=SecurityUtil.getUser(); if(!ObjectUtils.isEmpty(user))key.append(user.getUserId()).append(":"); break; caseGLOBAL: break; } MethodSignaturesignature=(MethodSignature)joinPoint.getSignature(); Methodmethod=signature.getMethod(); Class>targetClass=method.getDeclaringClass(); key.append(targetClass.getSimpleName()).append("-").append(method.getName()); returnkey.toString(); }
編寫lua腳本
編寫lua腳本 (兩種將時間添加到Redis的方法)。
Zset的UUID value值
UUID(可用其他有相同的特性的值)為Zset中的value值
參數介紹
KEYS[1] = prefix : ? : className : methodName
KEYS[2] = 唯一ID
KEYS[3] = 當前時間
ARGV = [次數,單位時間,次數,單位時間, 次數, 單位時間 ...]
由java傳入分布式不重復的 value 值
--1.獲取參數 localkey=KEYS[1] localuuid=KEYS[2] localcurrentTime=tonumber(KEYS[3]) --2.以數組最大值為ttl最大值 localexpireTime=-1; --3.遍歷數組查看是否超過限流規則 fori=1,#ARGV,2do localrateRuleCount=tonumber(ARGV[i]) localrateRuleTime=tonumber(ARGV[i+1]) --3.1判斷在單位時間內訪問次數 localcount=redis.call('ZCOUNT',key,currentTime-rateRuleTime,currentTime) --3.2判斷是否超過規定次數 iftonumber(count)>=rateRuleCountthen returntrue end --3.3判斷元素最大值,設置為最終過期時間 ifrateRuleTime>expireTimethen expireTime=rateRuleTime end end --4.redis中添加當前時間 redis.call('ZADD',key,currentTime,uuid) --5.更新緩存過期時間 redis.call('PEXPIRE',key,expireTime) --6.刪除最大時間限度之前的數據,防止數據過多 redis.call('ZREMRANGEBYSCORE',key,0,currentTime-expireTime) returnfalse
根據時間戳作為Zset中的value值
參數介紹
KEYS[1] = prefix : ? : className : methodName
KEYS[2] = 當前時間
ARGV = [次數,單位時間,次數,單位時間, 次數, 單位時間 ...]
根據時間進行生成value值,考慮同一毫秒添加相同時間值問題
以下為第二種實現方式,在并發高的情況下效率低,value是通過時間戳進行添加,但是訪問量大的話會使得一直在調用 redis.call('ZADD', key, currentTime, currentTime),但是在不沖突value的情況下,會比生成 UUID 好
--1.獲取參數 localkey=KEYS[1] localcurrentTime=KEYS[2] --2.以數組最大值為ttl最大值 localexpireTime=-1; --3.遍歷數組查看是否越界 fori=1,#ARGV,2do localrateRuleCount=tonumber(ARGV[i]) localrateRuleTime=tonumber(ARGV[i+1]) --3.1判斷在單位時間內訪問次數 localcount=redis.call('ZCOUNT',key,currentTime-rateRuleTime,currentTime) --3.2判斷是否超過規定次數 iftonumber(count)>=rateRuleCountthen returntrue end --3.3判斷元素最大值,設置為最終過期時間 ifrateRuleTime>expireTimethen expireTime=rateRuleTime end end --4.更新緩存過期時間 redis.call('PEXPIRE',key,expireTime) --5.刪除最大時間限度之前的數據,防止數據過多 redis.call('ZREMRANGEBYSCORE',key,0,currentTime-expireTime) --6.redis中添加當前時間(解決多個線程在同一毫秒添加相同value導致Redis漏記的問題) --6.1maxRetries最大重試次數retries重試次數 localmaxRetries=5 localretries=0 whiletruedo localresult=redis.call('ZADD',key,currentTime,currentTime) ifresult==1then --6.2添加成功則跳出循環 break else --6.3未添加成功則value+1再次進行嘗試 retries=retries+1 ifretries>=maxRetriesthen --6.4超過最大嘗試次數采用添加隨機數策略 localrandom_value=math.random(1,1000) currentTime=currentTime+random_value else currentTime=currentTime+1 end end end returnfalse
編寫 AOP 攔截
@Autowired privateRedisTemplateredisTemplate; @Autowired privateRedisScript limitScript; /** *限流 *XXX對限流要求比較高,可以使用在Redis中對規則進行存儲校驗或者使用中間件 * *@paramjoinPointjoinPoint *@paramrateLimiter限流注解 */ @Before(value="@annotation(rateLimiter)") publicvoidboBefore(JoinPointjoinPoint,RateLimiterrateLimiter){ //1.生成key Stringkey=getCombineKey(rateLimiter,joinPoint); try{ //2.執行腳本返回是否限流 Booleanflag=redisTemplate.execute(limitScript, ListUtil.of(key,String.valueOf(System.currentTimeMillis())), (Object[])getRules(rateLimiter)); //3.判斷是否限流 if(Boolean.TRUE.equals(flag)){ log.error("ip:'{}'攔截到一個請求RedisKey:'{}'", IpUtil.getIpAddr(((ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest()), key); thrownewServiceException(rateLimiter.message()); } }catch(ServiceExceptione){ throwe; }catch(Exceptione){ e.printStackTrace(); } } /** *獲取規則 * *@paramrateLimiter獲取其中規則信息 *@return */ privateLong[]getRules(RateLimiterrateLimiter){ intcapacity=rateLimiter.rules().length<1; ????//?1.?構建?args ????Long[]?args?=?new?Long[rateLimiter.preventDuplicate()???capacity?+?2?:?capacity]; ????//?3.?記錄數組元素 ????int?index?=?0; ????//?2.?判斷是否需要添加防重復提交到redis進行校驗 ????if?(rateLimiter.preventDuplicate())?{ ????????RateRule?preventRateRule?=?rateLimiter.preventDuplicateRule(); ????????args[index++]?=?preventRateRule.count(); ????????args[index++]?=?preventRateRule.timeUnit().toMillis(preventRateRule.time()); ????} ????RateRule[]?rules?=?rateLimiter.rules(); ????for?(RateRule?rule?:?rules)?{ ????????args[index++]?=?rule.count(); ????????args[index++]?=?rule.timeUnit().toMillis(rule.time()); ????} ????return?args; }
審核編輯:劉清
-
lua腳本
+關注
關注
0文章
21瀏覽量
7608 -
Redis
+關注
關注
0文章
378瀏覽量
10936
原文標題:Redis 多規則限流和防重復提交方案實現
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論