接口限流處理

1.為什么要限流

當(dāng)我們設(shè)計(jì)接口時,需要考慮的因素有很多,其中例如如在設(shè)計(jì)獲取短信驗(yàn)證碼的接口時,第一個想到的就是,接口如何去實(shí)現(xiàn)訪問控制,好比如我只能讓你1分鐘之內(nèi)最多請求1次,或者其他規(guī)則,這樣很大程度上對接口起到一定的保護(hù)。防止對接口的惡意請求,減少不必要的資源浪費(fèi)。當(dāng)然并非所有接口都需要做這些限制,這也需要根據(jù)實(shí)際業(yè)務(wù)而定。

2.怎么限流

基于springboot而言,我們想到的是通過redis的自加:incr來實(shí)現(xiàn)。我們可以通過用戶的唯一標(biāo)識來設(shè)計(jì)成redis的key,值為單位時間內(nèi)用戶的請求次數(shù)。

3.實(shí)現(xiàn)

基于,是什么,為什么,怎么做三部曲,閑話不多說,直接上代碼。

/**
*  注解用戶訪問控制
 * 在 second 秒內(nèi),最大只能請求 maxCount 次
 * @author Json
 * @date 2022/3/9 19:33
 */
@Documented
@Inherited
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {
    /**
     * 通過redis 實(shí)現(xiàn)的 這里指定redisKey 否則默認(rèn)用戶id標(biāo)識 做實(shí)現(xiàn)
     */
    String redisKey();

    /**
     * 時間
     */
    int second() default 1;

    /**
     * 最大請求量
     */
    int maxCount() default 1;

    /**
     * 錯誤文案
     */
    String errorMsg();
}

再寫個攔截器

@Slf4j
@Component
public class RequestLimitIntercept implements HandlerInterceptor {
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        //HandlerMethod 封裝方法定義相關(guān)的信息,如類,方法,參數(shù)等
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // 獲取方法中是否包含注解
        RequestLimit methodAnnotation = method.getAnnotation(RequestLimit.class);
        //獲取 類中是否包含注解,也就是controller 是否有注解
        RequestLimit classAnnotation = method.getDeclaringClass().getAnnotation(RequestLimit.class);
        // 如果 方法上有注解就優(yōu)先選擇方法上的參數(shù),否則類上的參數(shù)
        RequestLimit requestLimit = methodAnnotation != null ? methodAnnotation : classAnnotation;
        if (requestLimit != null) {
            if (isLimit(request, requestLimit)) {
                Result<String> result = new ResultUtil<String>().setErrorMsg(requestLimit.errorMsg());
                response.getWriter().write(JSONObject.toJSON(result).toString());
                return false;
            }
        }

        return true;
    }

    /**
     * 判斷請求是否受限
     *
     * @param request      HttpServletRequest
     * @param requestLimit RequestLimit
     * @return true 允許  false 不允許
     */
    public boolean isLimit(HttpServletRequest request, RequestLimit requestLimit) {
        // 受限的redis 緩存key ,因?yàn)檫@里用瀏覽器做測試,我就用sessionid 來做唯一key,如果是app ,可以使用 用戶ID 之類的唯一標(biāo)識。
        String redisKey = requestLimit.redisKey();
        String limitKey;
        if (StringUtils.isNotBlank(redisKey)) {
            limitKey = redisKey;
        }else {
            limitKey = request.getServletPath() + request.getSession().getId();
        }
        // 從緩存中獲取,當(dāng)前這個請求訪問了幾次
        Object obj = redisTemplate.opsForValue().get(limitKey);
        if (obj == null) {
            //初始 次數(shù)
            redisTemplate.opsForValue().set(limitKey, "1", requestLimit.second(), TimeUnit.SECONDS);
        } else {
            if (Integer.parseInt(String.valueOf(obj)) >= requestLimit.maxCount()) {
                return true;
            }
            // 次數(shù)自增 +1
            redisTemplate.opsForValue().increment(limitKey);
        }
        return false;
    }

}

再接口上我們加上上面的注解,即可實(shí)現(xiàn)單位時間內(nèi)的訪問控制,當(dāng)然這里只說了大概的原理,可以自行根據(jù)業(yè)務(wù)去改造,當(dāng)然還有其他辦法去實(shí)現(xiàn)類似的效果,只是redis做起來會更方便,更好。

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

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