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

相关推荐
不良手残4 分钟前
MyBatisPlus代码自动生成器
java
SamDeepThinking7 分钟前
一次线程池线上故障复盘:四层防线如何避免数据丢失
java·后端·程序员
花生智源20 分钟前
Java实现Prompt工程的技巧——从模板到工程化,告别字符串拼接
java·人工智能
折哥的程序人生 · 物流技术专研23 分钟前
Java 23 种设计模式:从踩坑到精通 | 番外:策略 vs 模板方法 —— 组合与继承的终极对决
java·策略模式·java面试·comparator·模版方法模式·java设计模式·从踩坑到精通
库克克30 分钟前
【C++】类和对象--this指针详解
java·开发语言·c++
SL_staff43 分钟前
JVS低代码完整源码解析:如何二次开发自己的表单组件?
java·低代码·vuex
QN1幻化引擎1 小时前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法
一生有你20201 小时前
Spring AI ChatMemory 监控与排查:从指标到排错全链路
java·人工智能·spring
程序员黎剑1 小时前
我把算卦变成了一段if-else
java·vue.js·spring boot·后端·ai编程
头茬韭菜1 小时前
4.1-4.2 工具注册 — Fail-Closed 类型安全设计与四层工具架构
java·安全·架构