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】

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

相关推荐
洛克大航海7 小时前
9-SpringCloud-服务网关 Gateway-高级特性之 Filter-2
java·spring cloud·gateway·filter
洛克大航海1 天前
9-SpringCloud-服务网关 Gateway-高级特性之 Filter-1
spring·spring cloud·gateway·filter
yangmf20402 天前
如何使用 INFINI Gateway 增量迁移 ES 数据
大数据·数据库·elasticsearch·搜索引擎·gateway
非凡的世界3 天前
ThinkPHP6 集成TCP长连接 GatewayWorker
网络·网络协议·tcp/ip·gateway·thinkphp·worker·workman
戮戮3 天前
一次深入排查:Spring Cloud Gateway TCP 连接复用导致 K8s 负载均衡失效
tcp/ip·spring cloud·kubernetes·gateway·负载均衡·netty
洛克大航海5 天前
7-SpringCloud-服务网关 Gateway-高级特性 Route
java·spring cloud·gateway·route
AWS官方合作商8 天前
在AWS S3上动态自定义图片尺寸:Lambda + API Gateway无服务器解决方案
serverless·gateway·aws
青鱼入云9 天前
对比nginx、kong、apisix、zuul、gateway网关
nginx·gateway·kong
唐僧洗头爱飘柔952712 天前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
xrkhy12 天前
微服务之Gateway网关(1)
微服务·架构·gateway