Gateway内置断言(Predicate)类型的 yaml 和 json 数据格式
创始人
2024-05-16 11:34:53

Gateway 内置 Predicate 格式配置

概述

本文针对 Spring Cloud Gateway 框架中断言(Predicate)类型在 Yaml 文件和以 JSON 数据格式中的配置进行整理。其中 JSON 格式数据类型在动态路由时需使用到,如动态读取数据库中保存的 JSON 格式的路由数据。

类型

Path

  • Path表示请求路径正则匹配
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: path_route  uri: http://127.0.0.1:8080  predicates:  - Path=/server-a/**,/server-b/**
  • 动态路由的JSON格式
{  "id": "path_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Path",  "args": {   "_genkey_0": "/server-a/**",  "_genkey_1": "/server-b/**"  }  }  ]  
}

tips:其中args中值对应的key,即_genkey_0可以任意定义值,查看源码读取该配置可知,源码通过args.values()获取配置的所有值,所以此处key的命名可任意命名。

Method

  • Method表示匹配指定的请求方式
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: method_route  uri: http://127.0.0.1:8082  predicates:  - Method=GET,POST
  • 动态路由的JSON格式
{  "id": "method_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Method",  "args": {   "_genkey_0": "GET",  "_genkey_1": "POST"}  ]  
}

tips:此类型args中的keyPath类型一样,可任意命名。

After

  • After表示路由在指定时间之后生效
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: after_route  uri: http://127.0.0.1:8082  predicates:- After=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
  • 动态路由的JSON格式
{  "id": "after_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "After",  "args": {  "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"  }  }  ]  
}

tipsAfter类型中,args对应值的key只能为datetime,此处可在源码AfterRouterPredicateFactory类中找到。

Before

  • Before表示路由在指定时间之前有效
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: before_route  uri: http://127.0.0.1:8080  predicates:  - Before=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
  • 动态路由的JSON格式
{  "id": "before_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Before",  "args": {  "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"  }  }  ]  
}

tipsBefore类型中,args对应值的key只能为datetime,此处可在源码BeforeRouterPredicateFactory类中找到。

Between

  • Between表示路由在指定时间之间有效
  • Yaml配置文件
spring:  application:  name: hello-gateway  cloud:  gateway:  routes:  - id: between_route  uri: http://127.0.0.1:8082  predicates:  - Between=2021-08-16T07:36:00.000+08:00[Asia/Shanghai], 2021-08-16T08:15:00.000+08:00[Asia/Shanghai]
  • 动态路由的JSON格式
{  "id": "path_route_addr",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Between",  "args": {  "datetime1": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]",  "datetime2": "2021-08-16T08:18:00.000+08:00[Asia/Shanghai]"  }  }  ]  
}

tipsBetween类型中,args对应值的key只能为datetime1datetime2,此处可在源码BetweenRouterPredicateFactory类中找到。

Cookie

  • Cookie表示cookie中存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: cookie_route  uri: https://127.0.0.1:8080predicates:  - Cookie=session, fly
  • 动态路由的JSON格式
{  "id": "cookie_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Cookie",  "args": {  "name": "session",  "regexp": "fly"  }  }  ]  
}

Header

  • Header表示header中存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: header_route  uri: https://example.org  predicates:  - Header=X-Request-Id, \d+
  • 动态路由的JSON格式
{  "id": "header_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Header",  "args": {  "header": "X-Request-Id",  "regexp": "\\d+"  }  }  ]  
}

Host

  • Host表示请求的host要和指定的字符串匹配,并且对应的值符合指定正则表达式,才算匹配成功,可以同时指定多个host匹配表达式,下面的例子给了两个,其中第一个指定了端口:
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: host_route  uri: http://127.0.0.1:8082  predicates:  - Host=localhost:8080,localhost:8081
  • 动态路由的JSON格式
{  "id": "header_route",  "uri": "http://127.0.0.1:8080",  "predicates":[  {  "name": "Host",  "args": {  "_genkey_0": "localhost:8080","_genkey_0": "localhost:8081"}  }  ]  
}

Query

  • Query表示在请求中要带有指定的参数,且该参数的值需等于配置的值或匹配正则表达式,才算匹配成功
  • Yaml配置文件
spring:  cloud:  gateway:  routes:- id: query_route  uri: http://127.0.0.1:8080 predicates:  - Query=name, fly
  • 动态路由的JSON格式
{  "id": "query_route",  "uri": "http://127.0.0.1:8082",  "predicates":[  {  "name": "Query",  "args": {   "param": "name",  "regexp": "fly"  }  }  ]  
}

RemoteAddr

  • RemoteAddr表示匹配指定来源的请求
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: remoteaddr_route  uri: http://127.0.0.1:8080predicates:  - RemoteAddr=192.168.0.1
```.
- 动态路由的`JSON`格式
```json
{  "id": "remoteaddr_route",  "uri": "http://127.0.0.1:8080",  "predicates":[  {  "name": "RemoteAddr",  "args": {   "_genkey_0": "192.168.0.1"  }  }  ]  
}

Weight

  • Weight表示按照权重将请求分发到不同的位置
  • Yaml配置文件
spring:  cloud:  gateway:  routes:  - id: weight_high  uri: http://127.0.0.1:8080  predicates:  - Weight=group1, 8
  • 动态路由的JSON格式
{  "id": "remoteaddr_route",  "uri": "http://127.0.0.1:8080",  "predicates":[  {  "name": "Weight",  "args": {   "weight.group": "group1",  "weight.weight":"8"  }}  ]  
}

总结

综上所述,只有四种类型的args值的key可任意命名,分别为PathMethodHostRemoteAddr四种类型,其余类型的key命名固定。

以上即是 gateway 网关断言所有类型的 yaml 和 json 格式数据格式,希望对有需要的小伙伴有所帮助。

相关内容

热门资讯

卢卡申科:中国做事不急于求成,... 【环球网报道 记者 赵建东】据白俄罗斯通讯社报道,白俄罗斯总统卢卡申科当地时间1月27日在明斯克主持...
本应帮助无家可归者,美国加州慈... 转自:扬子晚报美国加州洛杉矶为无家可归者服务的一个慈善组织CEO亚历山大·索弗(Alexander ...
福州港后铁路有新进展!敖江特大... 福州新闻网1月27日讯(记者 傅亦静)记者从福州港后铁路公司获悉,日前,随着最后一根钻孔灌注桩完成混...
国际现代五项联盟允许俄青少年运... △俄罗斯体育部部长米哈伊尔·杰格佳廖夫(资料图)当地时间1月27日,俄罗斯体育部部长米哈伊尔·杰格佳...