您好,登錄后才能下訂單哦!
這篇文章主要介紹“Springboot使用redis實現接口Api限流的方法”,在日常操作中,相信很多人在Springboot使用redis實現接口Api限流的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Springboot使用redis實現接口Api限流的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
該篇介紹的內容如題,就是利用redis實現接口的限流( 某時間范圍內 最大的訪問次數 ) 。
慣例,先看下我們的實戰目錄結構:
首先是pom.xml 核心依賴:
<!--用于redis數據庫連接--> <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: #連接池最大連接數 使用負值代表無限制 默認為8 max-active: 10 #最大空閑連接 默認8 max-idle: 10 #最小空閑連接 默認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年的,因為這些redis的整合教程,在這個系列里面一共有快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); //序列化設置 ,這樣為了存儲操作對象時正常顯示的數據,也能正常存儲和獲取 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 { /** * 時間內 秒為單位 */ int second() default 10; /** * 允許訪問次數 */ int maxCount() default 5; //默認效果 : 10秒內 對于使用該注解的接口,只能總請求訪問數 不能大于 5次 }
接下來是攔截器 RequestLimitInterceptor.java:
攔截接口的方式 是通過 ip地址+接口url ,做時間內的訪問計數
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; } //限制的時間范圍 int seconds = requestLimit.second(); //時間內的 最大次數 int maxCount = requestLimit.maxCount(); String ipAddr = IpUtil.getIpAddr(request); // 存儲key String key = ipAddr+":"+request.getContextPath() + ":" + request.getServletPath(); // 已經訪問的次數 Integer count = (Integer) redisTemplate.opsForValue().get(key); log.info("檢測到目前ip對接口={}已經訪問的次數", 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(); //這里傳提示語可以改成自己項目的返回數據封裝的類 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("/**") //再設置 放開哪些路徑 .excludePathPatterns("/static/**","/auth/login"); } }
最后還有兩個工具類
IpUtil:
http://www.neiyidaogou.com/article/218249.htm
RedisUtil :
http://www.neiyidaogou.com/article/218246.htm
最后寫個測試接口
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 "你好,如果對你有幫助,請點贊加關注。"; } }
這個/test接口的注解,我們設置的是 60秒內 最大訪問次數為 3次 (實際應用應該是根據具體接口做相關的次數限制。)
然后使用postman測試一下接口:
前面三次都是請求通過的:
第四次:
到此,關于“Springboot使用redis實現接口Api限流的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。