您的位置:首页 > 编程语言 > Java开发

SpringCloudGateWay系列二:Predicate谓词

2019-05-26 14:10 861 查看

 客户端向Spring Cloud Gateway发出请求。如何将请求和路由进行匹配,这个时候就用到 Predicate,它决定了请求由哪个路由处理。
 Predicate来自于java8的接口。Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与,或,非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。add–与、or–或、negate–非。
 Spring Cloud Gateway内置了许多Predicate,这些Predicate的源码都在

org.springframework.cloud.gateway.handler.predicate
包中。

1、AfterRoutePredicateFactory
 时间类型的Predicate (AfterRoutePredicateFactory BeforeRoutePredicateFactory BetweenRoutePredicateFactory),当只有满足特定时间要求的请求会进入到此predicate中,并交由router处理。

spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]

predicates:

- After=2017-01-20T17:42:47.789-07:00[America/Denver]
会被解析成PredicateDefinition对象
(name =After ,args= 2017-01-20T17:42:47.789-07:00[America/Denver])
。predicates的配置,遵循的约定大于配置的思想,这个After就是指定了它的处理类为AfterRoutePredicateFactory,同理,其他类型的predicate也遵循这个规则。

当请求的时间在这个配置的时间之后,请求会被路由到 http://example.org

 启动工程,在浏览器上访问 http://localhost:8081/,会显示 http://example.org 返回的结果,此时gateway路由到了配置的uri。如果我们将配置的时间设置到当前时之后,浏览器会显示404,此时证明没有路由到配置的uri。

2、CookieRoutePredicateFactory
 cookie类型的CookieRoutePredicateFactory,指定的cookie满足正则匹配,才会进入此router。
CookieRoute PredicateFactory需要2个参数,一个是cookie名字,另一个是值也可以是正则表达式。它用于匹配请求中,带有该名称的cookie和cookie匹配正则表达式的请求。

spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://example.org
predicates:
- Cookie=name, forezp

 在上面的配置中,请求带有cookie名为name, cookie值为forezp 的请求将都会转发到uri为 http://example.org 的地址上。
 使用curl命令进行请求,在请求中带上 cookie,会返回正确的结果,否则,请求报404错误。

$ curl -H 'Cookie:name=forezp' localhost:8081

3、HeaderRoutePredicateFactory
 HeaderRoutePredicateFactory需要2个参数,一个是header名,另外一个header值,该值可以是一个正则表达式。当此断言匹配了请求的header名和值时,断言通过,进入到router的规则中去。

spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://example.org
predicates:
- Header=X-Request-Id, \d+

 在上面的配置中,当请求的Header中有X-Request-Id的header名,且header值为数字时,请求会被路由到配置的 uri. 使用curl执行以下命令:

$ curl -H 'X-Request-Id:1' localhost:8081

 执行命令后,会正确的返回请求结果。如果在请求中没有带上X-Request-Id的header名,并且值不为数字时,请求就会报404,路由没有被正确转发。

4、HostRoutePredicateFactory
 HostRoutePredicateFactory需要一个参数即hostname,它可以使用. * 等去匹配host。这个参数会匹配请求头中的host的值,一致,则请求正确转发。

spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.org
predicates:
- Host=**.somehost.org

 在上面的配置中,请求头中含有Host为somehost.org的请求将会被路由转发转发到配置的uri。 启动工程,执行以下的curl命令,请求会返回正确的请求结果:

curl -H 'Host:www.somehost.org' localhost:8081

5、MethodRoutePredicateFactory
 MethodRoutePredicateFactory 需要一个参数,即请求的类型。比如GET类型的请求都转发到此路由。

spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://example.org
predicates:
- Method=GET

 在上面的配置中,所有的GET类型的请求都会路由转发到配置的uri。使用 curl命令模拟 get类型的请求,会得到正确的返回结果。

$ curl localhost:8081

使用 curl命令模拟 post请求,则返回404结果。

$ curl -XPOST localhost:8081

6、PathRoutePredicateFactory
 PathRoutePredicateFactory 需要一个参数: 一个spel表达式,应用匹配路径。

spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.org
predicates:
- Path=/foo/{segment}

 在上面的配置中,所有的请求路径满足/foo/{segment}的请求将会匹配并被路由,比如/foo/1 、/foo/bar的请求,将会命中匹配,并成功转发。
 使用curl模拟一个请求localhost:8081/foo/dew,执行之后会返回正确的请求结果。

$ curl localhost:8081/foo/dew

7、QueryRoutePredicateFactory
 QueryRoutePredicateFactory 需要2个参数:一个参数名和一个参数值的正则表达式。

spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://example.org
predicates:
- Query=foo, ba.

 在上面的配置文件中,配置了请求中含有参数foo,并且foo的值匹配ba.,则请求命中路由,比如一个请求中含有参数名为foo,值的为bar,能够被正确路由转发。
 模拟请求的命令如下:

$ curl localhost:8081?foo=bar

 QueryRoutePredicateFactory也可以只填一个参数,填一个参数时,则只匹配参数名,即请求的参数中含有配置的参数名,则命中路由。
 比如以下的配置中,配置了请求参数中含有参数名为foo 的参数将会被请求转发到uri为 http://example.org

spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://example.org
predicates:
- Query=foo

 总之每一种predicate都会对当前的客户端请求进行判断,是否满足当前的要求,如果满足则交给当前请求处理。如果有很多个Predicate,并且一个请求满足多个Predicate,则按照配置的顺序第一个生效。具体的可以在官网文档查看:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RC2/single/spring-cloud-gateway.html#gateway-how-it-works

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: