Cursor 生成的 Spring Cloud 网关,启动就报错:旧文漏了 KeyResolver 和 Redis 依赖
旧文给了一套很完整的代码。Spring Cloud Gateway 路由、Redis 限流、Sentinel 熔断、Nacos 持久化,还有 .cursorrules。
看着像能直接上线。
按它的依赖清单搭一次,网关没进限流逻辑,先卡在启动阶段。
三个地方漏了。
第一个坑:限流器不知道按谁限
旧文的 application.yml 里写了:
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
但整篇代码没有 KeyResolver 这个 Bean。
RequestRateLimiter 要先知道“按什么维度限流”。按用户 ID、按 IP、按租户,都行,但你得给它一个 KeyResolver。不给,Spring 启动时直接报找不到 Bean。
补一个:
@Bean
public KeyResolver userKeyResolver() {
return exchange -> Mono.just(
Optional.ofNullable(exchange.getRequest().getHeaders().getFirst("X-User-Id"))
.orElseGet(() -> {
var remote = exchange.getRequest().getRemoteAddress();
return remote == null ? "unknown" : remote.getAddress().getHostAddress();
})
);
}
生产环境别只用 IP。NAT 后面一堆人共用一个出口 IP,一个人打满,整栋楼都被限流。用 X-User-Id 或 tenant_id 更稳。
第二个坑:少了 Redis 的响应式依赖
旧文的 pom.xml 注释里写了一句“需要引入 spring-boot-starter-data-redis-reactive”,但依赖列表里没有。
RequestRateLimiter 底层用 RedisRateLimiter。它需要 ReactiveRedisConnectionFactory,没有这个依赖,启动日志会告诉你找不到连接工厂。
补上:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
本地起一个 Redis 先验证:
docker run -d --name gateway-redis -p 6379:6379 redis:7-alpine
redis-cli ping
返回 PONG 再启动网关。Redis 都没起,别急着查代码。
第三个坑:注解包名换了
旧文的 GatewayConfig 用的是:
import javax.annotation.PostConstruct;
这套在 Spring Boot 2.7 能用。到 Boot 3,javax.annotation 没了,统一换成 jakarta.annotation。
不改,编译阶段就报:
package javax.annotation does not exist
改成:
import jakarta.annotation.PostConstruct;
顺带说一句,旧文的 Sentinel 依赖是 2021.0.5.0 版本线,和 Boot 3 不匹配。你要保留 Sentinel,得换 2025 的版本线。不保留,就用 Spring Cloud Circuit Breaker 或 Resilience4j。两套熔断别塞在一个网关里。
2026 年能跑的最小依赖
先别上 Sentinel 和 Nacos。把限流跑通,再谈熔断和动态规则。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.6</version>
</parent>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2025.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
application.yml 也简化:
spring:
cloud:
gateway:
routes:
- id: user-service-route
uri: http://localhost:8081
predicates:
- Path=/user-api/**
filters:
- StripPrefix=1
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
redis-rate-limiter.requestedTokens: 1
key-resolver: "#{@userKeyResolver}"
data:
redis:
host: localhost
port: 6379
key-resolver 里的 #{@userKeyResolver} 是 SpEL,指向上面那个 Bean 名。名字写错,启动还是失败。
跑起来之后怎么验
先起一个下游服务,随便返回 200。然后打 30 次:
for i in $(seq 1 30); do
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/user-api/1
done | sort | uniq -c
按 10 QPS、burst 20 的配置,前 20 个左右是 200,后面开始出 429。
如果全是 200,先查 key-resolver 有没有生效。如果全 429,查 Redis 里有没有 key:
redis-cli keys '*'
正常会看到 request_rate_limiter.{...} 之类的键。
旧文的规则文件也该换了
旧文让 Cursor 读项目根目录的 .cursorrules。2026 年 Cursor 走 .cursor/rules 目录。单个规则文件用 .mdc,还要写 frontmatter。
比如 rules/gateway.mdc:
---
description: Spring Cloud Gateway 代码规则
globs:
- "**/*.java"
- "**/*.yml"
alwaysApply: false
---
- 使用 jakarta.annotation,不用 javax.annotation
- RequestRateLimiter 必须同时提供 KeyResolver
- Redis 配置写在 spring.data.redis 下
- 限流阈值从配置读取,不写死在代码里
这样 Cursor 才会在改 Java 和 yml 时带上这些约束。旧文件放根目录,新版很可能不读。
旧文最危险的一句话
旧文总结说“AI 生成的代码通常符合框架的标准用法,避免了手动编写时的配置错误”。
这句话只对了一半。
AI 很会把代码写“完整”,但不一定写“能启动”。依赖、Bean、命名空间、版本线,这些运行时约束它经常漏。
Cursor 适合生成骨架,不适合替你验证。生成完先跑一次 mvn spring-boot:run,把启动日志看完,再谈限流。
我还没压测 Redis 单点在 1000 QPS 下会不会成瓶颈。下一步把单点换 cluster,再补一组连接池和超时的数。
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐
所有评论(0)