• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    redis+lua实现分布式限流的示例

    发布者: Error | 发布时间: 2025-6-19 12:38| 查看数: 112| 评论数: 0|帖子模式

    为什么使用redis+lua实现分布式限流


    • 原子性:通过Lua脚本执行限流逻辑,所有操作在一个原子上下文中完成,避免了多步操作导致的并发问题。
    • 灵活性:Lua脚本可以编写复杂的逻辑,比如滑动窗口限流,易于扩展和定制化。
    • 性能:由于所有逻辑在Redis服务器端执行,减少了网络往返,提高了执行效率。

    使用ZSET也可以实现限流,为什么选择lua的方式

    使用zset需要额度解决这些问题

    • 并发控制:需要额外的逻辑来保证操作的原子性和准确性,可能需要配合Lua脚本或Lua脚本+WATCH/MULTI/EXEC模式来实现。
    • 资源消耗:长期存储请求记录可能导致Redis占用更多的内存资源。
    为什么redis+zset不能保证原子性和准确性

    • 多步骤操作:滑动窗口限流通常需要执行多个步骤,比如检查当前窗口的请求次数、添加新的请求记录、可能还需要删除过期的请求记录等。这些操作如果分开执行,就有可能在多线程或多进程环境下出现不一致的情况。
    • 非原子性复合操作:虽然单个Redis命令是原子的,但当你需要执行一系列操作来维持限流状态时(例如,先检查计数、再增加计数、最后可能还要删除旧记录),没有一个单一的Redis命令能完成这些复合操作。如果在这系列操作之间有其他客户端修改了数据,就会导致限流不准确。
    • 竞争条件:在高并发环境下,多个客户端可能几乎同时执行限流检查和增加请求的操作,如果没有适当的同步机制,可能会导致请求计数错误。

    实现


    依赖
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.     <modelVersion>4.0.0</modelVersion>
    5.     <parent>
    6.         <groupId>org.springframework.boot</groupId>
    7.         <artifactId>spring-boot-starter-parent</artifactId>
    8.         <version>2.2.6.RELEASE</version>
    9.         <relativePath/> <!-- lookup parent from repository -->
    10.     </parent>
    11.     <groupId>com.kang</groupId>
    12.     <artifactId>rate-limiter-project</artifactId>
    13.     <version>0.0.1-SNAPSHOT</version>
    14.     <name>rate-limiter-project</name>
    15.     <description>rate-limiter-project</description>
    16.     <properties>
    17.         <java.version>8</java.version>
    18.     </properties>
    19.     <dependencies>
    20.         <dependency>
    21.             <groupId>org.springframework.boot</groupId>
    22.             <artifactId>spring-boot-starter-data-redis</artifactId>
    23.         </dependency>

    24.         <dependency>
    25.             <groupId>org.apache.commons</groupId>
    26.             <artifactId>commons-pool2</artifactId>
    27.             <version>2.6.2</version>
    28.         </dependency>

    29.         <dependency>
    30.             <groupId>com.google.guava</groupId>
    31.             <artifactId>guava</artifactId>
    32.             <version>31.0.1-jre</version> <!-- 请检查最新版本 -->
    33.         </dependency>

    34.         <dependency>
    35.             <groupId>org.apache.commons</groupId>
    36.             <artifactId>commons-lang3</artifactId>
    37.             <version>3.12.0</version>
    38.         </dependency>

    39.         <dependency>
    40.             <groupId>org.springframework.boot</groupId>
    41.             <artifactId>spring-boot-starter-web</artifactId>
    42.         </dependency>

    43.         <dependency>
    44.             <groupId>org.projectlombok</groupId>
    45.             <artifactId>lombok</artifactId>
    46.             <optional>true</optional>
    47.         </dependency>

    48.         <dependency>
    49.             <groupId>org.springframework.boot</groupId>
    50.             <artifactId>spring-boot-starter-test</artifactId>
    51.             <scope>test</scope>
    52.         </dependency>
    53.     </dependencies>

    54.     <build>
    55.         <plugins>
    56.             <plugin>
    57.                 <groupId>org.springframework.boot</groupId>
    58.                 <artifactId>spring-boot-maven-plugin</artifactId>
    59.                 <configuration>
    60.                     <excludes>
    61.                         <exclude>
    62.                             <groupId>org.projectlombok</groupId>
    63.                             <artifactId>lombok</artifactId>
    64.                         </exclude>
    65.                     </excludes>
    66.                 </configuration>
    67.             </plugin>
    68.         </plugins>
    69.     </build>

    70. </project>
    复制代码
    lua脚本
    1. -- KEYS[1] 是Redis中存储计数的key,,,
    2. local key = KEYS[1]

    3. -- ARGV[1]是当前时间戳-[当前时间戳]
    4. local now = tonumber(ARGV[1])

    5. -- ARGV[2]是最大请求次数-[最大请求次数]
    6. local maxRequests = tonumber(ARGV[2])

    7. -- ARGV[3]是时间窗口长度-[时间窗口长度]
    8. local windowSize = tonumber(ARGV[3])

    9. -- 获取当前时间窗口的起始时间
    10. local windowStart = math.floor(now / windowSize) * windowSize

    11. -- 构建时间窗口内的key,用于区分不同窗口的计数
    12. local windowKey = key .. ':' .. tostring(windowStart)

    13. -- 获取当前窗口的计数
    14. local currentCount = tonumber(redis.call('get', windowKey) or '0')

    15. -- 如果当前时间不在窗口内,重置计数
    16. if now > windowStart + windowSize then
    17.     redis.call('del', windowKey)
    18.     currentCount = 0
    19. end

    20. -- 检查是否超过限制
    21. if currentCount + 1 <= maxRequests then
    22.     -- 未超过,增加计数并返回成功,并设置键的过期时间为窗口剩余时间,以自动清理过期数据。如果超过最大请求次数,则拒绝请求
    23.     redis.call('set', windowKey, currentCount + 1, 'EX', windowSize - (now - windowStart))
    24.     return 1 -- 成功
    25. else
    26.     return 0 -- 失败
    27. end
    复制代码
    yaml
    1. server:
    2.   port: 10086

    3. spring:
    4.   redis:
    5.     host: 127.0.0.1
    6.     port: 6379
    7.     database: 0
    8.     lettuce:
    9.       pool:
    10.         max-active: 20
    11.         max-idle: 10
    12.         min-idle: 5
    复制代码
    代码实现


    启动类
    1. package com.kang.limter;

    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;

    5. @Slf4j
    6. @SpringBootApplication
    7. public class RateLimiterProjectApplication {

    8.     public static void main(String[] args) {
    9.         SpringApplication.run(RateLimiterProjectApplication.class, args);
    10.         log.info("RateLimiterProjectApplication start success");
    11.     }

    12. }
    复制代码
    CacheConfig
    1. package com.kang.limter.cache;

    2. import com.google.common.cache.CacheBuilder;
    3. import com.google.common.cache.CacheLoader;
    4. import com.google.common.cache.LoadingCache;
    5. import com.kang.limter.utils.LuaScriptUtils;
    6. import lombok.extern.slf4j.Slf4j;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;

    9. import java.util.Collections;
    10. import java.util.List;
    11. import java.util.concurrent.TimeUnit;

    12. import static com.kang.limter.constant.SystemConstant.REDIS_RATE_LIMITER_LUA_SCRIPT_PATH;

    13. /**
    14. * @Author Emperor Kang
    15. * @ClassName CacheConfig
    16. * @Description 缓存配置
    17. * @Date 2024/6/13 10:07
    18. * @Version 1.0
    19. * @Motto 让营地比你来时更干净
    20. */
    21. @Slf4j
    22. @Configuration
    23. public class CacheConfig {

    24.     /**
    25.      * 缓存配置,加载lua脚本
    26.      * @return
    27.      */
    28.     @Bean(name = "rateLimiterLuaCache")
    29.     public LoadingCache<String, String> rateLimiterLuaCache() {
    30.         LoadingCache<String, String> cache = CacheBuilder.newBuilder()
    31.                 // 设置缓存的最大容量,最多100个键值对
    32.                 .maximumSize(100)
    33.                 // 设置缓存项过期策略:写入后2小时过期
    34.                 .expireAfterWrite(2, TimeUnit.HOURS)
    35.                 // 缓存统计信息记录
    36.                 .recordStats()
    37.                 // 构建缓存加载器,用于加载缓存项的值
    38.                 .build(new CacheLoader<String, String>() {
    39.                     @Override
    40.                     public String load(String scriptPath) throws Exception {
    41.                         try {
    42.                             return LuaScriptUtils.loadLuaScript(scriptPath);
    43.                         } catch (Exception e) {
    44.                             log.error("加载lua脚本失败:{}", e.getMessage());
    45.                             return null;
    46.                         }
    47.                     }
    48.                 });

    49.         // 预热缓存
    50.         warmUpCache(cache);

    51.         return cache;
    52.     }

    53.     /**
    54.      * 预热缓存
    55.      */
    56.     private void warmUpCache(LoadingCache<String, String> cache) {
    57.         try {
    58.             // 假设我们有一个已知的脚本列表需要预热
    59.             List<String> knownScripts = Collections.singletonList(REDIS_RATE_LIMITER_LUA_SCRIPT_PATH);
    60.             for (String script : knownScripts) {
    61.                 String luaScript = LuaScriptUtils.loadLuaScript(script);
    62.                 // 手动初始化缓存
    63.                 cache.put(script, luaScript);
    64.                 log.info("预加载Lua脚本成功: {}, length: {}", script, luaScript.length());
    65.             }
    66.         } catch (Exception e) {
    67.             log.error("预加载Lua脚本失败: {}", e.getMessage(), e);
    68.         }
    69.     }
    70. }
    复制代码

    • 这里使用缓存预热加快lua脚本的加载速度,基于JVM内存操作,所以很快
    SystemConstant
    1. package com.kang.limter.constant;

    2. /**
    3. * @Author Emperor Kang
    4. * @ClassName SystemConstant
    5. * @Description 系统常量
    6. * @Date 2024/6/12 19:25
    7. * @Version 1.0
    8. * @Motto 让营地比你来时更干净
    9. */
    10. public class SystemConstant {
    11.     /**
    12.      * 限流配置缓存key前缀
    13.      */
    14.     public static final String REDIS_RATE_LIMITER_KEY_PREFIX = "outreach:config:limiter:%s";

    15.     /**
    16.      * 限流lua脚本路径
    17.      */
    18.     public static final String REDIS_RATE_LIMITER_LUA_SCRIPT_PATH = "classpath:lua/rate_limiter.lua";
    19. }
    复制代码
    RateLimiterController
    1. package com.kang.limter.controller;

    2. import com.kang.limter.dto.RateLimiterRequestDto;
    3. import com.kang.limter.utils.RateLimiterUtil;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.PostMapping;
    7. import org.springframework.web.bind.annotation.RequestBody;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.web.bind.annotation.RestController;

    10. import static java.lang.Thread.sleep;

    11. /**
    12. * @Author Emperor Kang
    13. * @ClassName RateLimiterController
    14. * @Description TODO
    15. * @Date 2024/6/12 19:33
    16. * @Version 1.0
    17. * @Motto 让营地比你来时更干净
    18. */
    19. @Slf4j
    20. @RestController
    21. @RequestMapping("/rate/limiter")
    22. public class RateLimiterController {
    23.     @Autowired
    24.     private RateLimiterUtil rateLimiterUtil;

    25.     @PostMapping("/test")
    26.     public String test(@RequestBody RateLimiterRequestDto rateLimiterRequestDto) {
    27.         // 是否限流
    28.         if (!rateLimiterUtil.tryAcquire(rateLimiterRequestDto.getInterfaceCode(), 5, 1000)) {
    29.             log.info("触发限流策略,InterfaceCode:{}", rateLimiterRequestDto.getInterfaceCode());
    30.             return "我被限流了InterfaceCode:" + rateLimiterRequestDto.getInterfaceCode();
    31.         }

    32.         log.info("请求参数:{}", rateLimiterRequestDto);

    33.         try {
    34.             log.info("开始加工逻辑");
    35.             sleep(1000);
    36.         } catch (InterruptedException e) {
    37.             log.error("休眠异常");
    38.             Thread.currentThread().interrupt();
    39.             return "加工异常";
    40.         }

    41.         return "加工成功,成功返回";
    42.     }
    43. }
    复制代码
    RateLimiterRequestDto
    1. package com.kang.limter.dto;

    2. import lombok.Data;

    3. /**
    4. * @Author Emperor Kang
    5. * @ClassName RateLimiterRequestDto
    6. * @Description TODO
    7. * @Date 2024/6/12 19:39
    8. * @Version 1.0
    9. * @Motto 让营地比你来时更干净
    10. */
    11. @Data
    12. public class RateLimiterRequestDto {
    13.     /**
    14.      * 接口编码
    15.      */
    16.     private String interfaceCode;
    17. }
    复制代码
    ResourceLoaderException
    1. package com.kang.limter.exception;

    2. /**
    3. * @Author Emperor Kang
    4. * @ClassName ResourceLoaderException
    5. * @Description 自定义资源加载异常
    6. * @Date 2024/6/12 18:10
    7. * @Version 1.0
    8. * @Motto 让营地比你来时更干净
    9. */
    10. public class ResourceLoaderException extends Exception{
    11.     public ResourceLoaderException() {
    12.         super();
    13.     }

    14.     public ResourceLoaderException(String message) {
    15.         super(message);
    16.     }

    17.     public ResourceLoaderException(String message, Throwable cause) {
    18.         super(message, cause);
    19.     }

    20.     public ResourceLoaderException(Throwable cause) {
    21.         super(cause);
    22.     }

    23.     protected ResourceLoaderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    24.         super(message, cause, enableSuppression, writableStackTrace);
    25.     }
    26. }
    复制代码
    LuaScriptUtils
    1. package com.kang.limter.utils;

    2. import com.kang.limter.exception.ResourceLoaderException;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.core.io.DefaultResourceLoader;
    5. import org.springframework.core.io.Resource;
    6. import org.springframework.core.io.ResourceLoader;
    7. import org.springframework.util.Assert;

    8. import java.io.BufferedReader;
    9. import java.io.InputStreamReader;
    10. import java.nio.charset.StandardCharsets;

    11. @Slf4j
    12. public class LuaScriptUtils {

    13.     /**
    14.      * 从类路径下读取Lua脚本内容。
    15.      * @param scriptPath 类路径下的Lua脚本文件路径
    16.      * @return Lua脚本的文本内容
    17.      */
    18.     public static String loadLuaScript(String scriptPath) throws ResourceLoaderException {
    19.         Assert.notNull(scriptPath, "script path must not be null");
    20.         try {
    21.             // 读取lua脚本
    22.             ResourceLoader resourceLoader = new DefaultResourceLoader();
    23.             Resource resource = resourceLoader.getResource(scriptPath);
    24.             try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
    25.                 StringBuilder scriptBuilder = new StringBuilder();
    26.                 String line;
    27.                 while ((line = reader.readLine()) != null) {
    28.                     scriptBuilder.append(line).append("\n");
    29.                 }
    30.                 String lua = scriptBuilder.toString();
    31.                 log.debug("读取的lua脚本为: {}", lua);
    32.                 return lua;
    33.             }
    34.         } catch (Exception e) {
    35.             log.error("Failed to load Lua script from path: {}", scriptPath, e);
    36.             throw new ResourceLoaderException("Failed to load Lua script from path: " + scriptPath, e);
    37.         }
    38.     }
    39. }
    复制代码
    RateLimiterUtil
    1. package com.kang.limter.utils;

    2. import com.google.common.cache.LoadingCache;
    3. import com.kang.limter.exception.ResourceLoaderException;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.apache.commons.lang3.StringUtils;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.beans.factory.annotation.Qualifier;
    8. import org.springframework.data.redis.connection.ReturnType;
    9. import org.springframework.data.redis.core.RedisCallback;
    10. import org.springframework.data.redis.core.StringRedisTemplate;
    11. import org.springframework.stereotype.Component;
    12. import org.springframework.util.Assert;

    13. import java.nio.charset.StandardCharsets;

    14. import static com.kang.limter.constant.SystemConstant.REDIS_RATE_LIMITER_KEY_PREFIX;
    15. import static com.kang.limter.constant.SystemConstant.REDIS_RATE_LIMITER_LUA_SCRIPT_PATH;

    16. /**
    17. * @Author Emperor Kang
    18. * @ClassName RateLimiterUtil
    19. * @Description 限流工具类
    20. * @Date 2024/6/12 17:56
    21. * @Version 1.0
    22. * @Motto 让营地比你来时更干净
    23. */
    24. @Slf4j
    25. @Component
    26. public class RateLimiterUtil {
    27.     @Autowired
    28.     private StringRedisTemplate redisTemplate;

    29.     @Autowired
    30.     @Qualifier("rateLimiterLuaCache")
    31.     private LoadingCache<String, String> rateLimiterLuaCache;


    32.     /**
    33.      * @param interfaceCode 接口标识
    34.      * @param maxRequests   最大请求数
    35.      * @param windowSizeMs  窗口大小
    36.      * @return boolean
    37.      * @Description 尝试获取令牌
    38.      * @Author Emperor Kang
    39.      * @Date 2024/6/12 17:57
    40.      * @Version 1.0
    41.      */
    42.     public boolean tryAcquire(String interfaceCode, int maxRequests, long windowSizeMs) {
    43.         try {
    44.             long currentTimeMillis = System.currentTimeMillis();

    45.             String luaScript = rateLimiterLuaCache.get(REDIS_RATE_LIMITER_LUA_SCRIPT_PATH);
    46.             log.info("缓存查询lua,length={}", luaScript.length());

    47.             if(StringUtils.isBlank(luaScript)){
    48.                 log.info("从缓存中未获取到lua脚本,尝试手动读取");
    49.                 luaScript = LuaScriptUtils.loadLuaScript(REDIS_RATE_LIMITER_LUA_SCRIPT_PATH);
    50.             }

    51.             // 二次确认
    52.             if(StringUtils.isBlank(luaScript)){
    53.                 log.info("lua脚本加载失败,暂时放弃获取许可,不再限流");
    54.                 return true;
    55.             }

    56.             // 限流核心逻辑
    57.             String finalLuaScript = luaScript;
    58.             Long result = redisTemplate.execute((RedisCallback<Long>) connection -> {
    59.                 // 用于存储的key
    60.                 byte[] key = String.format(REDIS_RATE_LIMITER_KEY_PREFIX, interfaceCode).getBytes(StandardCharsets.UTF_8);
    61.                 // 当前时间(毫秒)
    62.                 byte[] now = String.valueOf(currentTimeMillis).getBytes(StandardCharsets.UTF_8);
    63.                 // 最大请求数
    64.                 byte[] maxRequestsBytes = String.valueOf(maxRequests).getBytes(StandardCharsets.UTF_8);
    65.                 // 窗口大小
    66.                 byte[] windowSizeBytes = String.valueOf(windowSizeMs).getBytes(StandardCharsets.UTF_8);
    67.                 // 执行lua脚本
    68.                 return connection.eval(finalLuaScript.getBytes(StandardCharsets.UTF_8), ReturnType.INTEGER, 1, key, now, maxRequestsBytes, windowSizeBytes);
    69.             });

    70.             Assert.notNull(result, "执行lua脚本响应结果为null");

    71.             // 获取结果
    72.             return result == 1L;
    73.         } catch (ResourceLoaderException e) {
    74.             log.error("加载lua脚本失败", e);
    75.         } catch (Exception e){
    76.             log.error("执行限流逻辑异常", e);
    77.         }
    78.         return true;
    79.     }
    80. }
    复制代码
    lua脚本
    1. -- KEYS[1] 是Redis中存储计数的key,,,
    2. local key = KEYS[1]

    3. -- ARGV[1]是当前时间戳-[当前时间戳]
    4. local now = tonumber(ARGV[1])

    5. -- ARGV[2]是最大请求次数-[最大请求次数]
    6. local maxRequests = tonumber(ARGV[2])

    7. -- ARGV[3]是时间窗口长度-[时间窗口长度]
    8. local windowSize = tonumber(ARGV[3])

    9. -- 获取当前时间窗口的起始时间
    10. local windowStart = math.floor(now / windowSize) * windowSize

    11. -- 构建时间窗口内的key,用于区分不同窗口的计数
    12. local windowKey = key .. ':' .. tostring(windowStart)

    13. -- 获取当前窗口的计数
    14. local currentCount = tonumber(redis.call('get', windowKey) or '0')

    15. -- 如果当前时间不在窗口内,重置计数
    16. if now > windowStart + windowSize then
    17.     redis.call('del', windowKey)
    18.     currentCount = 0
    19. end

    20. -- 检查是否超过限制
    21. if currentCount + 1 <= maxRequests then
    22.     -- 未超过,增加计数并返回成功,并设置键的过期时间为窗口剩余时间,以自动清理过期数据。如果超过最大请求次数,则拒绝请求
    23.     redis.call('set', windowKey, currentCount + 1, 'EX', windowSize - (now - windowStart))
    24.     return 1 -- 成功
    25. else
    26.     return 0 -- 失败
    27. end
    复制代码
    Jmeter压测



    200次请求/s,限流了195,而我们设置的最大令牌数就是5
    到此这篇关于redis+lua实现分布式限流的示例的文章就介绍到这了,更多相关redis+lua分布式限流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/database/3382943ks.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表