springboot+redis限流

本文介绍了如何在Spring Boot应用中配置Redis作为缓存并实现接口访问的限流,包括Redis连接池设置、Jackson序列化及Lettuce连接工厂。重点讲解了如何使用`RequestLimit`注解和`RequestLimitInterceptor`来限制指定接口的访问频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

redis yaml配置

redis:
  host: 1.117.89.11
  port: 6378
  password: Terry123456.
  timeout: 60s
  database: 2
  lettuce:
    pool:
      # 连接池中的最小空闲连接
      min-idle: 0
      # 连接池中的最大空闲连接
      max-idle: 8
      # 连接池的最大数据库连接数
      max-active: 8
      # #连接池最大阻塞等待时间(使用负值表示没有限制)
      max-wait: -1ms
    #spring cache 配置
    cache:
      type: redis
      redis:
        #        半小时
        time-to-live: 1800000 #毫秒

redisConfig配置:

package com.wlient.springboot_redis.Configuar;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();

        template.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);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式

        template.setKeySerializer(stringRedisSerializer);

        // hash的key也采用String的序列化方式

        template.setHashKeySerializer(stringRedisSerializer);

        // value序列化方式采用jackson

        template.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的value序列化方式采用jackson

        template.setHashValueSerializer(jackson2JsonRedisSerializer);


        template.afterPropertiesSet();

        return template;

    }
}

WebConfig配置文件:

package com.wlient.springboot_redis.Configuar;


import com.wlient.springboot_redis.common.Request.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;


@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private RequestLimitInterceptor requestLimitInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestLimitInterceptor)
                //拦截所有请求路径
                .addPathPatterns("/**")
                //再设置 放开哪些路径
                .excludePathPatterns("/static/**","/auth/login");
    }

}

自定义注解RequestLimit:

package com.wlient.springboot_redis.Interface;

import java.lang.annotation.*;

@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
package com.wlient.springboot_redis.common.Request;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wlient.springboot_redis.Interface.RequestLimit;
import com.wlient.springboot_redis.utils.IpUtil;
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;

@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={} 对接口:{} 已经访问的次数:{}",ipAddr, 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.info("请求过于频繁请稍后再试");
                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;
    }

}

接口测试:

@GetMapping("/test")
    @RequestLimit(maxCount = 3,second = 60)
    public String test() {
        return "你好,如果对你有帮助,请点赞加关注。";
    }

运行结果:

springboot-redis- 2021-08-17 15:25:37 [http-nio-8099-exec-1] INFO 
                c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:null
springboot-redis- 2021-08-17 15:25:39 [http-nio-8099-exec-2] INFO 
                c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:1
springboot-redis- 2021-08-17 15:25:40 [http-nio-8099-exec-5] INFO 
                c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:2
springboot-redis- 2021-08-17 15:25:41 [http-nio-8099-exec-3] INFO 
                c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:3
springboot-redis- 2021-08-17 15:25:41 [http-nio-8099-exec-3] INFO 
                c.w.s.c.R.RequestLimitInterceptor - 请求过于频繁请稍后再试
Spring Boot 和 Redis 结合可以很方便地实现应用的流量控制(限流)。Redis 作为缓存数据库,常用于实现分布式限流,因为它支持高并发操作且数据持久化。以下是使用 Spring Boot 和 Redis 进行限流的一种常见做法: 1. 添加依赖:首先,在你的`pom.xml`文件中添加 Spring Data Redis 和 Spring Cloud Config 或者 Spring Boot Actuator 的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth-zipkin</artifactId> </dependency> ``` 2. 配置Redis:在`application.properties`或`application.yml`中配置Redis连接信息。 ```properties spring.redis.host=localhost spring.redis.port=6379 ``` 3. 使用RateLimiter:Spring Cloud Sleuth 提供了 RateLimiter 接口,你可以创建一个 RedisRateLimiter 实现限流策略。例如: ```java @Autowired private RateLimiter rateLimiter; @GetMapping("/limited-endpoint") public ResponseEntity<String> limitedEndpoint() { // 检查请求是否超过限制 if (!rateLimiter.tryAcquire()) { return new ResponseEntity<>(HttpStatus.TOO_MANY_REQUESTS); } // 如果获取成功,继续处理业务逻辑 // ... return ResponseEntity.ok("Your request is processed"); } ``` 4. 系统监控:通过 Actuator 接口可以查看实时的限流统计,如每秒访问次数、令牌桶状态等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值