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:

https://www.jb51.net/article/218249.htm

RedisUtil :

https://www.jb51.net/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限流的實例的文章就介紹到這瞭,更多相關Springboot redis接口Api限流內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: