Springboot使用redis實(shí)現(xiàn)接口Api限流的實(shí)例
前言
該篇介紹的內(nèi)容如題,就是利用redis實(shí)現(xiàn)接口的限流( 某時(shí)間范圍內(nèi) 最大的訪問次數(shù) ) 。
正文
慣例,先看下我們的實(shí)戰(zhàn)目錄結(jié)構(gòu):
首先是pom.xml 核心依賴:
<!--用于redis數(shù)據(jù)庫連接--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--用于redis lettuce 連接池pool使用--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
然后是application.yml里面的redis接入配置:
spring: redis: lettuce: pool: #連接池最大連接數(shù) 使用負(fù)值代表無限制 默認(rèn)為8 max-active: 10 #最大空閑連接 默認(rèn)8 max-idle: 10 #最小空閑連接 默認(rèn)0 min-idle: 1 host: 127.0.0.1 password: 123456 port: 6379 database: 0 timeout: 2000ms server: port: 8710
redis的配置類, RedisConfig.java:
ps:可以看到日期是18年的,因?yàn)檫@些redis的整合教程,在這個(gè)系列里面一共有快10篇,不了解的看客如果感興趣可以去看一看。
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig; /** * @Author: JCccc * @CreateTime: 2018-09-11 * @Description: */ @Configuration @EnableCaching public class RedisConfig { @Bean public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheConfiguration cacheConfiguration = defaultCacheConfig() .disableCachingNullValues() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Object.class))); return RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build(); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //序列化設(shè)置 ,這樣為了存儲(chǔ)操作對象時(shí)正常顯示的數(shù)據(jù),也能正常存儲(chǔ)和獲取 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(factory); return stringRedisTemplate; } }
自定義注解:
import java.lang.annotation.*; /** * @Author JCccc * @Description * @Date 2021/7/23 11:46 */ @Inherited @Documented @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface RequestLimit { /** * 時(shí)間內(nèi) 秒為單位 */ int second() default 10; /** * 允許訪問次數(shù) */ int maxCount() default 5; //默認(rèn)效果 : 10秒內(nèi) 對于使用該注解的接口,只能總請求訪問數(shù) 不能大于 5次 }
接下來是攔截器 RequestLimitInterceptor.java:
攔截接口的方式 是通過 ip地址+接口url ,做時(shí)間內(nèi)的訪問計(jì)數(shù)
import com.elegant.testdemo.annotation.RequestLimit; import com.elegant.testdemo.utils.IpUtil; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; 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; import java.io.IOException; import java.util.concurrent.TimeUnit; /** * @Author JCccc * @Description * @Date 2021/7/23 11:49 */ @Component public class RequestLimitInterceptor implements HandlerInterceptor { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; // 獲取RequestLimit注解 RequestLimit requestLimit = handlerMethod.getMethodAnnotation(RequestLimit.class); if (null==requestLimit) { return true; } //限制的時(shí)間范圍 int seconds = requestLimit.second(); //時(shí)間內(nèi)的 最大次數(shù) int maxCount = requestLimit.maxCount(); String ipAddr = IpUtil.getIpAddr(request); // 存儲(chǔ)key String key = ipAddr+":"+request.getContextPath() + ":" + request.getServletPath(); // 已經(jīng)訪問的次數(shù) Integer count = (Integer) redisTemplate.opsForValue().get(key); log.info("檢測到目前ip對接口={}已經(jīng)訪問的次數(shù)", request.getServletPath() , count); if (null == count || -1 == count) { redisTemplate.opsForValue().set(key, 1, seconds, TimeUnit.SECONDS); return true; } if (count < maxCount) { redisTemplate.opsForValue().increment(key); return true; } log.warn("請求過于頻繁請稍后再試"); returnData(response); return false; } return true; } catch (Exception e) { log.warn("請求過于頻繁請稍后再試"); e.printStackTrace(); } return true; } public void returnData(HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); ObjectMapper objectMapper = new ObjectMapper(); //這里傳提示語可以改成自己項(xiàng)目的返回?cái)?shù)據(jù)封裝的類 response.getWriter().println(objectMapper.writeValueAsString("請求過于頻繁請稍后再試")); return; } }
接下來是 攔截器的配置 WebConfig.java:
import com.elegant.testdemo.interceptor.RequestLimitInterceptor; 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; /** * @Author JCccc * @Description * @Date 2021/7/23 11:52 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private RequestLimitInterceptor requestLimitInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(requestLimitInterceptor) //攔截所有請求路徑 .addPathPatterns("/**") //再設(shè)置 放開哪些路徑 .excludePathPatterns("/static/**","/auth/login"); } }
最后還有兩個(gè)工具類
IpUtil:
http://www.dbjr.com.cn/article/218249.htm
RedisUtil :
http://www.dbjr.com.cn/article/218246.htm
最后寫個(gè)測試接口
TestController.java
import com.elegant.testdemo.annotation.RequestLimit; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author JCccc * @Description * @Date 2021/7/23 11:55 */ @RestController public class TestController { @GetMapping("/test") @RequestLimit(maxCount = 3,second = 60) public String test() { return "你好,如果對你有幫助,請點(diǎn)贊加關(guān)注。"; } }
這個(gè)/test接口的注解,我們設(shè)置的是 60秒內(nèi) 最大訪問次數(shù)為 3次 (實(shí)際應(yīng)用應(yīng)該是根據(jù)具體接口做相關(guān)的次數(shù)限制。)
然后使用postman測試一下接口:
前面三次都是請求通過的:
第四次:
到此這篇關(guān)于Springboot使用redis實(shí)現(xiàn)接口Api限流的實(shí)例的文章就介紹到這了,更多相關(guān)Springboot redis接口Api限流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載
這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08詳談jvm--Java中init和clinit的區(qū)別
下面小編就為大家?guī)硪黄斦刯vm--Java中init和clinit的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Spring Boot集成Thymeleaf模板引擎的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot集成Thymeleaf模板引擎的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02springboot+idea+maven 多模塊項(xiàng)目搭建的詳細(xì)過程(連接數(shù)據(jù)庫進(jìn)行測試)
這篇文章主要介紹了springboot+idea+maven 多模塊項(xiàng)目搭建的詳細(xì)過程(連接數(shù)據(jù)庫進(jìn)行測試),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Vue結(jié)合Springboot實(shí)現(xiàn)用戶列表單頁面(前后端分離)
本文主要介紹了Vue結(jié)合Springboot實(shí)現(xiàn)用戶列表單頁面,可以實(shí)現(xiàn)簡單的查詢,刪除,修改,和添加用戶信息功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07解決Idea運(yùn)行junit測試時(shí)報(bào)Error:[3,17]?程序包org.junit不存在的問題
這篇文章主要介紹了Idea運(yùn)行junit測試時(shí)報(bào)Error:[3,17]?程序包org.junit不存在解決方法,本文給大家分享兩種解決辦法,需要的朋友可以參考下2023-03-03