日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

基于redis+lua進(jìn)行限流

作者:枯楓葉 更新時(shí)間: 2022-08-05 編程語(yǔ)言

1,首先我們r(jià)edis有很多限流的算法(比如:令牌桶,計(jì)數(shù)器,時(shí)間窗口)等,但是都有一定的缺點(diǎn),令牌桶在單項(xiàng)目中相對(duì)來(lái)說(shuō)比較穩(wěn)定,但是在分布式集群里面缺顯的不那么友好,這時(shí)候,在分布式里面進(jìn)行限流的話,我們則可以使用redis+lua腳本進(jìn)行限流,能抗住億級(jí)并發(fā)

2,下面說(shuō)說(shuō)lua+redis進(jìn)行限流的做法
開(kāi)發(fā)環(huán)境:idea+redis+lua
第一:
打開(kāi)idea的插件市場(chǎng),然后搜索lua,點(diǎn)擊右邊的安裝,然后安裝好了,重啟即可在這里插入圖片描述
第二:寫(xiě)一個(gè)自定義限流注解

package com.sport.sportcloudmarathonh5.config;

import java.lang.annotation.*;

/**
 * @author zdj
 * @version 1.0.0
 * @description 自定義注解實(shí)現(xiàn)分布式限流
 */
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisLimitStream {
    /**
     * 請(qǐng)求限制,一秒內(nèi)可以允許好多個(gè)進(jìn)入(默認(rèn)一秒可以支持100個(gè))
     * @return
     */
    int reqLimit() default 1000;

    /**
     * 模塊名稱
     * @return
     */
    String reqName() default "";
}

第三:在指定的方法上面添加該注解

/**
     * 壓測(cè)接口
     * @return
     */
    @Login(isLogin = false)
    @RedisLimitStream(reqName = "名額秒殺", reqLimit = 1000)
    @ApiOperation(value = "壓測(cè)接口", notes = "壓測(cè)接口", httpMethod = "GET")
    @RequestMapping(value = "/pressure", method = RequestMethod.GET)
    public ResultVO<Object> pressure(){
        return ResultVO.success("搶購(gòu)成功!");
    }

第四:添加一個(gè)攔截器對(duì)訪問(wèn)的方法在訪問(wèn)之前進(jìn)行攔截:

package com.sport.sportcloudmarathonh5.config;

import com.alibaba.fastjson.JSONObject;
import com.sport.sportcloudmarathonh5.service.impl.RedisService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zdj
 * @version 1.0.0
 * @description MyRedisLimiter注解的切面類(lèi)
 */
@Aspect
@Component
public class RedisLimiterAspect {
    private final Logger logger = LoggerFactory.getLogger(RedisLimitStream.class);
    /**
     * 當(dāng)前響應(yīng)請(qǐng)求
     */
    @Autowired
    private HttpServletResponse response;

    /**
     * redis服務(wù)
     */
    @Autowired
    private RedisService redisService;

    /**
     * 執(zhí)行redis的腳本文件
     */
    @Autowired
    private RedisScript<Boolean> rateLimitLua;

    /**
     * 對(duì)所有接口進(jìn)行攔截
     */
    @Pointcut("execution(public * com.sport.sportcloudmarathonh5.controller.*.*(..))")
    public void pointcut(){}

    /**
     * 對(duì)切點(diǎn)進(jìn)行繼續(xù)處理
     */
    @Around("pointcut()")
    public Object process(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        //使用反射獲取RedisLimitStream注解
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        //沒(méi)有添加限流注解的方法直接放行
        RedisLimitStream redisLimitStream = signature.getMethod().getDeclaredAnnotation(RedisLimitStream.class);
        if(ObjectUtils.isEmpty(redisLimitStream)){
            return proceedingJoinPoint.proceed();
        }

        //List設(shè)置Lua的KEYS[1]
        List<String> keyList = new ArrayList<>();
        keyList.add("ip:" + (System.currentTimeMillis() / 1000));

        //獲取注解上的參數(shù),獲取配置的速率
        //List設(shè)置Lua的ARGV[1]
        int value = redisLimitStream.reqLimit();

        // 調(diào)用Redis執(zhí)行l(wèi)ua腳本,未拿到令牌的,直接返回提示
        boolean acquired = redisService.execute(rateLimitLua, keyList, value);
        logger.info("執(zhí)行l(wèi)ua結(jié)果:" + acquired);
        if(!acquired){
            this.limitStreamBackMsg();
            return null;
        }

        //獲取到令牌,繼續(xù)向下執(zhí)行
        return proceedingJoinPoint.proceed();
    }

    /**
     * 被攔截的人,提示消息
     */
    private void limitStreamBackMsg() {
        response.setHeader("Content-Type", "text/html;charset=UTF8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.println("{\"code\":503,\"message\":\"當(dāng)前排隊(duì)人較多,請(qǐng)稍后再試!\",\"data\":\"null\"}");
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}

第五:寫(xiě)個(gè)配置類(lèi),在啟動(dòng)的時(shí)候?qū)⑽覀兊膌ua腳本代碼加載到redisscript中

package com.sport.sportcloudmarathonh5.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;

/**
 * @author zdj
 * @version 1.0.0
 * @description 實(shí)現(xiàn)redis的編碼方式
 */
@Configuration
public class RedisConfiguration {

    /**
     * 初始化將lua腳本加載到redis腳本中
     * @return
     */
    @Bean
    public DefaultRedisScript loadRedisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript();
        redisScript.setLocation(new ClassPathResource("limit.lua"));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }
}

第六:redis執(zhí)行l(wèi)ua的方法

  /**
     * 執(zhí)行l(wèi)ua腳本
     * @param redisScript lua源代碼腳本
     * @param keyList
     * @param value
     * @return
     */
    public boolean execute(RedisScript<Boolean> redisScript, List<String> keyList, int value) {
        return redisTemplate.execute(redisScript, keyList, String.valueOf(value));
    }

第七:在resources目錄下面新加一個(gè)lua腳本文件,將下面代碼拷貝進(jìn)去即可:

local key = KEYS[1] --限流KEY(一秒一個(gè))
local limit = tonumber(ARGV[1]) --限流大小
local current = tonumber(redis.call('get', key) or "0")
if current + 1 > limit then --如果超出限流大小
    return false
else --請(qǐng)求數(shù)+1,并設(shè)置2秒過(guò)期
    redis.call("INCRBY", key, "1")
    redis.call("expire", key, "2")
end
return true

在這里插入圖片描述
最后執(zhí)行即可:
可以使用jemster進(jìn)行測(cè)試:
在這里插入圖片描述

原文鏈接:https://blog.csdn.net/gelinwangzi_juge/article/details/125919151

欄目分類(lèi)
最近更新