后端下载限速(redis记录实时并发,bucket4j动态限速)

  • ✅ 使用 Redis 记录 所有用户的实时并发下载数
  • ✅ 使用 Bucket4j 实现 全局下载速率限制(动态)
  • ✅ 支持 动态调整限速策略
  • ✅ 下载接口安全、稳定、可监控

🧩 整体架构概览

模块功能
Redis存储全局并发数和带宽令牌桶状态
Bucket4j + Redis分布式限速器(基于令牌桶算法)
Spring Boot Web提供文件下载接口
AOP / Interceptor(可选)用于统一处理限流逻辑

📦 1. Maven 依赖(pom.xml

<dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Redis 连接池 --><dependency><groupId>io.lettuce.core</groupId><artifactId>lettuce-core</artifactId></dependency><!-- Bucket4j 核心与 Redis 集成 --><dependency><groupId>com.github.vladimir-bukhtoyarov</groupId><artifactId>bucket4j-core</artifactId><version>5.3.0</version></dependency><dependency><groupId>com.github.vladimir-bukhtoyarov</groupId><artifactId>bucket4j-redis</artifactId><version>5.3.0</version></dependency></dependencies>

🛠️ 2. Redis 工具类:记录全局并发数

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;import java.util.Collections;
import java.util.concurrent.TimeUnit;@Component
public class GlobalDownloadCounter {private final StringRedisTemplate redisTemplate;private final DefaultRedisScript<Long> incrScript;private final DefaultRedisScript<Long> decrScript;public static final String KEY_CONCURRENT = "global:download:concurrent";private static final long TTL_SECONDS = 60; // 自动清理僵尸计数public GlobalDownloadCounter(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;// Lua 脚本:原子增加并发数并设置过期时间String scriptIncr = """local key = KEYS[1]local ttl = tonumber(ARGV[1])local count = redis.call('GET', key)if not count thenredis.call('SET', key, 1)redis.call('EXPIRE', key, ttl)return 1elsecount = tonumber(count) + 1redis.call('SET', key, count)redis.call('EXPIRE', key, ttl)return countend""";incrScript = new DefaultRedisScript<>(scriptIncr, Long.class);// Lua 脚本:原子减少并发数String scriptDecr = """local key = KEYS[1]local count = redis.call('GET', key)if not count or tonumber(count) <= 0 thenreturn 0elsecount = tonumber(count) - 1redis.call('SET', key, count)return countend""";decrScript = new DefaultRedisScript<>(scriptDecr, Long.class);}public long increment() {return redisTemplate.execute(incrScript, Collections.singletonList(KEY_CONCURRENT), TTL_SECONDS).longValue();}public long decrement() {return redisTemplate.execute(decrScript, Collections.singletonList(KEY_CONCURRENT)).longValue();}public long getCurrentCount() {String value = redisTemplate.opsForValue().get(KEY_CONCURRENT);return value == null ? 0 : Long.parseLong(value);}
}

⚙️ 3. Bucket4j 配置:分布式限速器(带 Redis)

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Refill;
import io.github.bucket4j.distributed.proxy.ProxyManager;
import io.github.bucket4j.distributed.proxy.RedisProxyManager;
import io.github.bucket4j.redis.lettuce.cas.LettuceReactiveProxyManager;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.time.Duration;@Configuration
public class BandwidthLimiterConfig {@Beanpublic RedisClient redisClient() {return RedisClient.create("redis://localhost:6379");}@Beanpublic StatefulRedisConnection<String, String> redisConnection(RedisClient redisClient) {return redisClient.connect();}@Beanpublic ProxyManager<String> proxyManager(StatefulRedisConnection<String, String> connection) {return LettuceReactiveProxyManager.builder().build(connection.reactive());}@Beanpublic Bandwidth globalBandwidthLimit() {// 默认 10MB/sreturn Bandwidth.classic(10 * 1024 * 1024, Refill.greedy(10 * 1024 * 1024, Duration.ofSeconds(1)));}@Beanpublic Bucket globalBandwidthLimiter(ProxyManager<String> proxyManager, Bandwidth bandwidthLimit) {return proxyManager.builder().build("global:bandwidth:limiter", bandwidthLimit);}
}

📡 4. 下载接口实现

import io.github.bucket4j.Bucket;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;@RestController
@RequestMapping("/api/download")
public class DownloadController {private static final int MAX_CONCURRENT_DOWNLOADS = 100;private final GlobalDownloadCounter downloadCounter;private final Bucket bandwidthLimiter;public DownloadController(GlobalDownloadCounter downloadCounter, Bucket bandwidthLimiter) {this.downloadCounter = downloadCounter;this.bandwidthLimiter = bandwidthLimiter;}@GetMapping("/{fileId}")public void downloadFile(@PathVariable String fileId, HttpServletResponse response) throws IOException {long currentCount = downloadCounter.getCurrentCount();if (currentCount >= MAX_CONCURRENT_DOWNLOADS) {response.setStatus(HttpServletResponse.SC_TOO_MANY_REQUESTS);response.getWriter().write("Too many downloads. Please try again later.");return;}downloadCounter.increment();try {// 设置响应头response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment; filename=" + fileId + ".bin");ServletOutputStream out = response.getOutputStream();// 文件路径(示例)String filePath = "/path/to/files/" + fileId + ".bin";if (!Files.exists(Paths.get(filePath))) {response.setStatus(HttpServletResponse.SC_NOT_FOUND);response.getWriter().write("File not found.");return;}byte[] buffer = new byte[8192]; // 每次读取 8KBRandomAccessFile file = new RandomAccessFile(filePath, "r");int bytesRead;while ((bytesRead = file.read(buffer)) != -1) {if (bytesRead > 0) {boolean consumed = bandwidthLimiter.tryConsume(bytesRead);if (!consumed) {Thread.sleep(100); // 等待令牌生成continue;}out.write(buffer, 0, bytesRead);out.flush();}}file.close();out.close();} catch (InterruptedException e) {Thread.currentThread().interrupt(); // 恢复中断状态} finally {downloadCounter.decrement();}}
}

🔁 5. 动态调整下载速率接口(可选)

@RestController
@RequestMapping("/api/limit")
public class RateLimitController {private final Bucket bandwidthLimiter;private final Bandwidth globalBandwidthLimit;public RateLimitController(Bucket bandwidthLimiter, Bandwidth globalBandwidthLimit) {this.bandwidthLimiter = bandwidthLimiter;this.globalBandwidthLimit = globalBandwidthLimit;}@PostMapping("/set-bandwidth")public String setBandwidth(@RequestParam int mbPerSecond) {Bandwidth newLimit = Bandwidth.classic(mbPerSecond * 1024 * 1024,Refill.greedy(mbPerSecond * 1024 * 1024, Duration.ofSeconds(1)));bandwidthLimiter.replaceConfiguration(newLimit);return "Global bandwidth limit updated to " + mbPerSecond + " MB/s";}
}

📊 6. 监控接口(可选)

@GetMapping("/monitor/concurrent")
public ResponseEntity<Long> getConcurrentDownloads() {return ResponseEntity.ok(downloadCounter.getCurrentCount());
}

🧪 7. 测试建议

你可以使用 curl 或 Postman 发起多并发请求测试:

for i in {1..200}; docurl -X GET "http://localhost:8080/api/download/file1" --output "file$i.bin" &
done

观察是否触发限流、并发控制是否生效。


✅ 总结

组件作用
Redis分布式存储并发数和限流令牌桶
Lua 脚本原子操作并发计数器
Bucket4j + Redis全局下载速率限制
Spring Boot Controller处理下载逻辑
try-finally保证资源释放
动态接口 /set-bandwidth支持运行时修改限速

📌 扩展建议(可选)

  • 将限流逻辑封装到 AOP 切面
  • 添加 Prometheus 指标暴露并发数、限流次数等
  • 使用 Nginx 或 Gateway 做额外的限流保护
  • 加入用户身份识别,支持 用户级限速
  • 使用 Kafka 异步记录日志或审计下载行为

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/pingmian/84209.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android app 一个 crash的解决过程!

一、日志&#xff1a; crash 2024-10-25 12:15:33.020 2113-2113 AndroidRuntime pid-2113 E FATAL EXCEPTION: main Process: com..workhome, PID: 2113 java.lang.RuntimeException: Unable to start activity ComponentInfo{com..w…

[Java 基础]Object 类

java.lang.Object 是 Java 所有类的直接或间接父类&#xff0c;Java 中每个类都默认继承 Object 类&#xff08;即使你没写 extends Object&#xff09;。 Object 中的常用方法&#xff1a; 方法名功能简介toString()返回对象的字符串表示equals(Object)判断两个对象是否“逻…

大数据学习(135)-Linux系统性指令

&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一…

【Fifty Project - D35】

今日完成记录 TimePlan完成情况7&#xff1a;00 - 7&#xff1a;40爬坡√8&#xff1a;30 - 11&#xff1a;30Rabbit MQ√17&#xff1a;30 - 18&#xff1a;30羽毛球√ RabbitMQ 消费者端如何保证可靠性&#xff1f; 消息投递过程出现网络故障消费者接收到消息但是突然宕机…

P3 QT项目----记事本(3.4)

3.4 文件选择对话框 QFileDialog 3.4.1 QFileDialog 开发流程 使用 QFileDialog 的基本步骤通常如下&#xff1a; 实例化 &#xff1a;首先&#xff0c;创建一个 QFileDialog 对象的实例。 QFileDialog qFileDialog;设置模式 &#xff1a;根据需要设置对话框的模式&…

学习笔记(26):线性代数-张量的降维求和,简单示例

学习笔记(26)&#xff1a;线性代数-张量的降维求和&#xff0c;简单示例 1.先理解 “轴&#xff08;Axis&#xff09;” 的含义 张量的 “轴” 可以理解为 维度的方向索引 。对于形状为 (2, 3, 4) 的张量&#xff0c;3 个轴的含义是&#xff1a; 轴 0&#xff08;axis0&…

健康档案实训室:构建全周期健康管理的数据基石

一、健康档案实训室建设背景 随着“健康中国2030”战略深入推进&#xff0c;健康档案作为居民健康数据的核心载体&#xff0c;在疾病预防、慢性病管理、医疗决策等领域的价值日益凸显。在此背景下&#xff0c;健康档案实训室建设成为职业院校对接政策要求、培养专业健康管理…

【MATLAB第119期】基于MATLAB的KRR多输入多输出全局敏感性分析模型运用(无目标函数,考虑代理模型)

【MATLAB第119期】基于MATLAB的KRR多输入多输出全局敏感性分析模型运用&#xff08;无目标函数&#xff0c;考虑代理模型&#xff09; 下一期研究SHAP的多输入多输出敏感性分析方法 一、SOBOL&#xff08;无目标函数&#xff09; &#xff08;1&#xff09;针对简单线性数据…

Linux常用文件目录命令

浏览目录命令&#xff1a; ls 、pwd目录操作命令&#xff1a;cd、mkdir、rmdir浏览文件命令&#xff1a;cat、more、less、head、tail文件操作命令&#xff1a;cp、rm、mv、find、grep、tar 浏览目录命令 ls ◼ 命令名称&#xff1a;ls ◼ 命令英文原意&#xff1a;list ◼ …

PIN码vs密码,电脑登录的快捷键你用对了吗?

你是否也遇到过这样的窘境&#xff1a;信心满满地输入电脑开机密码&#xff0c;屏幕却无情地提示“密码错误”。仔细一看&#xff0c;才发现登录界面悄悄地变成了要求输入“PIN码”。这种因为混淆了PIN码和账户密码而导致的开机失败&#xff0c;相信不少朋友都碰到过。 PIN码作…

【大模型科普】AIGC技术发展与应用实践(一文读懂AIGC)

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈人工智能与大模型应用 ⌋ ⌋ ⌋ 人工智能&#xff08;AI&#xff09;通过算法模拟人类智能&#xff0c;利用机器学习、深度学习等技术驱动医疗、金融等领域的智能化。大模型是千亿参数的深度神经网络&#xff08;如ChatGPT&…

Spring是如何解决Bean的循环依赖:三级缓存机制

1、什么是 Bean 的循环依赖 在 Spring框架中,Bean 的循环依赖是指多个 Bean 之间‌互相持有对方引用‌,形成闭环依赖关系的现象。 多个 Bean 的依赖关系构成环形链路,例如: 双向依赖:Bean A 依赖 Bean B,同时 Bean B 也依赖 Bean A(A↔B)。链条循环: Bean A → Bean…

XXE漏洞知识

目录 1.XXE简介与危害 XML概念 XML与HTML的区别 1.pom.xml 主要作用 2.web.xml 3.mybatis 2.XXE概念与危害 案例&#xff1a;文件读取&#xff08;需要Apache >5.4版本&#xff09; 案例&#xff1a;内网探测&#xff08;鸡肋&#xff09; 案例&#xff1a;执行命…

02-性能方案设计

需求分析与测试设计 根据具体的性能测试需求&#xff0c;确定测试类型&#xff0c;以及压测的模块(web/mysql/redis/系统整体)前期要与相关人员充分沟通&#xff0c;初步确定压测方案及具体的性能指标QA完成性能测试设计后&#xff0c;需产出测试方案文档发送邮件到项目组&…

STL优先级队列的比较函数与大堆小堆的关系

STL中的priority_queue&#xff08;优先级队列&#xff09;通过比较函数来确定元素的优先级顺序&#xff0c;从而决定其内部是形成大堆还是小堆。以下是关键点总结&#xff1a; 默认行为与大堆&#xff1a; 默认情况下&#xff0c;priority_queue使用std::less<T>作为比较…

React---day11

14.4 react-redux第三方库 提供connect、thunk之类的函数 以获取一个banner数据为例子 store&#xff1a; 我们在使用异步的时候理应是要使用中间件的&#xff0c;但是configureStore 已经自动集成了 redux-thunk&#xff0c;注意action里面要返回函数 import { configureS…

OD 算法题 B卷【反转每对括号间的子串】

文章目录 反转每对括号间的子串 反转每对括号间的子串 给出一个字符串s&#xff0c; 仅含有小写英文字母和英文括号’(’ ‘)’&#xff1b;按照从括号内到外的顺序&#xff0c;逐层反转每对括号中的字符串&#xff0c;并返回最终的结果&#xff1b;结果中不能包含任何括号&am…

如何做好一份技术文档?从规划到实践的完整指南

如何做好一份技术文档&#xff1f;从规划到实践的完整指南 &#x1f31f; 嗨&#xff0c;我是IRpickstars&#xff01; &#x1f30c; 总有一行代码&#xff0c;能点亮万千星辰。 &#x1f50d; 在技术的宇宙中&#xff0c;我愿做永不停歇的探索者。 ✨ 用代码丈量世界&…

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…

详细讲解Flutter GetX的使用

Flutter GetX 框架详解&#xff1a;状态管理、路由与依赖注入 GetX 是 Flutter 生态中一款强大且轻量级的全功能框架&#xff0c;集成了状态管理、路由管理和依赖注入三大核心功能。其设计理念是简洁高效&#xff0c;通过最小的代码实现最大的功能&#xff0c;特别适合快速开发…