fix(common-ratelimiter): RateLimiterAspect 兼容非 HTTP 线程(gRPC)

5.X 分支限流切面位于 ruoyi-common-ratelimiter 模块,getCombineKey 与 6.X 一样
直接调用 ServletUtils.getRequest().getRequestURI(),非 HTTP 线程(如 gRPC 的
grpc-default-executor)无请求上下文导致 NPE,被切面包装为“服务器限流异常”,
使 @RateLimiter 注解在 gRPC 场景每次调用都失败。修复:request 为 null 时
跳过 URI/IP 段,仅以注解 key 限流;HTTP 线程行为不变。
This commit is contained in:
Angell 2026-09-09 13:33:27 +08:00
parent 815087e733
commit 13f00d1c0d

View File

@ -1,5 +1,6 @@
package org.dromara.common.ratelimiter.aspectj;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
@ -99,8 +100,14 @@ public class RateLimiterAspect {
key = expression.getValue(context, String.class);
}
StringBuilder stringBuffer = new StringBuilder(GlobalConstants.RATE_LIMIT_KEY);
stringBuffer.append(ServletUtils.getRequest().getRequestURI()).append(":");
if (rateLimiter.limitType() == LimitType.IP) {
// HTTP 线程gRPC消息消费异步任务等场景没有请求上下文getRequest() 返回 null
// 此处跳过 URI/IP 段仅以注解 key 限流否则 NPE 会导致被拦截方法每次调用都失败
// 业务侧表现为限流注解在非 HTTP 场景完全不可用
HttpServletRequest request = ServletUtils.getRequest();
if (request != null) {
stringBuffer.append(request.getRequestURI()).append(":");
}
if (rateLimiter.limitType() == LimitType.IP && request != null) {
// 获取请求ip
stringBuffer.append(ServletUtils.getClientIP()).append(":");
} else if (rateLimiter.limitType() == LimitType.CLUSTER) {