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

Spring Cloud API Gateway路由失效问题求助

解决Spring Cloud Gateway路由返回404问题

核心问题:YAML配置格式错误

你的Gateway配置文件存在多处语法和缩进问题,这是导致路由不生效的主要原因:

  1. cloud节点应隶属于spring节点,而非平级
  2. 路由配置的-id需改为- id(短横线后加空格)
  3. predicates下的-Path需改为- Path(短横线后加空格)
  4. uri末尾的斜杠可移除,不影响功能但更规范

修复后的完整配置:

server:
  port: 9191
spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      routes:
        - id: USER-SERVICE
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: localhost

额外检查项

  • 确认User Service在Eureka上注册的服务名完全等于USER-SERVICE(Spring Cloud服务名大小写敏感)
  • 检查Gateway的pom.xml是否引入了必需依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  • 验证User Service的接口路径:比如User Service提供的接口是/user/get/1,通过网关访问的路径应为http://localhost:9191/user/get/1,确保路径前缀匹配/user/**规则
  • 登录Eureka控制台(http://localhost:8761),确认API-GATEWAY和USER-SERVICE均已成功注册,且Gateway的fetch-registry配置为true以拉取服务列表

内容的提问来源于stack exchange,提问作者MatildaEliz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 22:25:00