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的相关依赖。

相关推荐
衣舞晨风16 小时前
HTTP/1.1 与 HTTP/2:协议差异、AI 场景优劣与网关链路设计
http·gateway·tcp·network·ai gateway
IT枫斗者19 小时前
缓存和数据库一致性:先更库还是先删缓存,延迟双删怎么落地
java·开发语言·数据库·人工智能·缓存·gateway
IT枫斗者4 天前
AI Coding 蜜月期之后,我们重新思考了 AI 提效
java·开发语言·人工智能·gateway·fastapi
顧棟5 天前
INFINI Gateway 启动过程分析
服务器·网络·gateway
Joy T10 天前
OpenClaw:个人 AI Agent 的运行架构、Skills 与工具管理
架构·gateway·plugin·channel·openclaw·agent runtime
captain37614 天前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
如夏倾城19 天前
BGP(Border Gateway Protocol 边界网关协议)
运维·服务器·网络·gateway·信息与通信
JJJennie77722 天前
MAI Gateway 免费试用与付费方案详解:先验效果,再谈 ROI
人工智能·大模型·gateway·ai网关
顧棟1 个月前
INFINI Gateway 服务器端口重用(SO_REUSEPORT)实现与分析
网络·gateway
逃逸线LOF1 个月前
服务网关Gateway
java·运维·gateway