Springboot使用redis進(jìn)行api防刷限流過(guò)程詳解
這篇文章主要介紹了Springboot使用redis進(jìn)行api防刷限流過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
api限流的場(chǎng)景
限流的需求出現(xiàn)在許多常見的場(chǎng)景中
- 秒殺活動(dòng),有人使用軟件惡意刷單搶貨,需要限流防止機(jī)器參與活動(dòng)
- 某api被各式各樣系統(tǒng)廣泛調(diào)用,嚴(yán)重消耗網(wǎng)絡(luò)、內(nèi)存等資源,需要合理限流
- 淘寶獲取ip所在城市接口、微信公眾號(hào)識(shí)別微信用戶等開發(fā)接口,免費(fèi)提供給用戶時(shí)需要限流,更具有實(shí)時(shí)性和準(zhǔn)確性的接口需要付費(fèi)。
api限流實(shí)戰(zhàn)
首先我們編寫注解類AccessLimit,使用注解方式在方法上限流更優(yōu)雅更方便!三個(gè)參數(shù)分別代表有效時(shí)間、最大訪問(wèn)次數(shù)、是否需要登錄,可以理解為 seconds 內(nèi)最多訪問(wèn) 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; }
限流的思路
- 通過(guò)路徑:ip的作為key,訪問(wèn)次數(shù)為value的方式對(duì)某一用戶的某一請(qǐng)求進(jìn)行唯一標(biāo)識(shí)
- 每次訪問(wèn)的時(shí)候判斷key是否存在,是否count超過(guò)了限制的訪問(wèn)次數(shù)
- 若訪問(wèn)超出限制,則應(yīng)response返回msg:請(qǐng)求過(guò)于頻繁給前端予以展示
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 請(qǐng)求過(guò)于頻繁請(qǐng)稍后再試 return false; } } return true; } }
注冊(cè)攔截器并配置攔截路徑和不攔截路徑
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("/不被攔截路徑 通常為登錄注冊(cè)或者首頁(yè)"); } }
在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 ""; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mac系統(tǒng)搭建JDK及JMETER過(guò)程解析
這篇文章主要介紹了Mac系統(tǒng)搭建JDK及JMETER過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot+Hibernate實(shí)現(xiàn)自定義數(shù)據(jù)驗(yàn)證及異常處理
這篇文章主要為大家介紹了SpringBoot如何整合Hibernate自定義數(shù)據(jù)驗(yàn)證及多種方式異常處理,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-04-04Java實(shí)現(xiàn)合并word文檔的示例代碼
在做項(xiàng)目中,經(jīng)常會(huì)遇到一種情況,需要將一個(gè)小word文檔的內(nèi)容插入到一個(gè)大word(主文檔)中。本文就為大家準(zhǔn)備了Java實(shí)現(xiàn)合并word文檔的方法,需要的可以參考一下2022-08-08AOP之事務(wù)管理<aop:advisor>的兩種配置方式
這篇文章主要介紹了AOP之事務(wù)管理<aop:advisor>的兩種配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java數(shù)據(jù)結(jié)構(gòu)之鏈表相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)關(guān)于Java數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),文章圍繞Java鏈表展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06淺談java中的一維數(shù)組、二維數(shù)組、三維數(shù)組、多維數(shù)組
下面小編就為大家?guī)?lái)一篇淺談java中的一維數(shù)組、二維數(shù)組、三維數(shù)組、多維數(shù)組。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05