POM檔案依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--gateway fhadmin.org-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.2</version>
</dependency>
<!--spring-boot fhadmin.org-->
<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>
<!--alibaba fhadmin.org-->
<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檔案配置如下:
#fhadmin.org
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: http://localhost:9010去呼叫服務時,是可以呼叫的,但是當我用uri lb://consumer時就無法呼叫服務,報錯503.
解決辦法是:
加入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元件 fhadmin.cn-->
<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。
本作品採用《CC 協議》,轉載必須註明作者和本文連結