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

相关推荐
xiaoqiMikko1 分钟前
Dependabot 面板全绿,不代表你的 Tomcat 没洞
java·spring boot
前端开发张小七16 分钟前
Java 学习笔记 · 第三课:多线程与并发编程(线程、同步、死锁、Lock、乐观锁与悲观锁)
java·后端·程序员
花生了什么事o1 小时前
JVM 垃圾回收:对象如何被判定和回收
java·jvm
evans在进步1 小时前
HashMap 为什么线程不安全?ConcurrentHashMap 如何解决?
java·spring boot·spring
我命由我123451 小时前
匈牙利命名法
java·服务器·后端·学习·java-ee·kotlin·学习方法
闲猫1 小时前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain
小田的博客1 小时前
SAP MM 供应商银行主数据更新报错!message R1228!
android·java·服务器
范什么特西2 小时前
知识总结03
java
空谷有来人3 小时前
Ubuntu22.04安装jdk17,配置环境变量
java·jdk