Spring Cloud gateway 路由规则

Spring Cloud gateway 路由规则

文章目录

  • [Spring Cloud gateway 路由规则](#Spring Cloud gateway 路由规则)
    • 一、路由常用属性解析
      • [1.1 示例配置](#1.1 示例配置)
      • [1.2 属性解析](#1.2 属性解析)
    • 二、问题分析,springCloud微服务中没有任何路由配置,网关为什么能根据请求转发到相应的业务服务的
      • [2.1 开启,用于启用通过服务发现来路由到服务的功能](#2.1 开启,用于启用通过服务发现来路由到服务的功能)
      • [2.2 断点RoutePredicateHandlerMapping类查看自动注册的路由](#2.2 断点RoutePredicateHandlerMapping类查看自动注册的路由)
      • [2.3 自动创建注册到Nacos的路由配置,包括dubbo服务](#2.3 自动创建注册到Nacos的路由配置,包括dubbo服务)
      • [2.4 未开启 【spring: cloud: gateway: discovery: locator: enabled: false】](#2.4 未开启 【spring: cloud: gateway: discovery: locator: enabled: false】)

一、路由常用属性解析

1.1 示例配置

复制代码
server:
  port: 90
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #启动DiscoveryClient,可以实现服务的发现,自动根据注册的服务创建对应的路由
      routes:
        - id: Auth
          uri: lb://wuxsms-auth
          order: -1
          predicates:
            - Path=/wuxsms-auth/**
          filters:
            - RewritePath=/wuxsms-auth/(?<segment>.*), /$\{segment}
            - StripPrefix=2

1.2 属性解析

  1. id

    1. 类型:字符串
    2. 作用:唯一标识一个路由。必须是唯一的,不能重复。
    3. 示例:
  2. uri

    1. 类型:字符串
    2. 作用:指定目标微服务的 URI。可以是具体的 URL(http://www.baidu.com等第三方系统),也可以配合nacos服务注册中心负载均衡的 URI(如 lb://service-name)。
  3. predicates

    1. 类型:列表

    2. 作用:定义一组断言,用于决定请求是否匹配该路由。每个断言都是一个条件,所有条件都满足时,请求才会被路由到目标微服务。

    3. 常见断言:

      • Path:匹配请求路径。
      • Method:匹配请求方法(GET、POST 等)。
      • Query:匹配查询参数。
      • Header:匹配请求头。
      • Cookie:匹配请求中的 Cookie。
      • Host:匹配请求主机。
    4. 匹配规则

    匹配规则 语法 示例 解释
    精确匹配 Path=/exact/path Path=/api/user 匹配请求路径为 /api/user 的请求
    单级通配符 Path=/path/* Path=/api/user/* 匹配 /api/user/ 下的一级子路径,例如 /api/user/profile
    多级通配符 Path=/path/** Path=/api/user/** 匹配 /api/user/ 下的所有子路径,包括多级子路径,例如 /api/user/profile 和 /api/user/orders/123
    正则表达式 Path=/pattern Path=/api/user/\d+ 匹配 /api/user/ 后跟一个或多个数字的路径,例如 /api/user/123
  4. filters

    1. 类型:列表

    2. 作用:定义一组过滤器,用于在请求转发前后对请求或响应进行修改。过滤器可以改变请求路径、添加请求头等。

    3. 常见过滤器:

      • StripPrefix:去掉路径前缀。

        • StripPrefix 过滤器可以用来去掉路径中的前缀部分。这个过滤器更简单,适合去掉固定数量的路径段。

          例如:

          YAML 复制代码
          spring:
            cloud:
              gateway:
                routes:
                  - id: user-service
                    uri: lb://user-service
                    predicates:
                      - Path=/api/user/**
                    filters:
                      - StripPrefix=2

          在这个配置中:

          • Path=/api/user/** 表示匹配以 /api/user/ 开头的路径。
          • StripPrefix=2 表示去掉路径中的前两个段,即 /apiuser
      • RewritePath:重写请求路径。

      • springCloud 中网关自动转发到其他服务就是使用改属性的配置

      • AddResponseHeader:添加响应头。

      • SetStatus:设置响应状态码。

      • Retry:重试机制。

  5. order

    1. 类型:整数
    2. 作用:定义路由的优先级。顺序值越小,优先级越高。默认情况下,路由是按配置顺序依次匹配的,只要匹配到就不再往下匹配。
  6. metadata

    1. 类型:键值对
    2. 作用:存储额外的元数据信息,可以在过滤器或其他组件中使用。

二、问题分析,springCloud微服务中没有任何路由配置,网关为什么能根据请求转发到相应的业务服务的

2.1 开启,用于启用通过服务发现来路由到服务的功能

YAML 复制代码
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

2.2 断点RoutePredicateHandlerMapping类查看自动注册的路由

发现有两条【lb://wuxsms-auth】服务的路由配置

为什么会有两条呢?

  1. 第一条:nacos配置中心自定义配置的一条
  2. 第二条:配置【spring: cloud: gateway: discovery: locator: enabled: true】是自动创建的一条

第一条:

第二条:

2.3 自动创建注册到Nacos的路由配置,包括dubbo服务

Plain 复制代码
onNext(Route{id='ReactiveCompositeDiscoveryClient_wuxsms-auth', uri=lb://wuxsms-auth, order=0, predicate=Paths: [/wuxsms-auth/**], match trailing slash: true, gatewayFilters=[[[RewritePath /wuxsms-auth/(?<remaining>.*) = '/${remaining}'], order = 1]], metadata={nacos.instanceId=192.168.1.111#8889#DEFAULT#DEFAULT_GROUP@@wuxsms-auth, nacos.weight=1.0, nacos.cluster=DEFAULT, nacos.healthy=true, dubbo.metadata-service.urls=[ "dubbo://192.168.1.111:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=wuxsms-auth&bind.ip=192.168.1.111&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=wuxsms-auth&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=1540&qos.enable=false&release=2.7.4.1&revision=2.2.0.RELEASE&side=provider&threads=500&timeout=6000&timestamp=1731655720770&version=1.0.0" ], preserved.register.source=SPRING_CLOUD}})
  1. predicate: /wuxsms-auth/**,配置服务名称开头的路由
  2. RewritePath : /wuxsms-auth/(?.*), /${segment} 去掉服务前缀后再转发到相应的服务
  3. order:0 ,小于nacos配置中心的优先级
  4. match trailing slash:true 匹配尾部斜杠
  5. Metadata: 元数据 (存储额外的元数据信息,可以在过滤器或其他组件中使用)

2.4 未开启 【spring: cloud: gateway: discovery: locator: enabled: false】

不开启该配置,则需要手动配置路由规则。若两者都没有配置则系统无法正常访问

相关推荐
huipeng92613 小时前
GateWay使用详解
java·spring boot·spring cloud·微服务·gateway
随风,奔跑4 天前
Spring Cloud Alibaba(四)---Spring Cloud Gateway
后端·spring·gateway
jiayong234 天前
Hermes Agent 的 Skills、Plugins、Gateway 深度解析
ai·gateway·agent·hermes agent·hermes
鬼蛟4 天前
Gateway
gateway
武超杰4 天前
Spring Cloud Gateway 从入门到实战
spring cloud·gateway
StackNoOverflow5 天前
Spring Cloud Gateway 服务网关详解
gateway
tsyjjOvO5 天前
服务网关 Gateway 从入门到精通
gateway
甜鲸鱼6 天前
JWT过滤器:从单体应用到微服务架构
微服务·架构·gateway·springcloud
notfound40436 天前
解决SpringCloudGateway用户请求超时导致日志未记录情况
java·spring boot·spring·gateway·springcloud
接着奏乐接着舞6 天前
gateway
gateway