spring cloud gateway 配置(spring-cloud-gateway)

在Spring Cloud Gateway基础入门一(简介及基础应用)_IHai技术之家-CSDN博客中简单介绍了Spring Cloud Gateway的配置方式和开发方式,本节详细介绍其配置内容。

配置模式

1. 简化配置模式

简化配置是将路由的断言配置进行了简化,路由以id进行分组,每组配置中的匹配规则以列表方式配置,每条规则以“=”分隔,左侧是路由断言名称,右侧是此断言的参数,如下官方示例(通过Cookie值匹配转发路由):

spring:
  cloud:
    gateway:
      routes:
        - id: after_route #路由分组ID
        uri: https://example.org #转发服务URI
        predicates:
          - Cookie=mycookie,mycookievalue 

其中“Cookie=mycookie,mycookievalue”断言配置,“=”左侧 Cookie 指定使用 Cookie Route Predicate Factory,右侧 mycookie,mycookievalue 指定 Cookie 名称及值

2. 完全配置模式

完全配置模式则是相对于简化配置模式而言,断言相关配置更加完整,如下官方示例:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
          - name: Cookie #指定使用 Cookie Route Predicate Factory
            args:
              name: mycookie #指定 Cookie 名称为 mycookie
              regexp: mycookievalue #指定 mycookie 的值为 mycookievalue

路由匹配规则

Spring Cloud Gateway 自带了11种路由匹配规则(3.1.0版本),分别如下:

1、The After Route Predicate Factory

作用:在指定时间之后的请求生效

断言名称:After

参数名:datetime

参数值:包含时区的时间戳

示例(predicates配置):After=2022-01-20T17:42:47.789-07:00[Asia/Shanghai]

2、The Before Route Predicate Factory

作用:在指定时间之前的请求生效

断言名称:Before

参数名:datetime

参数值:包含时区的时间戳

示例(predicates配置):After=2022-01-20T17:42:47.789-07:00[Asia/Shanghai]

3、The Between Route Predicate Factory

作用:在指定时间范围内的请求生效

断言名称:Between

参数名:datetime

参数值:包含时区的时间戳

示例(predicates配置):Between=2022-01-20T17:42:47.789-07:00[Asia/Shanghai],2023-01-20T17:42:47.789-07:00[Asia/Shanghai]

4、The Cookie Route Predicate Factory

作用:根据指定的Cookie及其值进行匹配

断言名称:Cookie

参数名:name,regexp

参数值:Cookie名及相应值

示例(predicates配置):Cookie=chocolate, ch.p

5、The Header Route Predicate Factory

作用:根据指定的请求头及其值进行匹配

断言名称:Header

参数名:header,regexp

参数值:请求头及相应值

示例(predicates配置):Header=X-Request-Id, d+

6、The Host Route Predicate Factory

作用:根据域名进行匹配

断言名称:Host

参数名:patterns

参数值:需要匹配的域名,多个用逗号分隔,支持Ant风格的模式匹配

示例(predicates配置):Host=**.somehost.org,**.anotherhost.org

7、The Method Route Predicate Factory

作用:根据请求方式匹配

断言名称:Method

参数名:methods

参数值:需要匹配的请求方式,多个用逗号分隔

示例(predicates配置):Method=GET,POST

8、The Path Route Predicate Factory

作用:根据请求路径匹配

断言名称:Path

参数名:patterns, matchTrailingSlash

参数值:需要匹配的路径,支持Spring风格的模式匹配;默认matchTrailingSlash为true(后缀路径模式匹配)

示例(predicates配置):Path=/red/{segment},/blue/{segment}

|9、The Query Route Predicate Factory

作用:根据请求参数匹配

断言名称:Query

参数名:param,regexp

参数值:Query parameter名及其值(Java正则表达式,可不配置)

示例(predicates配置):(1) Query=green #Query parameter中包含green则匹配 (2) Query=red, gree. #Query parameter中包含red,其值匹配"gree."则匹配

10、The RemoteAddr Route Predicate Factory

作用:根据请求方地址匹配

断言名称:RemoteAddr

参数名:sources

参数值:请求方IP列表

示例(predicates配置):RemoteAddr=192.168.1.1/24

11、The Weight Route Predicate Factory

作用:根据权重进行路由

断言名称:Weight

参数名:group,weight

参数值:分组及权重

示例(predicates配置):Weight=group1, 8

spring cloud gateway 配置(spring-cloud-gateway)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论