spring cloud alibaba gateway nacos 503错误代码
Posted fhspringcloud
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud alibaba gateway nacos 503错误代码相关的知识,希望对你有一定的参考价值。
POM文件依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我在父组件中加入的nacos的相关依赖如下:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>$nacos.version</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>$nacos.version</version>
</dependency>
nacos的版本如下:
<properties>
<nacos.version>2021.1</nacos.version>
</properties>
gateway的application.yml文件配置如下:
server:
port: 9040
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: consumer
uri: lb://consumer
# uri: http://localhost:9010
predicates:
- Path=/**
nacos:
discovery:
server-addr: localhost:8848
metadata:
preserved.heart.beat.interval: 3 #心跳间隔。时间单位:秒。心跳间隔
preserved.heart.beat.timeout: 6 #心跳暂停。时间单位:秒。 即服务端6秒收不到客户端心跳,会将该客户端注册的实例设为不健康:
preserved.ip.delete.timeout: 9 #Ip删除超时。时间单位:秒。即服务端9秒收不到客户端心跳,会将该客户端注册的实例删除:
当我通过uri: localhost:9010去调用服务时,是可以调用的,但是当我用uri lb://consumer时就无法调用服务,报错503.
解决办法是:(实例项目fhadmin.cn)
加入feign依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.2</version>
</dependency>
<!--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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
猜测原因:nacos兼容feign,feign集成ribbon,默认实现负载均衡;或许是nacos不兼容springcloud gateway自带的ribbon。
以上是关于spring cloud alibaba gateway nacos 503错误代码的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud Alibaba系列教程——Spring Cloud Alibaba开篇
spring boot 整合spring cloud alibaba
spring boot 整合spring cloud alibaba
深度剖析Spring Cloud Alibaba系列——如何兼容Spring Cloud
Spring Cloud Alibaba Sentinel集成
五分钟带你玩转spring cloud alibaba越玩越溜!实战Spring Cloud Alibaba Sentinel