refactor(config): 调整限流与存储配置
- 移除 IP 限流过滤器,改用切面实现限流 - 更新存储、定时任务与健康检查配置
This commit is contained in:
1 parent
95adee879e
commit
06c71969e5
11 files changed
+40
-128
No files matched your search
@@ -14,9 +14,6 @@ public interface RedisConstants {
|
||||
interface RateLimit {
|
||||
// 接口级限流 Key(示例:rate_limit:api:{user}:{uri})
|
||||
String API = "rate_limit:api:{}:{}";
|
||||
|
||||
// IP 全局限流 Key(示例:rate_limit:ip:192.168.1.100)
|
||||
String IP = "rate_limit:ip:{}";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,9 @@ public interface SystemConstants {
|
||||
// 系统默认密码
|
||||
String DEFAULT_PASSWORD = "123456";
|
||||
|
||||
// 初始密码配置键
|
||||
String CONFIG_KEY_USER_INIT_PASSWORD = "sys.user.initPassword";
|
||||
|
||||
// 超级管理员角色编码
|
||||
String ROOT_ROLE_CODE = "ROOT";
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class XxlJobConfig {
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.accessToken}")
|
||||
@Value("${xxl.job.access-token}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package com.youlai.boot.framework.web.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.boot.common.constant.RedisConstants;
|
||||
import com.youlai.boot.common.result.ResultCode;
|
||||
import com.youlai.boot.framework.web.util.IPUtils;
|
||||
import com.youlai.boot.framework.web.ratelimit.RateLimitProperties;
|
||||
import com.youlai.boot.framework.web.ratelimit.RedisSlidingWindowRateLimiter;
|
||||
import com.youlai.boot.framework.web.util.ResponseWriter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* IP 全局限流过滤器
|
||||
*
|
||||
* @author Ray.Hao
|
||||
* @since 4.4.0
|
||||
*/
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 5)
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IpRateLimitFilter extends OncePerRequestFilter {
|
||||
|
||||
private final RedisSlidingWindowRateLimiter rateLimiter;
|
||||
private final RateLimitProperties rateLimitProperties;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
|
||||
// 未启用时直接放行
|
||||
RateLimitProperties.Ip ipConfig = rateLimitProperties.getIp();
|
||||
if (!ipConfig.isEnabled()) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
String ip = IPUtils.getIpAddr(request);
|
||||
String key = StrUtil.format(RedisConstants.RateLimit.IP, ip);
|
||||
long windowMs = ipConfig.getWindow().toMillis();
|
||||
|
||||
// limiter 内部已统一 Fail-Open:Redis 异常返回 null,按未计数放行
|
||||
Long count = rateLimiter.count(key, windowMs);
|
||||
|
||||
int limit = ipConfig.getLimit();
|
||||
int current = count != null ? count.intValue() : 0;
|
||||
|
||||
setRateLimitHeaders(response, limit, current, windowMs);
|
||||
|
||||
if (current > limit) {
|
||||
log.warn("IP 限流触发 ip={} count={} limit={}", ip, current, limit);
|
||||
response.setHeader("Retry-After", String.valueOf(ipConfig.getWindow().getSeconds()));
|
||||
ResponseWriter.writeError(response, ResultCode.REQUEST_CONCURRENCY_LIMIT_EXCEEDED);
|
||||
return;
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置渐进式限流响应头(X-RateLimit-Limit / Remaining / Reset)
|
||||
* 超限时额外返回 Retry-After
|
||||
*/
|
||||
private void setRateLimitHeaders(HttpServletResponse response,
|
||||
int limit,
|
||||
int current,
|
||||
long windowMs) {
|
||||
int remaining = Math.max(0, limit - current);
|
||||
long resetAt = (System.currentTimeMillis() + windowMs) / 1000;
|
||||
response.setHeader("X-RateLimit-Limit", String.valueOf(limit));
|
||||
response.setHeader("X-RateLimit-Remaining", String.valueOf(remaining));
|
||||
response.setHeader("X-RateLimit-Reset", String.valueOf(resetAt));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "rate-limit", name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class RateLimitAspect {
|
||||
@@ -40,10 +42,10 @@ public class RateLimitAspect {
|
||||
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
|
||||
String key = buildKey(request, rateLimit);
|
||||
int limit = rateLimit.limit() > 0 ? rateLimit.limit() : rateLimitProperties.getDefaultLimit();
|
||||
int limit = rateLimit.limit() > 0 ? rateLimit.limit() : rateLimitProperties.getLimit();
|
||||
long windowMs = rateLimit.window() > 0
|
||||
? rateLimit.window() * 1000L
|
||||
: rateLimitProperties.getDefaultWindow().toMillis();
|
||||
: rateLimitProperties.getWindow().toMillis();
|
||||
|
||||
// limiter 内部已统一 Fail-Open:Redis 异常返回 null,按未计数放行
|
||||
Long count = rateLimiter.count(key, windowMs);
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 限流配置
|
||||
* 接口限流配置
|
||||
*
|
||||
* @author Ray.Hao
|
||||
* @since 4.3.1
|
||||
@@ -17,27 +17,10 @@ import java.time.Duration;
|
||||
@ConfigurationProperties(prefix = "rate-limit")
|
||||
public class RateLimitProperties {
|
||||
|
||||
/** 接口限流注解未指定 limit 时,窗口内允许的最大请求数 */
|
||||
private int defaultLimit = 5;
|
||||
// @RateLimit 未指定 limit 时,窗口内允许的最大请求数
|
||||
private int limit = 60;
|
||||
|
||||
/** 接口限流注解未指定 window 时使用的滑动窗口时长 */
|
||||
private Duration defaultWindow = Duration.ofSeconds(60);
|
||||
|
||||
/** IP 全局限流配置 */
|
||||
private Ip ip = new Ip();
|
||||
|
||||
@Data
|
||||
public static class Ip {
|
||||
|
||||
/** 是否启用 IP 全局限流 */
|
||||
private boolean enabled = true;
|
||||
|
||||
/** 窗口内最大请求数(默认 1000) */
|
||||
private int limit = 1000;
|
||||
|
||||
/** 滑动窗口大小,支持 60s / 1m / 500ms 等 Duration 写法(默认 60s) */
|
||||
private Duration window = Duration.ofSeconds(60);
|
||||
|
||||
}
|
||||
// @RateLimit 未指定 window 时使用的滑动窗口
|
||||
private Duration window = Duration.ofSeconds(60);
|
||||
|
||||
}
|
||||
+1
-1
@@ -42,7 +42,7 @@ public class RedisSlidingWindowRateLimiter {
|
||||
/**
|
||||
* 记录一次请求并返回窗口内计数
|
||||
*
|
||||
* @param key 限流 Key(如 {@code rate_limit:ip:192.168.1.1})
|
||||
* @param key 限流 Key(如 {@code rate_limit:api:{user}:{uri}})
|
||||
* @param windowMs 窗口大小(毫秒)
|
||||
* @return 当前窗口内请求数(包含本次);Redis 异常时返回 null,调用方按 Fail-Open 放行
|
||||
*/
|
||||
|
||||
@@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 系统健康检查接口,返回 ok
|
||||
* 不标注 @RateLimit,仅受 IP 全局限流层约束,用作该层的验证端点
|
||||
*/
|
||||
@Tag(name = "00.系统健康检查")
|
||||
@RestController
|
||||
|
||||
Reference in new issue
Block a user