H__D  

Predicate介绍

  Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础架构的一部分进行匹配Spring Cloud Gateway包括许多内置的路由断言工厂。所有这些断言都与HTTP请求的不同属性匹配。您可以将多个路由断言工厂与逻辑and语句结合使用

  官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#gateway-request-predicates-factories

  常用的Predicate:

  

 

常用的Predicate

  演示项目搭建参考:【SpringCloud】Gateway路由配置(十七),演示请求使用curl命令

  1、After Route Predicate

  所述After断言有一个参数,一个datetime(其是Java ZonedDateTime)。该断言匹配在指定日期时间之后发生的请求。下面的示例配置路由后断言:

  例子1. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: after_route
6           uri: http://localhost:80017           predicates:
8             - After=2020-04-20T23:57:57.308+08:00[Asia/Shanghai]

  这路由符合2020年4月20日23:57时区时间(上海)之后的任何请求。

  测试请求命令:curl http://localhost:9527/payment/get/1

  2、Before Route Predicate

  所述Before断言有一个参数,一个datetime(其是Java ZonedDateTime)。该断言匹配在指定之前发生的请求datetime以下示例配置了路由前断言:

  例子2. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: before_route
6           uri: http://localhost:80017           predicates:
8             - Before=2020-04-21T23:57:57.308+08:00[Asia/Shanghai]

  这路由符合2020年4月21日23:57时区时间(上海)之前的任何请求。

  3、Between Route Predicate

  该Between断言有两个参数,datetime1并且datetime2 这是Java ZonedDateTime对象。该断言匹配在之后datetime1和之前发生的请求datetime2datetime2参数必须是后datetime1以下示例配置了路由之间的断言:

  例子3. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: between_route
6           uri: http://localhost:8001
7           predicates:
8             - Between=2020-04-20T23:57:57.308+08:00[Asia/Shanghai], 2020-04-21T23:57:57.308+08:00[Asia/Shanghai]

  4、Cookie Route Predicate

  所述Cookie断言采用两个参数,该cookie nameregexp(其是Java正则表达式)。该断言匹配具有给定名称且其值与正则表达式匹配的cookie。以下示例配置Cookie路由断言:

  例子4. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: cookie_route
6           uri: http://localhost:8001
7           predicates:
8             - Cookie=username, xiaoming

  此路由匹配具有名称为usernamexiaoming正则表达式匹配的cookie的请求。

  测试请求命令:curl http://localhost:9527/payment/get/1 --cookie "username=xiaoming"    

  5、Header Route Predicate

  所述Header断言采用两个参数,报头name和一个regexp(其是Java正则表达式)。该断言与具有给定名称且其值与正则表达式匹配的标头匹配。以下示例配置标头路由断言:

  例子5. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: header_route
6           uri: http://localhost:8001
7           predicates:
8             - Header=X-Request-Id, \d+

  如果请求具有名为X-Request-Id其值与\d+正则表达式匹配的标头(即,其值为一个或多个数字),则此路由匹配

  测试请求命令:curl http://localhost:9527/payment/get/1 -H "X-Request-Id:123"

  6、Host Route Predicate

  Host断言需要一个参数:主机名的列表patterns该模式是带有.分隔符的Ant样式的模式。断言Host匹配模式标头匹配。以下示例配置主机路由断言:

  例子6. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: host_route
6           uri: http://localhost:8001
7           predicates:
8             - Host=**.x.com

  如果请求具有这种路由匹配Host用的头值**.x.com

  测试请求命令:curl http://localhost:9527/payment/get/1 -H "Host:demo1.x.com"

  7、Method Route Predicate

  所述Method断言需要methods的参数,它是一个或多个参数:HTTP方法来匹配。以下示例配置方法路由断言:

  例子7. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: method_route
6           uri: http://localhost:8001
7           predicates:
8             - Method=GET

  测试请求命令:curl http://localhost:9527/payment/get/1 -X GET

  8、Path Route Predicate

  Path断言有两个参数:春天的列表PathMatcher patterns和一个可选的标志叫matchOptionalTrailingSeparator以下示例配置路径路由断言:

  例子8. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: path_route
6           uri: http://localhost:8001
7           predicates:
8             - Path=/payment/get/**

  测试请求命令:curl http://localhost:9527/payment/get/1 

  9、Query Route Predicate

  所述Query断言采用两个参数:所要求的param和可选的regexp(其是Java正则表达式)。以下示例配置查询路由断言:

  例子9. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: query_route
6           uri: http://localhost:8001
7           predicates:
8             - Query=green

  如果请求包含green查询参数,则前面的路由匹配

  测试请求命令:curl http://localhost:9527/payment/get/1?green=1

  10、RemoteAddr Route Predicate

  所述RemoteAddr断言需要的列表sources,其是CIDR的表示法(IPv4或IPv6)的字符串,如192.168.0.1/16(其中192.168.0.1是一个IP地址和16一个子网掩码)。以下示例配置一个RemoteAddr路由谓词:

  例子10. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: query_route
6           uri: http://localhost:8001
7           predicates:
8             - RemoteAddr=192.168.1.1/24

  请求测试命令:curl http://192.168.1.4:9527/payment/get/1

  11、Weight Route Predicate

  Weight断言有两个参数:groupweight(一个int)。权重是按组计算的。以下示例配置权重路由断言:

  例子11. application.yml

 1 spring:
 2   gateway:
 3     discovery:
 4       routes:
 5         - id: weight_high
 6           uri: http://localhost:8001
 7           predicates:
 8             - Weight=group1, 8
 9         - id: weight_low
10           uri: http://localhost:8002
11           predicates:
12             - Weight=group1, 2

  这条路线会将大约80%的流量转发到http://localhost:8001,将大约20%的流量转发到http://localhost:8001

  请求测试命令:curl http://localhost:9527/payment/get/1

 

posted on 2020-04-21 01:30  H__D  阅读(4262)  评论(0编辑  收藏  举报