首先我们将uri配置成本地地址测试服务是否可以调通
yaml
spring:
cloud:
nacos:
discovery:
server-addr: xxx.xxx.xxx.xxx:8848
gateway:
routes:
- id: angular_app
uri: http://localhost:8082
predicates:
- Path=/angular/**
filters:
- StripPrefix=1
xxx.xxx.xxx.xxx:8848 切换成你的nacos服务器地址
bash
yanghaoyuan@yanghaoyuandeMacBook-Pro ~ % curl http://localhost:8080/angular/users
[{"id":1,"name":"张三","age":23,"email":"zhangsan@good.com","isDeleted":0,"createdDate":"2024-05-06T03:43:13","lastModifiedDate":null,"createdBy":"1","lastModifiedBy":null}]%
8080端口是本地gateway的服务端口,上面表明已经可以调通。
yaml
spring:
cloud:
nacos:
discovery:
server-addr: xxx.xxx.xxx.xxx:8848
gateway:
routes:
- id: angular_app
uri: lb://user-service
predicates:
- Path=/angular/**
filters:
- StripPrefix=1
换成 lb负载均衡的方式调用调不通,初步判断应该是没有添加lb的依赖包,所以lb的方式无法获取服务列表从而无法转发。
java
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>4.0.3</version>
</dependency>
在pom中添加lb的依赖,重启服务测试
bash
yanghaoyuan@yanghaoyuandeMacBook-Pro ~ % curl http://localhost:8080/angular/users
[{"id":1,"name":"张三","age":23,"email":"zhangsan@good.com","isDeleted":0,"createdDate":"2024-05-06T03:43:13","lastModifiedDate":null,"createdBy":"1","lastModifiedBy":null}]%
ok,👌已经可以调通,nacos与spring cloud gateway一起使用的时候要注意依赖包。