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

相关推荐
A ?Charis6 小时前
记录一下_treafik使用Gateway-APi使用的细节参数
gateway
WeiLai11126 小时前
面试基础--微服务架构:如何拆分微服务、数据一致性、服务调用
java·分布式·后端·微服务·中间件·面试·架构
茶本无香8 小时前
kafka+spring cloud stream 发送接收消息
spring cloud·kafka·java-zookeeper
Swift社区8 小时前
【微服务优化】ELK日志聚合与查询性能提升实战指南
spring·elk·微服务·云原生·架构
陌殇殇12 小时前
002 SpringCloudAlibaba整合 - Feign远程调用、Loadbalancer负载均衡
java·spring cloud·微服务
m0_6754470821 小时前
Java版企电子招标采购系统源业码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
java·spring cloud·企业电子招投标系统源码·招投标系统源码
LUCIAZZZ21 小时前
EasyExcel快速入门
java·数据库·后端·mysql·spring·spring cloud·easyexcel
落落落sss21 小时前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
黄名富1 天前
Spring Cloud — 深入了解Eureka、Ribbon及Feign
分布式·spring·spring cloud·微服务·eureka·ribbon
LUCIAZZZ1 天前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking