java B2B2C Springcloud多租户电子商城系统- Gateway 之Predict篇

predicate简介html

电子商务平台源码请加企鹅求求:一零三八七七四六二六。Predicate来自于java8的接口。Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate组合成其余复杂的逻辑(好比:与,或,非)。能够用于接口请求参数校验、判断新老数据是否有变化须要进行更新操做。add–与、or–或、negate–非。java

Spring Cloud Gateway内置了许多Predict,这些Predict的源码在org.springframework.cloud.gateway.handler.predicate包中,若是读者有兴趣能够阅读一下。web

predicate实战正则表达式

如今以案例的形式来说解predicate,本文中的案例基原本源于官方文档,官方文档地址:cloud.spring.io/spring-clou…spring

建立一个工程,在工程的pom文件引入spring cloud gateway 的起步依赖spring-cloud-starter-gateway,spring cloud版本和spring boot版本,代码以下:浏览器

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.5.RELEASE</version>
   </parent>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Finchley.SR1</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>

复制代码

After Route Predicate Factorybash

AfterRoutePredicateFactory,可配置一个时间,当请求的时间在配置时间以后,才交给 router去处理。不然则报错,不经过路由。cookie

在工程的application.yml配置以下:app

server:
  port: 8081
spring:
  profiles:
    active: after_route

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

复制代码

在上面的配置文件中,配置了服务的端口为8081,配置spring.profiles.active:after_route指定了程序的spring的启动文件为after_route文件。在application.yml再建一个配置文件,语法是三个横线,在此配置文件中经过spring.profiles来配置文件名,和spring.profiles.active一致,而后配置spring cloud gateway 相关的配置,id标签配置的是router的id,每一个router都须要一个惟一的id,uri配置的是将请求路由到哪里,本案例所有路由到http://httpbin.org:80/get。框架

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这个类所处理,这个After就是指定了它的Gateway web handler类为AfterRoutePredicateFactory,同理,其余类型的predicate也遵循这个规则。

当请求的时间在这个配置的时间以后,请求会被路由到http://httpbin.org:80/get。

启动工程,在浏览器上访问http://localhost:8081/,会显示http://httpbin.org:80/get返回的结果,此时gateway路由到了配置的uri。若是咱们将配置的时间设置到当前时以后,浏览器会显示404,此时证实没有路由到配置的uri.

跟时间相关的predicates还有Before Route Predicate Factory、Between Route Predicate Factory,读者能够自行查阅官方文档,再次再也不演示。

Header Route Predicate Factory

Header Route Predicate Factory须要2个参数,一个是header名,另一个header值,该值能够是一个正则表达式。当此断言匹配了请求的header名和值时,断言经过,进入到router的规则中去。

在工程的配置文件加上如下的配置:

spring:
 profiles:
   active: header_route

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


复制代码

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

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

复制代码

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

Cookie Route Predicate Factory

Cookie Route Predicate Factory须要2个参数,一个时cookie名字,另外一个时值,能够为正则表达式。它用于匹配请求中,带有该名称的cookie和cookie匹配正则表达式的请求。

在配置文件添加如下配置:

spring:
  profiles:
    active: cookie_route


---
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://httpbin.org:80/get
        predicates:
        - Cookie=name, forezp
  profiles: cookie_route

复制代码

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

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

Host Route Predicate Factory

Host Route Predicate Factory须要一个参数即hostname,它能够使用. * 等去匹配host。这个参数会匹配请求头中的host的值,一致,则请求正确转发。

在工程的配置文件,加上如下配置:

spring:
  profiles:
    active: host_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://httpbin.org:80/get
        predicates:
        - Host=**.fangzhipeng.com
  profiles: host_route
复制代码

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

curl -H 'Host:www.fangzhipeng.com' localhost:8081
复制代码

Method Route Predicate Factory

Method Route Predicate Factory 须要一个参数,即请求的类型。好比GET类型的请求都转发到此路由。在工程的配置文件加上如下的配置:

spring:
  profiles:
    active: method_route

---
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://httpbin.org:80/get
        predicates:
        - Method=GET
  profiles: method_route


复制代码

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

$ curl localhost:8081

复制代码

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

$ curl -XPOST localhost:8081

复制代码

Path Route Predicate Factory

Path Route Predicate Factory 须要一个参数: 一个spel表达式,应用匹配路径。

在工程的配置文件application.yml文件中,作如下的配置:

spring:
  profiles:
    active: path_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://httpbin.org:80/get
        predicates:
        - Path=/foo/{segment}
  profiles: path_route

复制代码

在上面的配置中,全部的请求路径知足/foo/{segment}的请求将会匹配并被路由,好比/foo/1 、/foo/bar的请求,将会命中匹配,并成功转发。

使用curl模拟一个请求localhost:8081/foo/dew,执行以后会返回正确的请求结果。

$ curl localhost:8081/foo/dew

复制代码

Query Route Predicate Factory

Query Route Predicate Factory 须要2个参数:一个参数名和一个参数值的正则表达式。在工程的配置文件application.yml作如下的配置:

spring:
  profiles:
    active: query_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://httpbin.org:80/get
        predicates:
        - Query=foo, ba.
  profiles: query_route

复制代码

在上面的配置文件中,配置了请求中含有参数foo,而且foo的值匹配ba.,则请求命中路由,好比一个请求中含有参数名为foo,值的为bar,可以被正确路由转发。

模拟请求的命令以下:

$ curl localhost:8081?foo=bar


复制代码

Query Route Predicate Factory也能够只填一个参数,填一个参数时,则只匹配参数名,即请求的参数中含有配置的参数名,则命中路由。好比如下的配置中,配置了请求参数中含有参数名为foo 的参数将会被请求转发到uri为http://httpbin.org:80/get。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://httpbin.org:80/get
        predicates:
        - Query=foo
  profiles: query_route

复制代码

总结

在本篇文章中,首先介绍了Spring Cloud Gateway的工做流程和原理,而后介绍了gateway框架内置的predict及其分类,最后以案例的形式重点讲解了几个重要的Predict。Predict做为断言,它决定了请求会被路由到哪一个router 中。在断言以后,请求会被进入到filter过滤器的逻辑,下篇文章将会为你们介绍Spring Cloud Gateway过滤器相关的内容。

 java B2B2C Springcloud多租户电子商城系统 

相关文章
相关标签/搜索