SpringCloud微服务系列04-Alibaba架构01-Gateway-02-Predicate
Route Predicate Factories
Spring Cloud Gateway 匹配路由作为 Spring WebFlux HandlerMapping 基础结构的一部分。 Spring Cloud Gateway 包括许多内置的路由谓词工厂。所有这些谓词都匹配 HTTP 请求的不同属性。可以组合多个路由谓词工厂,并通过逻辑与进行组合。
Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping
infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and
.
After Route Predicate Factory
把RouteConfig.java注释掉
AfterRoutePredicateFactory,可配置一个时间,当请求的时间在配置时间之后,才交给 router去处理。否则则报错,不通过路由。
当请求的时间在这个配置的时间之后,请求会被路由到http://httpbin:org:80/get。:
在工程的application.yml配置如下:
1 | server: |
它实际被AfterRoutePredicateFactory这个类所处理,这个After就是指定了它的Gateway web handler类为AfterRoutePredicateFactory,同理,其他类型的predicate也遵循这个规则。
测试
进入服务http://localhost:8711/
after修改时间大于当前实际时间测试
1 | # 当请求的时间在这个配置的时间之后,请求会被路由到http://httpbin:org:80/get。: |
再次测试
Before Route Predicate Factory
The Before Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen before the current datetime.
和之前那个after的原理一养,只不过时间是方向
Between Route Predicate Factory
1 | predicates: |
在这个时间区间内可分发路由
Cookie Route Predicate Factory
The Cookie Route Predicate Factory takes two parameters, the cookie name and a regular expression. This predicate matches cookies that have the given name and the value matches the regular expression.
Cookie Route Predicate Factory需要2个参数,一个时cookie名字,另一个时值,可以为正则表达式。它用于匹配请求中,带有该名称的cookie和cookie匹配正则表达式的请求。
1 | spring: |
在上面的配置中,请求带有cookie名为 name, cookie值为forezp 的请求将都会转发到uri为 http://httpbin.org:80/get的地址上。 使用curl命令进行请求,在请求中带上 cookie,会返回正确的结果,否则,请求报404错误。
当cookie匹配时转发
Header Route Predicate Factory
The Header Route Predicate Factory takes two parameters, the header name and a regular expression. This predicate matches with a header that has the given name and the value matches the regular expression.
Header Route Predicate Factory需要2个参数,一个是header名,另外一个header值,该值可以是一个正则表达式。当此断言匹配了请求的header名和值时,断言通过,进入到router的规则中去。
1 | spring: |
在上面的配置中,当请求的Header中有X-Request-Id的header名,且header值为数字时,请求会被路由到配置的 uri. 使用curl执行以下命令:
1 | C:\Users\DELL>curl -H Cookie:chocolate=ch.p http://localhost:8711/ |
执行命令后,会正确的返回请求结果,结果省略。如果在请求中没有带上X-Request-Id的header名,并且值不为数字时,请求就会报404,路由没有被正确转发。
This route matches if the request has a header named X-Request-Id
whos value matches the \d+
regular expression (has a value of one or more digits).
Host Route Predicate Factory
The Host Route Predicate Factory takes one parameter: the host name pattern. The pattern is an Ant style pattern with .
as the separator. This predicates matches the Host
header that matches the pattern.
1 | spring: |
在上面的配置中,请求头中含有Host为fangzhipeng.com的请求将会被路由转发转发到配置的uri。 启动工程,执行以下的curl命令,请求会返回正确的请求结果:
1 | curl -H 'Host:www.fangzhipeng.com' localhost:8081 |
输入curl -H Host:www.jerma.com http://localhost:8711
结果如下
1 | C:\Users\DELL>curl -H Host:www.jerma.com http://localhost:8711 |
输入curl -H Host:www.jermaine.com http://localhost:8711
后结果如下
1 | C:\Users\DELL>curl -H Host:www.jermaine.com http://localhost:8711 |
Method Route Predicate Factory
The Method Route Predicate Factory takes one parameter: the HTTP method to match.
Method Route Predicate Factory 需要一个参数,即请求的类型。比如GET类型的请求都转发到此路由。在工程的配置文件加上以下的配置:
1 |
|
curl localhost:8081
使用 curl命令模拟 post请求,则返回404结果。
curl -XPOST localhost:8081
Path Route Predicate Factory
The Path Route Predicate Factory takes one parameter: a Spring PathMatcher
pattern.
Path Route Predicate Factory 需要一个参数: 一个spel表达式,应用匹配路径。
在工程的配置文件application.yml文件中,做以下的配置:
这个是目前所有predicates中使用最频繁的:
1 |
|
Query Route Predicate Factory
The Query Route Predicate Factory takes two parameters: a required param
and an optional regexp
.
Query Route Predicate Factory 需要2个参数:一个参数名和一个参数值的正则表达式。在工程的配置文件application.yml做以下的配置:
1 |
|
curl localhost:8081?foo=bar
Query Route Predicate Factory也可以只填一个参数,填一个参数时,则只匹配参数名,即请求的参数中含有配置的参数名,则命中路由。比如以下的配置中,配置了请求参数中含有参数名为foo 的参数将会被请求转发到uri为http://httpbin.org:80/get。
1 | spring: |
RemoteAddr Route Predicate Factory
he RemoteAddr Route Predicate Factory takes a list (min size 1) of CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16
(where 192.168.0.1
is an IP address and 16
is a subnet mask).
application.yml.
1 | spring: |
This route would match if the remote address of the request was, for example, 192.168.1.10
.
还有很多Predicate Factory可以参考https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.0.RELEASE/single/spring-cloud-gateway.html#gateway-starter
总结如一下图片
引用资料
https://spring.io/guides/gs/gateway
https://www.fangzhipeng.com/springcloud/2018/11/06/sc-f-gateway1.html
https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
https://www.fangzhipeng.com/springcloud/2018/12/21/sc-f-gatway1.html
https://www.fangzhipeng.com/springcloud/2018/12/05/sc-f-gateway2.html
https://www.fangzhipeng.com/springcloud/2018/12/21/sc-f-gatway3.html
springcloud之gateway路由熔断