Spring Cloud Gateway异常:No servers available for service: ms-authentication-server
Spring Cloud Gateway报错「No servers available for service: ms-authentication-server」解决方法
问题场景
本地运行微服务集群时,通过Spring Cloud Gateway访问ms-authentication-server服务时触发报错:No servers available for service: ms-authentication-server。直接访问该服务的URL可正常响应,将网关路由的uri从负载均衡格式lb://ms-authentication-server改为具体的http://localhost:...后也能正常工作,但负载均衡方式访问始终失败。Eureka控制台未显示相关错误信息。
相关配置信息
网关服务器配置
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <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> <version>4.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.yml
server: port: 8085 spring: application: name: ms-gateway-server cloud: gateway: routes: - id: ms-authentication-server uri: lb://ms-authentication-server predicates: - Path=/api/v1/** filters: - StripPrefix=2 loadbalancer: ribbon: enabled: false eureka: client: registerWithEureka: true fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: preferIpAddress: true leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30
Eureka服务器pom.xml配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> <relativePath/> </parent> <groupId>com.cristhianvg</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> <spring-cloud.version>2023.0.0</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
使用版本
- Spring Boot 3.2.1
- Spring Cloud 4.1.0 (2023.0.0)
修复方案
问题核心原因是目标微服务ms-authentication-server配置了独立的上下文路径,导致网关通过负载均衡标识访问时,无法正确匹配服务的实际路径。调整该微服务的上下文路径配置即可解决问题,具体可选择两种方式:
- 移除目标服务的上下文路径配置(如删除
server.servlet.context-path或server.web.context-path配置项); - 保留上下文路径的同时,修改网关路由的
uri为lb://ms-authentication-server/[上下文路径],并同步调整StripPrefix等过滤器参数适配路径规则。
内容的提问来源于stack exchange,提问作者Cristhian Villegas García
相关产品推荐
相关产品推荐

