1、是什么

2、Route Predicate Factories这个是什么?

gateway启动时打印的信息
image.png

Spring Cloud Gateway 将路由匹配作为Spring WebFlux Handler Mapping基础架构的一部分。
Spring Cloud Gateway 包括许多内置的Route Predicate 工厂,所有的这些Predicate都和Http请求的不同属性匹配,多个Route Predicate可以进行组合。

Spring Cloud Gateway 创建route对象时,使用RoutePredicateFactory创建Predicate对象,Predicate对象可以赋值给Route,SpringCloud Gateway包含许多内置的Route Predicate Factories.

所有的 这些谓词都匹配Http的请求的各种属性,多种谓词工厂可以组合,并通过逻辑and

官网对gateway的断言每个都写了栗子:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#the-after-route-predicate-factory

3、常用的Route Predicate

常用的Route Predicate

After Route Predicate**

  • - After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
  • 匹配该断言时间之后的 uri请求

Before Route Predicate

  • - After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]

- Before=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]

Between Route Predicate

  • - Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] , 2020-03-08T10:59:34.102+08:00[Asia/Shanghai]

Cookie Route Predicate
不带cookies访问
带上cookies访问 - Cookie=username,atguigu #并且Cookie是username=zhangshuai才能访问
Cookie Route Predicate 需要两个参数,一个时Cookie name,一个是正则表达式。
路由规则会通过获取对应的Cookie name值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上就不执行
Header Route Predicate
两个参数:一个是属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行;

Host Route Predicate

  • - Host=**.atguigu.com

Method Route Predicate

  • - Method=GET

Path Route Predicate

Query Route Predicate**

  • - Query=username, \d+ #要有参数名称并且是正整数才能路由

<br />
<br />