Gateway结合nacos(lb://xxx)无效问题

Gateway结合nacos无效

版本如下:

com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:2021.0.1.0

org.springframework.cloud:spring-cloud-starter-gateway:3.1.1

配置如下:

yml 复制代码
server:
  port: 7000
spring:
  application:
    name: springCloudGateway
  cloud:
    nacos:
      discovery:
        server-addr: xxx.xxx.xxx.xx:8848
      username: xxx
      password: xxx
    gateway:
      routes:
        - id: product_route
          uri: lb://nacos-consumer
          predicates:
            - Path=/consumer/**
          filters:
            - StripPrefix=1

我测试过了,使用http://localhost:xxx的方式就可以访问

lb:和http:的区别就在于,一个是固定的访问地址,一个是使用负载均衡去访问,所以问题出来负载均衡这里

在网上找到解决方案说是添加一个loadBalancer依赖就好了

xml 复制代码
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.1.1</version>
        </dependency>

问题解决!但为什么我添加一个负载均衡依赖就好了?

我科学上网了一下找到了这个为什么会这样,如下:
在Spring Cloud Gateway 2.x及以上版本中,官方文档已经明确表示不再支持Ribbon作为默认的负载均衡器实现。因此,从这个版本开始,Spring Cloud Gateway不再提供自带的负载均衡器组件。
不过,Spring Cloud Gateway可以与Spring Cloud LoadBalancer集成来实现负载均衡的功能。Spring Cloud LoadBalancer是一个独立的模块,它提供了一个基于服务实例列表的负载均衡器实现,可以与Spring Cloud Gateway和其他Spring Cloud组件一起使用。

也就是说如果你想要在Spring Cloud Gateway中使用lb://service-name这种形式的URI来访问Nacos注册中心的服务,那么需要在依赖中引入Spring Cloud LoadBalancer的相关依赖。

相关推荐
A ?Charis1 天前
记录一下_treafik使用Gateway-APi使用的细节参数
gateway
运维开发王义杰4 天前
Kubernetes:EKS 中 Istio Ingress Gateway 负载均衡器配置及常见问题解析
kubernetes·gateway·istio
铭毅天下5 天前
极限网关 INFINI Gateway 从 0 到 1:简单易懂的入门教程
gateway
铭毅天下5 天前
极限网关核心架构解析:从 Nginx 到 INFINI Gateway 的演进
运维·nginx·架构·gateway
Xwzzz_6 天前
Spring Cloud Gateway中断言路由和过滤器的使用
java·spring cloud·gateway
zzyh1234566 天前
springcloudalibaba组件gateway
前端·javascript·gateway
@明明不知道6 天前
spring cloud 微服务部署(2025年)第一章:Nacos、LoadBalancer、GateWay、Ribbon集成之Nacos部署
spring cloud·微服务·nacos·gateway·loadbalancer
山海不说话7 天前
从零搭建微服务项目(第7章——微服务网关模块基础实现)
java·spring boot·spring·spring cloud·微服务·gateway
默辨7 天前
再谈SpringCloud Gateway源码
java·网关·spring cloud·gateway·reactor·webflux
飞火流星020277 天前
【动态路由】系统web url整合系列【springcloud-gateway实现】【不改hosts文件版】组件一:多个Eureka路由过滤器
gateway·gateway动态路由·gateway实现反向代理·系统url整合·eureka路由过滤器·gateway路由过滤器