当前位置: 首页 > news >正文

接口限流代码 - 实践

接口限流代码 - 实践

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
@Component
public class RateLimitInterceptor implements HandlerInterceptor {
@Autowired
private RateLimiter rateLimiter;
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) {
String ip = request.getRemoteAddr();
if (!rateLimiter.tryAcquire(ip)) {
response.setStatus(429);
return false;
}
return true;
}
}
public class RedisRateLimiter implements RateLimiter {
private final RedisTemplate redisTemplate;
public boolean tryAcquire(String key, int limit, int timeout) {
Long count = redisTemplate.opsForValue().increment(key, 1);
if (count == 1) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
return count = 1) {
tokens--;
return true;
}
return false;
}
}
@Configuration
public class RateLimitConfig {
@Bean
public RateLimiter redisRateLimiter(RedisTemplate redisTemplate) {
return new RedisRateLimiter(redisTemplate);
}
@Bean
public RateLimiter tokenBucketRateLimiter() {
return new TokenBucketRateLimiter(10, 1); // 10容量,每秒1个令牌
}
}
  1. Redis限流:通过Redis的原子操作实现精确计数,适合分布式环境
  2. 令牌桶算法:单机限流方案,支持突发流量处理
  3. 拦截器机制:统一处理所有接口请求
  4. 可配置化:支持动态调整限流参数
  5. 响应状态码:返回429 Too Many Requests

实际部署时建议:

  • 结合Nginx层限流
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
http {
# 白名单IP段定义
geo $limit {
default 1;
10.0.0.0/8 0;    # 内网不限流
192.168.1.100 0; # 特定IP不限流
}
# 分层限流区域定义
limit_req_zone $binary_remote_addr zone=api_low:10m rate=100r/s;
limit_req_zone $binary_remote_addr zone=api_medium:10m rate=50r/s;
limit_req_zone $binary_remote_addr zone=api_high:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
# 自定义日志格式
log_format rate_log '$remote_addr - $http_x_forwarded_for [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'RateStatus=$limit_req_status ConnStatus=$limit_conn_status '
'ServiceTime=$request_time';
access_log /var/log/nginx/rate_limit.log rate_log;
error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name api.example.com;
# 公共静态资源不限流
location /static/ {
alias /var/www/static/;
access_log off;
}
# 健康检查接口不限流
location /health {
access_by_lua_block {
ngx.say("OK");
ngx.exit(200);
}
}
# 重要接口分层限流
location ~ ^/api/vip/ {
limit_req zone=api_high burst=20 nodelay;
limit_conn conn_limit 3;
proxy_pass http://backend_vip;
}
location ~ ^/api/business/ {
limit_req zone=api_medium burst=30 delay=10;
limit_conn conn_limit 10;
proxy_pass http://backend_biz;
}
# 普通接口限流
location /api/ {
limit_req zone=api_low burst=50;
limit_conn conn_limit 20;
proxy_pass http://backend;
}
# 白名单绕过限流
if ($limit = 0) {
set $limit_pass 1;
}
location / {
limit_req zone=api_low burst=50;
limit_conn conn_limit 20;
proxy_pass http://backend;
}
}
}
# 全局总限流(共享100r/s)
limit_req_zone $server_name zone=global_limit:10m rate=100r/s;
location /api/ {
limit_req zone=global_limit;  # 所有IP共享100请求/秒
}

http://www.wxhsa.cn/company.asp?id=3719

相关文章:

  • OutGuess 安装与问题排查指南(Kali Linux 环境)
  • 拓展操作码举例
  • TryHackMe | Cicada-3301 Vol:1
  • 完整教程:Word添加图/表题注
  • [MCP][01]简介与概念
  • CF819B Mister B and PR Shifts
  • 第一次自我介绍
  • 在Linux环境部署Flask应用并启用SSL/TLS安全协议
  • 0127_责任链模式(Chain of Responsibility)
  • 洛枫娜娜米讨厌数学……?
  • Spatial 语言核心概念简介
  • Redis数据库的五类核心数据结构
  • RAG 个人知识库 向量查找原理
  • css-1
  • Java-JDK8新特性
  • 解决MySQL ONLY_FULL_GROUP_BY 错误的方案
  • 博客园美化
  • spatial 一个芯片设计语言的简介 scala dsl 并行支持 -1
  • NOIP备考
  • NVIDIA GPGPU 访存通路设计调研
  • 用 Java 和 Tesseract 实现验证码图像识别
  • AGC003D
  • Java 实现验证码图像识别与处理流程详解
  • 图论杂题。
  • 暑假训练小结
  • 初识python:一些基础的知识(函数)
  • Java并发编程(3)
  • 斐波那契子序列
  • [豪の学习笔记] 软考中级备考 基础复习#10
  • 题解:CF2137D Replace with Occurrences