You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Spring Cloud Gateway 4.0.6添加新路由/断言

Spring Cloud Gateway 4.0.6 添加路由/断言的正确方式

问题分析

你当前的配置在同一个路由下添加了两个Path断言,Spring Cloud Gateway中同一个路由的多个断言是**逻辑与(AND)**的关系,意味着请求必须同时匹配/api/v1/auth/register和/api/v1/auth/authenticate两个路径才会被路由,这显然不可能,所以返回404。

正确配置方式

方式1:单个Path断言中配置多个路径

在同一个Path断言里用逗号分隔多个路径,只要请求匹配其中任意一个就会触发路由:

spring.cloud.gateway.routes[0].id=regs-auth
spring.cloud.gateway.routes[0].uri=lb://regs-auth-ms
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/v1/auth/register,/api/v1/auth/authenticate

方式2:新增独立的路由条目

如果后续两个路径需要配置不同的过滤器或规则,可以单独创建路由:

# 处理注册请求的路由
spring.cloud.gateway.routes[0].id=regs-auth-register
spring.cloud.gateway.routes[0].uri=lb://regs-auth-ms
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/v1/auth/register

# 处理认证请求的路由
spring.cloud.gateway.routes[1].id=regs-auth-authenticate
spring.cloud.gateway.routes[1].uri=lb://regs-auth-ms
spring.cloud.gateway.routes[1].predicates[0]=Path=/api/v1/auth/authenticate

额外技巧(非官方文档渠道)

  • 查看Spring Cloud Gateway官方示例仓库,学习实际项目中的配置写法
  • 开启网关调试日志,添加以下配置,查看请求的路由匹配过程,快速定位问题:
logging.level.org.springframework.cloud.gateway=DEBUG
logging.level.org.springframework.http.server.reactive=DEBUG
logging.level.org.springframework.web.reactive=DEBUG
  • 加入Spring技术社区的讨论群,和其他开发者交流问题与解决方案

内容的提问来源于stack exchange,提问作者Victor-FSF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:45:04