Spring Cloud Gateway无法找到微服务(404 Not Found)问题求助
API Gateway 404 问题排查方案
1. 验证Eureka服务注册与发现配置
- 确认ApiGateway的
application.yml中Eureka客户端配置正确,必须开启服务拉取与注册:eureka: client: service-url: defaultZone: http://[Eureka服务器地址]:[端口]/eureka/ fetch-registry: true register-with-eureka: true - 核对Eureka Server中显示的服务名称,确保与ApiGateway路由配置里的
service-id或uri(如lb://HOTEL-SERVICE)完全一致(大小写敏感)。
2. 检查路由规则配置
- 确认路由的断言(predicates)、目标服务(uri)和过滤器(filters)配置正确,示例规范配置:
spring: cloud: gateway: routes: - id: hotel-service-route uri: lb://HOTEL-SERVICE # 对应Eureka中注册的服务名 predicates: - Path=/api/hotel/** # 匹配的请求路径 filters: - StripPrefix=1 # 若需移除前缀,确保数值与路径层级匹配 - 检查是否存在路由规则冲突,避免多个路由覆盖同一请求路径。
3. 核对依赖配置
- 确认
pom.xml中引入了正确的核心依赖,且无冲突:<!-- Spring Cloud Gateway 核心依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Eureka 客户端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> - 注意:不要同时引入
spring-boot-starter-web依赖,Gateway基于WebFlux,Web依赖会导致底层容器冲突。
4. 测试服务实例连通性
- 在ApiGateway所在机器上,直接访问Eureka注册的微服务实例地址,验证网络是否连通。
- 查看Eureka Server中服务的
status状态,必须为UP,若为DOWN说明服务实例未正常注册。
5. 分析ApiGateway日志
- 开启DEBUG级日志,追踪路由匹配与转发过程:
logging: level: org.springframework.cloud.gateway: DEBUG org.springframework.cloud.netflix.eureka: DEBUG - 从日志中确认请求是否匹配到目标路由,转发时是否出现异常信息。
6. 核对请求端口与路径
- 确认ApiGateway的监听端口与测试URL的端口一致,避免端口混淆。
- 检查请求路径是否严格匹配路由断言规则,注意斜杠、大小写等细节。
内容的提问来源于stack exchange,提问作者Pranay Sahu
相关产品推荐
相关产品推荐

