详解Spring Cloud Gateway应用1之谓词

系统
客户端调用spring cloud gateway配置的请求。根据配置的映射确定请求与路由,将其发送到网关Web处理程序。

[[375771]]

 环境:springboot2.3.7 + spring cloud Hoxton.SR9

Spring Cloud Gateway工作流程:

Spring Cloud Gateway应用详解1之谓词

客户端调用spring cloud gateway配置的请求。根据配置的映射确定请求与路由,将其发送到网关Web处理程序。此处理程序通过特定于请求的过滤器运行请求。过滤器被虚线分割的原因是,过滤器可以在代理请求发送之前和之后运行逻辑。执行所有“pre”过滤器逻辑。然后发出代理请求。在发出代理请求之后,运行“post”过滤器逻辑。

一. 路由谓词

1.After 路由谓词工厂

作用:After 谓词工厂接受一个参数datetime(它是java ZonedDateTime)。此谓词匹配在指定日期时间之后发生的请求。以下示例配置After Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: after_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - After=2021-01-10T17:42:47.789-07:00   

 所有的请求必须在2021-01-10T17:42:47.789-07:00之后才能访问。当超过当前配置的这个时间,请求会返回404。

测试:

当前时间:2021-01-11 17:09

After=2021-01-10T17:42:47.789-07:00


正常调整到了页面:

设置:

After=2021-01-20T17:42:47.789-07:00

 

 

2.Before 路由谓词工厂

作用:Before 谓词工厂接受一个参数datetime(它是java ZonedDateTime)。此谓词匹配在指定日期时间之前发生的请求。以下示例配置Before Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: before_route 
  11.         uri: https://www.qq.com 
  12.         predicates: 
  13.         - Before=2021-01-10T17:42:47.789-07:00 

 运行结果与After正好相反。

3.Between 路由谓词工厂

作用:Between谓词工厂接受两个参数datetime(它是java ZonedDateTime)。此谓词匹配请求必须在两个日期之间发生。以下示例配置Between Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: between_route 
  11.         uri: https://www.163.com 
  12.         predicates: 
  13.         - Between=2021-01-01T17:42:47, 2021-01-10T17:42:47 

 请求必须位于2021-01-01T17:42:47到2021-01-10T17:42:47之间,如果超过这个日期范围返回

404。

失败:

配置:Between=2021-01-01T17:42:47, 2021-01-10T17:42:47

 

 

4.Cookie 路由谓词工厂

作用:只有网关请求中包含指定的cookie 才能匹配,否则404。以下示例配置Cookie Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: cookie_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - Cookie=code,testiptv255 

 当请求header中包含的Cookie中包含name为code,值为testiptv255时,请求才能被匹配。

示例:

 

 

当不包含name为code的cookie时请求返回404。

5.Header 路由谓词工厂

作用:当请求中包含指定的Header信息时,请求才能匹配。以下示例配置Header Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: header_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - Header=Host,localhost:20000 

 当网关请求Header中包含Host请求头,并且值为localhost:20000,请求才会被匹配。

示例:

 

 

6.Host 路由谓词工厂

作用:当请求Header中的Host头信息为配置中的列表中,那么网关将被匹配。以下示例配置Host Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: host_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - Host=**.xg.com,localhost:20000 

 当请求Header中的Host取值为:www.xg.com或者xxx.xg.com或者localhost:20000时路由将被匹配:

示例:

 

 

7.Method 路由谓词工厂

作用:当请求是指定的Method时,路由将被匹配。以下示例配置Method Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: method_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - Method=GET,POST 

 当请求是GET或者是POST请求时,网关路由将会被匹配。

示例:


8.Path 路由谓词工厂

作用:当请求的路径为指定的Path时,路由将会被匹配。以下示例配置Path Route谓词:

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: path_route 
  11.         uri: https://www.baidu.com 
  12.         predicates: 
  13.         - Path=/api-a/{segment},/api-1/{segment} 

 当请求的地址为:http://xxx/api-a/xxx或者是http://xxx/api-1/xxx时,路由将会被匹配;注意:你的目标地址应该有/api-a/xxx和/api-1/xxx这两个请求地址。

示例:

Spring Cloud Gateway应用详解1之谓词

页面显示的404是因为http://www.baidu.com/api-a/1没有这样的请求地址是baidu返回的404,并不是我们服务器返回的404错误。

9.Query 路由谓词工厂

作用:当请求中包含指定的查询参数或者是包含指定的查询参数及值时才匹配路由。以下示例配置Query Route谓词:

  1. 1、方式1: 
  2. spring: 
  3.   cloud: 
  4.     gateway: 
  5.       enabled: true 
  6.       discovery: 
  7.         locator: 
  8.           enabled: true 
  9.           lowerCaseServiceId: true 
  10.       routes: 
  11.       - id: query_route 
  12.         uri: https://www.baidu.com 
  13.         predicates: 
  14.         - Query=name 
  15. 2、方式2: 
  16. spring: 
  17.   cloud: 
  18.     gateway: 
  19.       enabled: true 
  20.       discovery: 
  21.         locator: 
  22.           enabled: true 
  23.           lowerCaseServiceId: true 
  24.       routes: 
  25.       - id: query_route 
  26.         uri: https://www.baidu.com 
  27.         predicates: 
  28.         - Query=name,admin 

 方式1:当请求中只要包含name参数即可。

Spring Cloud Gateway应用详解1之谓词

方式2:当请求中包含name参数名并且值为admin时匹配路由。

Spring Cloud Gateway应用详解1之谓词

当值非admin时返回404。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-01-14 08:13:39

Spring Clou应用内置过滤器

2024-04-03 08:08:15

谓词网关开发

2023-02-15 08:12:19

http超时过滤器

2023-05-04 08:09:33

serviceId路径谓词中心注册

2023-03-08 09:03:55

2023-01-26 01:41:27

核心全局过滤器

2023-04-14 09:01:25

2023-02-20 10:13:00

灰度发布实现

2017-07-03 08:29:42

Spring Clou服务详解

2024-01-29 08:00:00

架构微服务开发

2019-08-22 09:55:17

RedisAPI数据

2022-04-11 07:34:46

OAuth2UAA节点

2021-11-04 10:11:02

Sentinel网关限流

2017-07-11 14:48:33

Spring Clou服务提供者

2017-08-18 15:14:04

Spring Clou服务消费者

2023-11-28 08:36:16

Spring中Body读取

2017-09-20 09:46:38

Spring BootSpring Clou内存

2022-08-02 08:32:21

Spring项目网关

2023-02-28 08:57:06

Spring上下线缓存

2022-08-15 09:22:12

JWT认证系统
点赞
收藏

51CTO技术栈公众号