spring.cloud.gateway.discovery.locator.enabled配置项分析

spring.cloud.gateway.discovery.locator.enabled

该配置默认为false,此配置不支持动态配置,修改后需要重启gateway生效。

开启了spring.cloud.gateway.discovery.locator.enable=true这项配置后,gateway会为每个服务创建一个默认的路由规则,将以服务名开头的请求路径转发到对应的服务。

如果你在配置文件中添加了该服务的其他规则,则两个规则会同时存在,并且都会生效。

相当于为每个服务默认增加了如下配置

复制代码
- id: authority-center
  uri: lb://authority-center
  predicates:
    - Path=/authority-center/**
  filters:
    - StripPrefix=1

示例1

gateway配置文件如下

复制代码
server:
  port: 9001
    
spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true

微服务配置如下

复制代码
server:
  port: 7000
  servlet:
    context-path: /center

spring:
  application:
    name: authority-center

访问url:http://127.0.0.1:9001/authority-center/center/authority/token

这时候无需其他配置,通过网关可以访问到authority-center微服务的/authority/token接口

示例2

复制代码
spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        - id: e-commerce-authority-center
          uri: lb://authority-center
          predicates:
            - Path=/center/**

微服务配置如下

复制代码
server:
  port: 7000
  servlet:
    context-path: /center

spring:
  application:
    name: authority-center

访问url-1:http://127.0.0.1:9001/center/authority/token

访问url-2:http://127.0.0.1:9001/authority-center/authority/token

这时候访问url-1、url-2可以正常访问

spring.cloud.gateway.discovery.locator.lowerCaseServiceId

该配置是针对自动生成路由的配置,

即spring.cloud.gateway.discovery.locator.enabled=true时自动生成的路由配置有效,对于手动配置的路由不生效

比如服务名配置为 AUTHORITY-CENTER大写的服服务名,如果想访问到,需要使用如下路径

http://127.0.0.1:9001/AUTHORITY-CENTER/center/authority/token

如果配置了

spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true,则可以使用如下的方式访问

http://127.0.0.1:9001/authority-center/center/authority/token

相关推荐
喵个咪1 天前
RushWind Shop — 一笔订单的穿越:电商买路径上的每一道闸门
后端·微服务·rust
月光船幽幽1 天前
基线漂移破解:微服务测试一致性方案
python·科技·微服务
方便面不加香菜2 天前
Docker Compose 文件 -- docker-compose.yml
spring cloud·docker·容器
威嵌神州2 天前
NXP P2020核心板技术解析:PowerPC架构与VxWorks存量项目迁移指南
嵌入式硬件·微服务·云原生·架构
真上帝的左手3 天前
31. 占位-14
分布式·spring cloud·架构·系统架构
captain3764 天前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
行百里er5 天前
【Spring Insight 技术分享】如何让 AI 解释失败也不拖垮控制台
后端·spring cloud·开源
YOU OU6 天前
项目实战知识点—在线oj项目
微服务
行百里er6 天前
【Spring Insight 技术分享】如何给 AI 接口调用做滑动窗口限流
spring boot·后端·spring cloud
万里侯6 天前
日志聚类之后别把 noise 扔掉:降噪应该改变视图,不抹掉证据
人工智能·微服务·ai·容器·k8s