mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-14 16:07:32 +08:00
fix(common-redis): RateLimiterAspect 兼容非 HTTP 线程(gRPC)
getCombineKey 原实现直接调用 ServletUtils.getRequest().getRequestURI(),gRPC 的 grpc-default-executor 线程无请求上下文导致 NPE,被切面包装为"服务器限流异常", 使 @RateLimiter 注解在 gRPC 场景每次调用都失败(HealthBeatHandler 健康上报 因此被静默吞掉,常驻任务被健康判定误杀)。修复:request 为 null 时跳过 URI/IP 段,仅以注解 key 限流;HTTP 线程行为不变。注意:合并 RuoYi-Vue-Plus 上游时 本框架文件需保留此修复
This commit is contained in:
parent
9fd1d56831
commit
9f22638949
@ -1,5 +1,6 @@
|
||||
package org.dromara.common.redis.aspectj;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@ -112,8 +113,14 @@ public class RateLimiterAspect {
|
||||
key = expression.getValue(context, String.class);
|
||||
}
|
||||
StringBuilder stringBuffer = new StringBuilder(GlobalConstants.RATE_LIMIT_KEY);
|
||||
stringBuffer.append(ServletUtils.getRequest().getRequestURI()).append(StringUtils.COLON);
|
||||
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(StringUtils.COLON);
|
||||
}
|
||||
if (rateLimiter.limitType() == LimitType.IP && request != null) {
|
||||
// 获取请求ip
|
||||
stringBuffer.append(ServletUtils.getClientIP()).append(StringUtils.COLON);
|
||||
} else if (rateLimiter.limitType() == LimitType.CLUSTER) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user