使用Redis完成接口限流

接口限流

在一个高并发系统中对流量的把控是非常重要的,当巨大的流量直接请求到我们的服务器上没多久就可能造成接口不可用,不处理的话甚至会造成整个应用不可用。为了避免这种情况的发生我们就需要在请求接口时对接口进行限流的操作。

怎么做?

基于springboot而言,我们想到的是通过redis的自加:incr来实现。我们可以通过用户的唯一标识来设计成redis的key,值为单位时间内用户的请求次数。


一、准备工作

创建Spring Boot 工程,引入 Web 和 Redis 依赖,同时考虑到接口限流一般是通过注解来标记,而注解是通过 AOP 来解析的,所以我们还需要加上 AOP 的依赖:

<!-- 需要的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

提前准备好一个redis示例,并在项目中进行配置。

#具体配置以实际为主这里只是演示
spring:  
	redis:
		host: localhost
		port: 6379
		password: 123  

二、创建限流注解

限流我们一般分为两种情况:
1、针对某一个接口单位时间内指定允许访问次数,例如:A接口1分钟内允许访问100次;
2、针对ip地址进行限流,例如:ip地址A可以在1分钟内访问接口50次;

针对这两种情况我们定义一个枚举类:

public enum LimitType {

    /**
     * 默认策略
     */
    DEFAULT,

    /**
     * 根据IP进行限流
     */
    IP
}

接下来定义限流注解:

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimiter {

    /**
     * 限流key
     */
    String key() default "rate_limit:";

    /**
     * 限流时间,单位秒
     */
    int time() default 60;

    /**
     * 限流次数
     */
    int count() default 50;

    /**
     * 限流类型
     */
    LimitType limitType() default LimitType.DEFAULT;
}

第一个参数 key 是一个前缀,实际使用过程中是这个前缀加上接口方法的完整路径共同来组成一个 key 来存到redis中。使用时在需要进行限流的接口中加上注解并配置详细的参数即可。

三、定制RedisTemplate

在实际使用过程中我们通常是通过RedisTemplate来操作redis的,所以这里就需要定制我们需要的RedisTemplate,默认的RedisTemplate中是有一下小问题的,就是直接使用JdkSerializationRedisSerializer这个工具进行序列化时存放到redis中的key和value是会多一些前缀的,这样就会导致我们在读取数据时可能会出现错误。

修改 RedisTemplate 序列化方案,代码如下:

	@Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        // 值采用json序列化
        template.setValueSerializer(jacksonSeial);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        return template;
    }

四、开发lua脚本

我们在java 代码中将 Lua 脚本定义好,然后发送到 Redis 服务端去执行。我们在 resources 目录下新建 lua 文件夹专门用来存放 lua 脚本,脚本内容如下:

local key = KEYS[1]
local count = tonumber(ARGV[1])
local time = tonumber(ARGV[2])
local current = redis.call('get', key)
if current and tonumber(current) > count then
    return tonumber(current)
end
current = redis.call('incr', key)
if tonumber(current) == 1 then
    redis.call('expire', key, time)
end
return tonumber(current)

KEYSARGV 都是一会调用时候传进来的参数,tonumber 就是把字符串转为数字,redis.call 就是执行具体的 redis 指令。具体的流程:

  • 首先获取到传进来的 key 以及 限流的 count 和时间 time。
  • 通过 get 获取到这个 key 对应的值,这个值就是当前时间段内这个接口访问了多少次。
  • 如果是第一次访问,此时拿到的结果为 nil,否则拿到的结果应该是一个数字,所以接下来就判断,如果拿到的结果是一个数字,并且这个数字还大于 count,那就说明已经超过流量限制了,那么直接返回查询的结果即可。
  • 如果拿到的结果为 nil,说明是第一次访问,此时就给当前 key 自增 1,然后设置一个过期时间。
  • 最后把自增 1 后的值返回就可以了。

接下来写一个Bean来加载这个脚本:

	@Bean
	public DefaultRedisScript<Long> limitScript() {
		DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
		redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/limit.lua")));
		redisScript.setResultType(Long.class);
		return redisScript;
	}

五、解析注解

自定义切面解析注解:

@Slf4j
@Aspect
@Component
public class RateLimiterAspect {

    private final RedisTemplate redisTemplate;
    private final RedisScript<Long> limitScript;

    public RateLimiterAspect(RedisTemplate redisTemplate, RedisScript<Long> limitScript) {
        this.redisTemplate = redisTemplate;
        this.limitScript = limitScript;
    }

    @Around("@annotation(com.example.demo.annotation.RateLimiter)")
    public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable{
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        RateLimiter rateLimiter = methodSignature.getMethod().getAnnotation(RateLimiter.class);
        
		//判断该方法是否存在限流的注解
        if (null != rateLimiter){
        	//获得注解中的配置信息
            int count = rateLimiter.count();
            int time = rateLimiter.time();
            String key = rateLimiter.key();
			
			//调用getCombineKey()获得存入redis中的key   key -> 注解中配置的key前缀-ip地址-方法路径-方法名
            String combineKey = getCombineKey(rateLimiter, methodSignature);
            log.info("combineKey->,{}",combineKey);
            //将combineKey放入集合
            List<Object> keys = Collections.singletonList(combineKey);
            log.info("keys->",keys);
            try {
            	//执行lua脚本获得返回值
                Long number = (Long) redisTemplate.execute(limitScript, keys, count, time);
                //如果返回null或者返回次数大于配置次数,则限制访问
                if (number==null || number.intValue() > count) {
                    throw new ServiceException("访问过于频繁,请稍候再试");
                }
                log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), combineKey);
            } catch (ServiceException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException("服务器限流异常,请稍候再试");
            }
        }

        return joinPoint.proceed();
    }

	/**
     * Gets combine key.
     *
     * @param rateLimiter the rate limiter
     * @param signature   the signature
     * @return the combine key
     */
    public String getCombineKey(RateLimiter rateLimiter, MethodSignature signature) {
        StringBuilder stringBuffer = new StringBuilder(rateLimiter.key());
        if (rateLimiter.limitType() == LimitType.IP) {
            stringBuffer.append(RequestUtil.getIpAddr(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest())).append("-");
        }
        Method method = signature.getMethod();
        Class<?> targetClass = method.getDeclaringClass();
        stringBuffer.append(targetClass.getName()).append("-").append(method.getName());
        return stringBuffer.toString();
    }


}

六、自定义异常处理

由于访问次数达到限制时是抛异常出来,所以我们还需要写一个全局异常捕获:

/**
 * 自定义ServiceException
 */
public class ServiceException extends Exception{

    public ServiceException(){
        super();
    }

    public ServiceException(String msg){
        super(msg);
    }
}

/**
 * 异常捕获处理
 */
 @RestControllerAdvice
public class GlobalExceptionAdvice {
	
	@ExceptionHandler(ServiceException.class)
    public Result<Object> serviceException(ServiceException e) {
    	//Result.failure()是我们在些项目是自定义的统一返回
        return Result.failure(e.getMessage());
    }

}
    

七、测试结果

测试代码:

	@GetMapping("/strategy")
    @RateLimiter(time = 3,count = 1,limitType = LimitType.IP)
    public String strategyTest(){
        return "test";
    }

当访问次数大于配置的限制时限制接口调用

异常结果

正常结果

正长结果
以上就是使用Redis完成接口限流的过程,感谢观看!

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Spring Boot结合Redis实现接口限的步骤如下[^1][^2]: 1. 引入依赖:在Spring Boot项目的pom.xml文件中添加Redis和AOP的相关依赖。 2. 配置RedisTemplate:在Spring Boot的配置文件中配置Redis连接信息,包括主机名、端口号、密码等。 3. 创建自定义注解:使用@RateLimiter注解来标记需要进行接口限流的方法。 4. 编写切面类:创建一个切面类,使用@Aspect注解标记,并在该类中编写切点和通知方法。 5. 实现接口限流逻辑:在通知方法中,使用Redis的原子操作来实现接口限流的逻辑。可以使用Redis的incr命令来对接口的访问次数进行计数,然后根据设定的阈值来判断是否限流。 6. 配置切面:在Spring Boot的配置类中,使用@EnableAspectJAutoProxy注解开启AOP功能,并将切面类添加到容器中。 7. 在需要进行接口限流的方法上添加注解:在需要进行接口限流的方法上添加@RateLimiter注解,并配置相关参数,如限流的阈值、时间窗口大小等。 8. 测试接口限流效果:启动Spring Boot应用程序,并访问被限流接口,观察接口的访问频率是否受到限制。 以下是一个示例代码,演示了如何使用Spring Boot和Redis实现接口限流: ```java // 1. 创建自定义注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RateLimiter { int value() default 10; // 默认限流阈值为10 int window() default 60; // 默认时间窗口为60秒 } // 2. 编写切面类 @Aspect @Component public class RateLimiterAspect { @Autowired private RedisTemplate<String, String> redisTemplate; @Around("@annotation(rateLimiter)") public Object around(ProceedingJoinPoint joinPoint, RateLimiter rateLimiter) throws Throwable { String methodName = joinPoint.getSignature().getName(); String key = "rate_limiter:" + methodName; int limit = rateLimiter.value(); int window = rateLimiter.window(); // 使用Redis的incr命令对接口的访问次数进行计数 Long count = redisTemplate.opsForValue().increment(key, 1); if (count == 1) { // 设置过期时间,保证计数器在一定时间后自动清零 redisTemplate.expire(key, window, TimeUnit.SECONDS); } if (count > limit) { // 超过限流阈值,抛出异常或返回错误信息 throw new RuntimeException("接口访问频率超过限制"); } // 执行原方法 return joinPoint.proceed(); } } // 3. 在需要进行接口限流的方法上添加注解 @RestController public class DemoController { @RateLimiter(value = 5, window = 60) // 每分钟最多访问5次 @GetMapping("/demo") public String demo() { return "Hello World!"; } } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值