Spring Cloud API Gateway路由失效问题求助
解决Spring Cloud Gateway路由返回404问题
核心问题:YAML配置格式错误
你的Gateway配置文件存在多处语法和缩进问题,这是导致路由不生效的主要原因:
cloud节点应隶属于spring节点,而非平级- 路由配置的
-id需改为- id(短横线后加空格) predicates下的-Path需改为- Path(短横线后加空格)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
相关产品推荐
相关产品推荐

