這篇文章主要介紹了Springboot使用redis進行api防刷限流過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
創(chuàng)新互聯(lián)公司是一家專業(yè)提供東乃企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站建設、成都做網(wǎng)站、html5、小程序制作等業(yè)務。10年已為東乃眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。
api限流的場景
限流的需求出現(xiàn)在許多常見的場景中
api限流實戰(zhàn)
首先我們編寫注解類AccessLimit,使用注解方式在方法上限流更優(yōu)雅更方便!三個參數(shù)分別代表有效時間、最大訪問次數(shù)、是否需要登錄,可以理解為 seconds 內(nèi)最多訪問 maxCount 次。
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface AccessLimit { int seconds(); int maxCount(); boolean needLogin() default true; }
限流的思路
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class AccessLimtInterceptor implements HandlerInterceptor { @Autowired private RedisService redisService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod hm = (HandlerMethod) handler; AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if (null == accessLimit) { return true; } int seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); boolean needLogin = accessLimit.needLogin(); if (needLogin) { //判斷是否登錄 } String key = request.getContextPath() + ":" + request.getServletPath() + ":" + ip ; Integer count = redisService.get(key); if (null == count || -1 == count) { redisService.set(key, 1); redisService.expire(seconds); return true; } if (count < maxCount) { redisService.inCr(key); return true; } if (count >= maxCount) { // response 返回 json 請求過于頻繁請稍后再試 return false; } } return true; } }
注冊攔截器并配置攔截路徑和不攔截路徑
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; // extends WebMvcConfigurerAdapter 已經(jīng)廢棄,java 8開始直接繼承就可以 @Configuration public class IntercepterConfig implements WebMvcConfigurer { @Autowired private AccessLimtInterceptor accessLimtInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(accessLimtInterceptor) .addPathPatterns("/攔截路徑") .excludePathPatterns("/不被攔截路徑 通常為登錄注冊或者首頁"); } }
在Controller層的方法上直接可以使用注解@AccessLimit
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("test") public class TestControler { @GetMapping("accessLimit") @AccessLimit(seconds = 3, maxCount = 10) public String testAccessLimit() { //xxxx return ""; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:Springboot使用redis進行api防刷限流過程詳解
標題路徑:http://muchs.cn/article6/ghgpog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、虛擬主機、移動網(wǎng)站建設、電子商務、商城網(wǎng)站、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)