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

相关推荐
技术管理修行12 分钟前
【二】主流架构模式深度对比:单体、前后端分离与微服务
微服务·云原生·架构·服务发现·前后端分离·单体架构
一眼万年042 小时前
Redis主从模式
redis·微服务
2401_853275735 小时前
对微服务的了解
微服务·云原生·架构
在未来等你6 小时前
Java并发编程实战 Day 26:消息队列在并发系统中的应用
微服务·kafka·消息队列·rabbitmq·并发编程·高并发系统·: java
Mr_hwt_1238 小时前
基于nacos和gateway搭建微服务管理平台详细教程
java·spring boot·spring cloud·微服务·nacos
掘金-我是哪吒9 小时前
分布式微服务系统架构第148集:JavaPlus技术文档平台日更
分布式·微服务·云原生·架构·系统架构
chanalbert10 小时前
SpringBoot Starter设计:依赖管理的革命
spring boot·spring·spring cloud
chanalbert1 天前
SpringBoot设计基石:约定优于配置与模块化架构
spring boot·spring·spring cloud
在未来等你2 天前
互联网大厂Java求职面试:云原生架构与微服务设计中的复杂挑战
java·微服务·ai·云原生·秒杀系统·rag·分布式系统
zfj3212 天前
什么时候使用微服务,什么时候不用
微服务·架构