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

相关推荐
Lionel_SSL2 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
记得开心一点嘛2 小时前
手搓Springboot
java·spring boot·spring
老华带你飞2 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
独行soc2 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
脑子慢且灵2 小时前
[JavaWeb]模拟一个简易的Tomcat服务(Servlet注解)
java·后端·servlet·tomcat·intellij-idea·web
华仔啊3 小时前
SpringBoot 中 6 种数据脱敏方案,第 5 种太强了,支持深度递归!
java·后端
异常驯兽师4 小时前
Spring 中处理 HTTP 请求参数注解全解析
java·spring·http
连合机器人5 小时前
晨曦中的守望者:当科技为景区赋予温度
java·前端·科技
AD钙奶-lalala5 小时前
idea新建的项目new 没有java class选项
java·ide·intellij-idea
sheji34165 小时前
【开题答辩全过程】以 12306候补购票服务系统为例,包含答辩的问题和答案
java·eclipse