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

相关推荐
Java识堂4 小时前
如何对微服务进行拆分?
微服务·云原生·架构
就改了6 小时前
微服务接口性能优化:CompletableFuture 并行聚合实践
java·微服务·性能优化
2601_961845158 小时前
专升本资料哪个好|教材+真题+网课对比推荐PDF
考研·http·微服务·容器·visual studio
heimeiyingwang9 小时前
【架构实战】网关架构设计:微服务的统一入口
微服务·云原生·架构
JJJennie7779 小时前
从苹果 2026 落地场景,看系统级 Agent 时代的隐私边界与 MAI Gateway 的企业Token治理
人工智能·gateway·apple
真实的菜9 小时前
微服务架构痛点
java·微服务·架构
百珏9 小时前
流量没暴涨,网关却挂了:Spring Cloud Gateway 从 500 QPS 优化到 4200 QPS
后端·spring cloud·架构
kakawzw10 小时前
微服务组件源码6——Spring Gateway
spring·gateway
sbjdhjd10 小时前
04 (下) | K8S微服务实战:从 Service 到金丝雀发布
运维·微服务·云原生·kubernetes·开源·云计算·excel
白露与泡影10 小时前
Java 8老系统旁路接入AI Gateway:不升级JDK也能用AI
java·人工智能·gateway