nacos与spring cloud gateway 一起的使用的时候uri: lb://server-name配置不起用

首先我们将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一起使用的时候要注意依赖包。

相关推荐
.格子衫.5 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多6 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
Jabes.yang8 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪8 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人8 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR228 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠9 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の9 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng10 小时前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse
聪明的笨猪猪11 小时前
Java Spring “AOP” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试