Java 注解式限流教程(使用 Redis + AOP)

article/2025/7/22 2:47:41

在这里插入图片描述

Java 注解式限流教程(使用 Redis + AOP)

在上一节中,我们已经实现了基于 Redis 的请求频率控制。现在我们将进一步升级功能,使用 Spring AOP + 自定义注解 实现一个更优雅、可复用的限流方式 —— 即通过 @RateLimiter 注解,对任意接口进行限流保护。


🧩 技术栈

  • Spring Boot 3.x
  • Redis
  • Jedis 连接池
  • Lua 脚本实现原子性操作
  • Spring AOP 实现注解式切面处理

📦 Maven 依赖配置(补充 AOP 支持)

确保你的 pom.xml 中包含以下依赖:

<!-- Spring Boot Redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>3.2.10</version>
</dependency><!-- Jedis 连接池 -->
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.0.2</version>
</dependency><!-- Spring Boot AOP -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>3.2.10</version>
</dependency>

🛠️ Redis 配置(application.yml)

与之前保持一致:

spring:data:redis:host: 127.0.0.1port: 6379password: database: 1timeout: 5000msjedis:pool:max-active: 8   # 最大连接数max-idle: 4     # 最大空闲连接min-idle: 1     # 最小空闲连接max-wait: 2000ms # 获取连接最大等待时间

📁 项目结构概览

org.example.websocket.test
├── annotation
│   └── RateLimiter.java
├── aspect
│   └── RateLimiterAspect.java
├── utils
│   └── RedisRateLimiter.java
├── config
│   └── RateLimiterConfig.java
└── controller└── RateLimitController.java

🔖 第一步:创建自定义限流注解

RateLimiter.java

package org.example.websocket.test.annotation;import java.lang.annotation.*;/*** 自定义限流注解,支持方法或类级别标注*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter {/*** 每个窗口内允许的最大请求数,默认10次*/int limit() default 10;/*** 窗口时间(秒),默认60秒*/int windowTime() default 60;/*** 限流维度:* - ip: 按客户端IP限流* - user: 按用户ID限流(需从参数中获取)*/String key() default "ip";
}

🧠 第二步:编写 AOP 切面逻辑

RateLimiterAspect.java

package org.example.websocket.test.aspect;import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.example.websocket.test.annotation.RateLimiter;
import org.example.websocket.test.utils.RedisRateLimiter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import java.util.Random;@Aspect
@Component
public class RateLimiterAspect {private final RedisRateLimiter redisRateLimiter;public RateLimiterAspect(RedisRateLimiter redisRateLimiter) {this.redisRateLimiter = redisRateLimiter;}@Around("@annotation(rateLimiter)")public Object around(ProceedingJoinPoint joinPoint, RateLimiter rateLimiter) throws Throwable {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String keyPrefix = rateLimiter.key();String dynamicKey = "";if ("ip".equals(keyPrefix)) {dynamicKey = getClientIP(request);} else if ("user".equals(keyPrefix)) {dynamicKey = request.getParameter("account");}if (dynamicKey == null || dynamicKey.isEmpty()) {return "无法识别限流标识,请检查请求参数或IP信息";}String redisKey = "rate_limit:" + keyPrefix + ":" + dynamicKey;// 执行限流判断if (!redisRateLimiter.check(redisKey, rateLimiter.limit(), rateLimiter.windowTime())) {return "请求过于频繁,请稍后再试";}// 防枚举攻击延迟try {Thread.sleep(new Random().nextInt(200));} catch (InterruptedException ignored) {}// 继续执行原方法return joinPoint.proceed();}private String getClientIP(HttpServletRequest request) {String ip = request.getHeader("X-Forwarded-For");if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}if (ip != null && ip.contains(",")) {ip = ip.split(",")[0].trim();}return ip;}
}

🧮 第三步:优化 Redis 限流工具类

RedisRateLimiter.java

package org.example.websocket.test.utils;import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;import java.util.Collections;public class RedisRateLimiter {private final StringRedisTemplate redisTemplate;public RedisRateLimiter(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}public boolean check(String key, int limit, int expireTime) {String luaScript = buildLuaScript();DefaultRedisScript<Boolean> script = new DefaultRedisScript<>(luaScript, Boolean.class);Boolean isAllowed = redisTemplate.execute(script,Collections.singletonList(key),String.valueOf(limit), String.valueOf(expireTime));return Boolean.TRUE.equals(isAllowed);}private String buildLuaScript() {return "local key = KEYS[1]\n" +"local limit = tonumber(ARGV[1])\n" +"local expire_time = tonumber(ARGV[2])\n" +"local current = redis.call('incr', key)\n" +"if current == 1 then\n" +"    redis.call('expire', key, expire_time)\n" +"elseif current > limit then\n" +"    return false\n" +"end\n" +"return true";}
}

⚙️ 第四步:注册 RedisRateLimiter Bean

RateLimiterConfig.java

package org.example.websocket.test.config;import org.example.websocket.test.utils.RedisRateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;@Configuration
public class RateLimiterConfig {@Beanpublic RedisRateLimiter redisRateLimiter(StringRedisTemplate redisTemplate) {return new RedisRateLimiter(redisTemplate);}
}

🧪 第五步:在控制器中使用注解限流

RateLimitController.java

package org.example.websocket.test.controller;import jakarta.servlet.http.HttpServletRequest;
import org.example.websocket.test.annotation.RateLimiter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class RateLimitController {// 使用注解按 IP 限流@GetMapping("/testAnnoRateLimit")@RateLimiter(limit = 10, windowTime = 60, key = "ip")public String testAnnoRateLimit(@RequestParam String account, HttpServletRequest request) {return "恭喜,请求成功放行!";}// 使用注解按用户限流@GetMapping("/testUserRateLimit")@RateLimiter(limit = 5, windowTime = 30, key = "user")public String testUserRateLimit(@RequestParam String account, HttpServletRequest request) {return "用户[" + account + "] 请求成功放行!";}
}

✅ 效果演示

当你多次快速访问 /testAnnoRateLimit 接口时,在超过设定频率后会返回:

请求过于频繁,请稍后再试

📌 总结

你现在拥有了一个 基于注解的限流系统,可以轻松地对任意接口进行限流保护。该方案具备如下优点:

特性描述
✅ 注解驱动使用 @RateLimiter 轻松启用限流
✅ 多种限流维度支持 IP、用户等多维限流
✅ Lua 原子操作Redis + Lua 保证线程安全
✅ 易于扩展后续可增加令牌桶算法、滑动窗口优化等


http://www.hkcw.cn/article/QnNTEBZOne.shtml

相关文章

C++学习-入门到精通【10】面向对象编程:多态性

C学习-入门到精通【10】面向对象编程&#xff1a;多态性 目录 C学习-入门到精通【10】面向对象编程&#xff1a;多态性一、多态性介绍&#xff1a;多态电子游戏二、类继承层次中对象之间的关系1.从派生类对象调用基类函数2.将派生类指针指向基类对象3.通过基类指针调用派生类的…

基于springboot的医护人员排班系统设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论文…

1、python代码实现与大模型的问答交互

一、基础知识 1.1导入库 torch 是一个深度学习框架&#xff0c;用于处理张量和神经网络。modelscope是由阿里巴巴达摩院推出的开源模型库。 AutoTokenizer 是ModelScope 库的类&#xff0c;分词器应用场景包括自然语言处理&#xff08;NLP&#xff09;中的文本分类、信息抽取…

再见Cursor!Trae Pro 登场

5 月 27 日&#xff0c;字节跳动旗下的 AI 编辑器 Trae 国际版正式推出了 Pro 订阅计划。长期以来&#xff0c;Trae 凭借免费使用和出色的编程体验&#xff0c;深受大家喜爱。不过&#xff0c;免费版在实际使用中&#xff0c;排队等待的情况时有发生&#xff0c;着实给用户带来…

【Docker 从入门到实战全攻略(一):核心概念 + 命令详解 + 部署案例】

1. 是什么 Docker 是一个用于开发、部署和运行应用程序的开源平台&#xff0c;它使用 容器化技术 将应用及其依赖打包成独立的容器&#xff0c;确保应用在不同环境中一致运行。 2. Docker与虚拟机 2.1 Docker&#xff08;容器化&#xff09; 容器化是一种轻量级的虚拟化技术…

rm删除到回收站

rm删除到回收站 背景安装trash-clipip安装包管理器安装 将trash-put别名设为rm设置回收站文件过期时间 trash基本用法删除文件删除后文件去了哪里 查看回收站从回收站中恢复文件恢复文件到指定路径 删除回收站中的指定文件 背景 在Linux命令行下操作的时候会不小心误删文件或目…

DDP与FSDP:分布式训练技术全解析

DDP与FSDP:分布式训练技术全解析 DDP(Distributed Data Parallel)和 FSDP(Fully Sharded Data Parallel)均为用于深度学习模型训练的分布式训练技术,二者借助多 GPU 或多节点来提升训练速度。 1. DDP(Distributed Data Parallel) 实现原理 数据并行:把相同的模型复…

数据采集是什么?一文讲清数据采集系统的模式!

目录 一、数据采集是什么&#xff1f; 二、为什么要进行数据采集 1. 为企业决策提供依据 2. 推动科学研究的发展 3. 提升生产效率和质量 三、数据采集系统的模式 1. 实时采集模式 2. 定时采集模式 3. 事件驱动采集模式 四、不同模式的应用场景及选择考虑因素 1. 应用…

python学习day33

知识点回顾&#xff1a; 1.PyTorch和cuda的安装 2.查看显卡信息的命令行命令&#xff08;cmd中使用&#xff09; 3.cuda的检查 4.简单神经网络的流程 a.数据预处理&#xff08;归一化、转换成张量&#xff09; b.模型的定义 i.继承nn.Module类 ii.定义每一个层 iii.定义前向传播…

Python中的变量、赋值及函数的参数传递概要

Python中的变量、赋值及函数的参数传递概要 python中的变量、赋值 python中的变量不是盒子。 python中的变量无法用“变量是盒子”做解释。图说明了在 Python 中为什么不能使用盒子比喻&#xff0c;而便利贴则指出了变量的正确工作方式。 如果把变量想象为盒子&#xff0c;那…

如何优化微信小程序中渲染带有图片的列表(二进制流存储方式的图片存在本地数据库)

方法一&#xff1a;对列表的获取进行分页处理 实现方法&#xff1a; 前端请求&#xff08;需要向后端传两个参数&#xff0c;pageIndex是获取第几页是从0开始&#xff0c;pageSize是这一页需要获取多少个数据&#xff09; 后端接口实现&#xff08;因为这里是通过参数拼接请求…

电磁器件的“折纸革命“:牛津《Sci. Reports》发布剪纸超材料

01 前沿速递&#xff1a;顶尖团队破解行业难题 近日&#xff0c;牛津大学工程科学系杨云芳、Andrea Vallecchi、Ekaterina Shamonina、Christopher Stevens及游忠教授团队在《Scientific Reports》发表突破性研究&#xff0c;提出一类基于剪纸&#xff08;Kirigami&#xff0…

【Java学习笔记】接口

接口 应用场景引出 一、接口的介绍 1. 接口的基本结构 interface 接口名{属性抽象方法 }引出关键字&#xff1a;implements 2. 子类实现接口 class a implements 接口名{}3. 接口中的属性说明&#xff1a;属性默认是public static final修饰的 &#xff08;1&#xff09;f…

02 APP 自动化-Appium 运行原理详解

环境搭建见 01 APP 自动化-环境搭建 文章目录 一、Appium及Appium自动化测试原理二、Appium 自动化配置项三、常见 ADB 命令四、第一个 app 自动化脚本 一、Appium及Appium自动化测试原理 Appium 跨平台、开源的 app 自动化测试框架&#xff0c;用来测试 app 应用程序&#x…

(1)pytest简介和环境准备

1. pytest简介 pytest是python的一种单元测试框架&#xff0c;与python自带的unittest测试框架类似&#xff0c;但是比unittest框架使用起来更简洁&#xff0c;效率更高。根据pytest的官方网站介绍&#xff0c;它具有如下特点&#xff1a; 非常容易上手&#xff0c;入门简单&a…

同元软控、核动力研究院与华北电力大学产学研联合实训室正式揭牌

2025年5月27日&#xff0c;华北电力大学、苏州同元软控信息技术有限公司&#xff08;以下简称“同元软控”&#xff09;、中国核动力研究设计院&#xff08;以下简称“核动力研究院”&#xff09;联合实训室揭牌授权仪式暨座谈交流会在华北电力大学召开。华北电力大学教务处处长…

PyTorch中nn.Module详解

直接print(dir(nn.Module))&#xff0c;得到如下内容&#xff1a; 一、模型结构与参数 parameters() 用途&#xff1a;返回模块的所有可训练参数&#xff08;如权重、偏置&#xff09;。示例&#xff1a;for param in model.parameters():print(param.shape)named_parameters…

若依项目天气模块

在若依项目里添加了一个天气模块&#xff0c;记录一下过程。 一、功能结构与组件布局 天气模块以卡片形式&#xff08;el-card&#xff09;展示&#xff0c;包含以下核心功能&#xff1a; 实时天气&#xff1a;显示当前城市、温度、天气状况&#xff08;如晴、多云&#xff…

APM32芯得 EP.06 | APM32F407移植uC/OS-III实时操作系统经验分享

《APM32芯得》系列内容为用户使用APM32系列产品的经验总结&#xff0c;均转载自21ic论坛极海半导体专区&#xff0c;全文未作任何修改&#xff0c;未经原文作者授权禁止转载。 最近我开始学习 uC/OS-III 实时操作系统&#xff0c;并着手将其移植到APM32F407 开发板上。在这个过…

图解gpt之注意力机制原理与应用

大家有没有注意到&#xff0c;当序列变长时&#xff0c;比如翻译一篇长文章&#xff0c;或者处理一个长句子&#xff0c;RNN这种编码器就有点力不从心了。它把整个序列信息压缩到一个固定大小的向量里&#xff0c;信息丢失严重&#xff0c;而且很难记住前面的细节&#xff0c;特…