基于SpringBoot+Redis实现接口限流

前言

业务中需要对一些接口进行限流处理,防止机器人调用或者保证服务质量;

实现方式

  • 基于redis的lua脚本

引入依赖

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

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

redis配置

package com.qiangesoft.wechat.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 *
 * @author qiangesoft
 * @date 2024-03-19
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

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

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public DefaultRedisScript<Long> limitScript() {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(limitScriptText());
        redisScript.setResultType(Long.class);
        return redisScript;
    }

    /**
     * 限流脚本
     */
    private String limitScriptText() {
        return "local key = KEYS[1]\n" +
                "local count = tonumber(ARGV[1])\n" +
                "local time = tonumber(ARGV[2])\n" +
                "local current = redis.call('get', key);\n" +
                "if current and tonumber(current) > count then\n" +
                "    return tonumber(current);\n" +
                "end\n" +
                "current = redis.call('incr', key)\n" +
                "if tonumber(current) == 1 then\n" +
                "    redis.call('expire', key, time)\n" +
                "end\n" +
                "return tonumber(current);";
    }
}

限流注解

package com.qiangesoft.wechat.config;

import java.lang.annotation.*;

/**
 * 限流注解
 *
 * @author qiangesoft
 * @date 2024-03-19
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter {

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

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

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

}

限流实现

package com.qiangesoft.wechat.config;

import com.qiangesoft.wechat.utils.IpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

/**
 * 限流处理
 *
 * @author qiangesoft
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Slf4j
@Aspect
@Component
public class RateLimiterAspect {

    private final RedisTemplate<Object, Object> redisTemplate;

    private final RedisScript<Long> limitScript;

    @Before("@annotation(rateLimiter)")
    public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
        int time = rateLimiter.time();
        int count = rateLimiter.count();

        String key = this.buildKey(rateLimiter, point);
        List<Object> keys = Collections.singletonList(key);
        try {
            Long number = redisTemplate.execute(limitScript, keys, count, time);
            if (number == null || number.intValue() > count) {
                throw new RuntimeException("访问过于频繁,请稍候再试");
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("服务器限流异常,请稍候再试");
        }
    }

    /**
     * 缓存key
     *
     * @param rateLimiter
     * @param point
     * @return
     */
    public String buildKey(RateLimiter rateLimiter, JoinPoint point) {
        String limitId = "";
        if (rateLimiter.limitType() == LimitType.IP) {
            limitId = IpUtil.getIpAddr();
        }
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Class<?> targetClass = method.getDeclaringClass();
        String key = "rate_limit:" + limitId + "-" + targetClass.getName() + "-" + method.getName();
        return key;
    }
}

测试代码

package com.qiangesoft.wechat.controller;

import com.qiangesoft.wechat.config.RateLimiter;
import com.qiangesoft.wechat.utils.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试接口
 *
 * @author qiangesoft
 * @date 2024-03-19
 */
@RestController
public class TestController {

    @RateLimiter
    @GetMapping("/test")
    public ResultVO test() {
        return ResultVO.ok("调用成功!");
    }

}

在这里插入图片描述
继续频繁点击
在这里插入图片描述

相关推荐

  1. Resilience4j 实现接口

    2024-03-20 05:08:12       25 阅读
  2. SpringBoot整合resilience4j实现接口

    2024-03-20 05:08:12       61 阅读
  3. Redis+Lua脚本+SpringAOP实现接口

    2024-03-20 05:08:12       42 阅读
  4. SpringBoot + Redis 实现接口,一个注解的事

    2024-03-20 05:08:12       35 阅读
  5. Redis实现

    2024-03-20 05:08:12       58 阅读
  6. Redisson实现

    2024-03-20 05:08:12       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 05:08:12       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 05:08:12       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 05:08:12       82 阅读
  4. Python语言-面向对象

    2024-03-20 05:08:12       91 阅读

热门阅读

  1. 12350安全生产举报热线系统解决方案

    2024-03-20 05:08:12       34 阅读
  2. 字节-安全研究实习生(二面)

    2024-03-20 05:08:12       30 阅读
  3. 掌握C#中的GUI多线程技巧:WinForms和WPF实例详解

    2024-03-20 05:08:12       37 阅读
  4. OpenAI的ChatGPT应对大学会计考试

    2024-03-20 05:08:12       48 阅读
  5. ppt插件构思

    2024-03-20 05:08:12       36 阅读
  6. 富格林:理智做单增加出金盈利

    2024-03-20 05:08:12       37 阅读
  7. AI智能机器人的安装方法搭建电销机器人

    2024-03-20 05:08:12       38 阅读
  8. 共享旅游卡与我们当下的生活关联

    2024-03-20 05:08:12       39 阅读