场景
版本
JDK 1.8
Spring Boot 2.6.0
Spring Cloud 2021.0.1
Spring Cloud Alibaba 2021.0.1.0
配置
gateway 开启动态路由 spring.cloud.gateway.discovery.locator.enabled: true
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: 9b02d316-10f4-40bc-b06b-32cd0e6b3732
group: DEV_GROUP
service: ${spring.application.name}
password: nacos
username: nacos
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: demo-service
uri: lb://demo-service
predicates:
- Path=/demo-server/demo/test/gateway/yaml
问题
请求通过gateway代理的接口报错( “status”: 503,“error”: “Service Unavailable”)。
原因
- Spring Cloud 从2020版本开始弃用了Ribbon。
- Alibaba 在2020及之后版本的Nacos 中删除了Ribbon的jar包。
- 由于上述两个原因,无法通过lb路由到指定的微服务,导致出现503错误。
解决方案
Spring Cloud 2020版官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
验证
其它
公众号