在升级spring cloud 体系版本时候遇到org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for xxx
出现以上错误主要原因有几个:
1、spring.application.name 中不要使用下划线,只能是字母数字和横杠
对下划线很多位置是不支持的(可以形成编码规范,name不要用下划线)。
spring:
application:
name: cloud-gateway
2、针对gateway的配置是否正确
spring:
application:
name: cloud-gateway
cloud:
nacos:
discovery:
server-addr: 10.0.1.73:8848
gateway:
discovery:
locator:
# 让gateway根据注册中心找到其他服务
enabled: true
routes:
# - id: cloud-service
# uri: lb://cloud-service
# predicates:
# - Path=/*
3、服务名是否和服务注册中心一致
去对应的服务注册中心查看注册的服务名和路由规则中设置的是否一致。
4、版本及依赖导致兼容问题
Spring Cloud一直在迭代新版本,新版本很多会弃用老版本的一些特性和组件,所以在新版本中使用老版本的方式会出现各种问题,建议升级版本之前先查一下官方说明,changelog。
从Spring Cloud 2020开始弃用了Ribbon,因此Spring Cloud Alibaba在2021版本nacos中删除了Ribbon的jar包,因此无法通过lb路由到指定微服务,出现了503情况。
所以只需要引入springcloud loadbalancer包即可
<!--客户端负载均衡loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
如果你使用的是fegin客户端还得添加对应的lb依赖
<!--fegin组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.0.2</version>
</dependency>
如果使用了consul可能还要检查下是否开启了actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
最后是建议如无刚需不建议升级,升级前先查阅对应版本及依赖和changelog等官方文档,做好升级准备后再升级。
参考来源:
springcloud2020版本gateway+nacos,服务报错503 Service Unavailable_疯狂的炫的博客-CSDN
博客
nacos+spring cloud gateway 出现503 Service Unavailable_wangzibai的博客-CSDN博客
关于spring cloud gateway中可能会出现503 Source Unavailable的问题_Irrxing的博客-CSDN博客
Spring cloud GateWay 503 Service Unanailable_代码搬晕工的博客-CSDN博客
SpringCloud Gateway报500 Invalid host: lb://xxx_a_lllk的博客-CSDN博客