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

相关推荐
4***99745 小时前
后端在微服务中的Spring Cloud Gateway
java·微服务·架构
是垚不是土5 小时前
轻量化CICD落地:基于Jenkins与Supervisor的中小企业服务发布实践
运维·servlet·ci/cd·微服务·jenkins
杀死那个蝈坦5 小时前
Caffeine
java·jvm·spring cloud·tomcat
w***H6506 小时前
SpringCloud-持久层框架MyBatis Plus的使用与原理详解
spring·spring cloud·mybatis
拾忆,想起6 小时前
Dubbo动态服务发现配置指南:从基础到云原生实践
服务器·网络·微服务·云原生·架构·服务发现·dubbo
k***08297 小时前
Gateway Timeout504 网关超时的完美解决方法
gateway
GEM的左耳返8 小时前
Java面试实战:从Spring Boot到AI集成的技术深度挑战
spring boot·redis·微服务·kafka·java面试·spring ai·缓存优化
Hernon8 小时前
微服务架构设计:从零打造互联网车贷系统 —— 业务背景与架构蓝图
java·微服务·架构·微服务架构设计
Mr.朱鹏8 小时前
RocketMQ可视化监控与管理
java·spring boot·spring·spring cloud·maven·intellij-idea·rocketmq
小毅&Nora9 小时前
【微服务】【部署】 ② 优雅停机 - 从“关门打烊“到“无缝交接“的实战指南
微服务·云原生·架构