跳到主要内容

【3】路由断言工厂(路由匹配规则)详解

文章目录

    • RoutePredicateFactory
  • Spring Cloud Gateway提供的路由断言工厂(路由规则)
    • After
    • Before
    • Between
    • Cookie
    • Header
    • Host
    • Method
    • Path
    • Query
    • RemoteAddr
    • Weight

RoutePredicateFactory

在上篇的入门案例中,我们使用 Path 实现了一个简单的针对请求路径的转发规则,在编写路由时,需要配置id、uri、predicates。
 

Spring Cloud Gateway 创建 Route对象时,使用RoutePredicateFactory 创建 Predicate对象,Predicate对象可以赋值给Route。

Spring Cloud Gateway包含许多内置的 Route Predicate Factories:

  • 所有这些断言都匹配 HTTP请求的不同属性。
  • 多个Route Predicate Factories可以通过逻辑与(and)结合起来一起使用。
  • 路由断言工厂RoutePredicateFactory包含的主要实现类如图所示,包括Datetime、请求的远程地址、路由权重、请求头、Http地址、请求方法、请求路径和请求参数等类型的路由断言。

 
还可以查看RoutePredicateFactory 接口的实现类,每个都对应了一个规则。