跳到主要内容
版本:5.0.1

Gateway 请求断言

DeepSeek V3 中英对照 Gateway Request Predicates

Spring Cloud Gateway MVC 作为 Spring WebMvc.fn HandlerMapping 基础设施的一部分进行路由匹配。Spring Cloud Gateway 复用了许多来自 WebMvc.fn 的 RequestPredicate 实现,并包含了其他自定义的 RequestPredicate 实现。所有这些断言都匹配 HTTP 请求的不同属性。你可以使用 RequestPredicate.and() 和 RequestPredicate.or() 方法组合多个路由断言工厂。

请求后断言​

After 路由谓词工厂接受一个参数,即 datetime(这是一个 Java ZonedDateTime)。此谓词匹配发生在指定日期时间之后的请求。日期时间可以指定为 ZonedDateTime 字符串或纪元毫秒数。以下示例配置了一个 after 路由谓词:

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

日期时间也可以指定为纪元毫秒数:

spring:
cloud:
gateway:
mvc:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=1484968967789
import java.time.ZonedDateTime;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.after;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsAfter() {
return route("after_route")
.route(after(ZonedDateTime.parse("2017-01-20T17:42:47.789-07:00[America/Denver]")), http())
.before(uri("https://example.org"))
.build();
}
}

该路由匹配 2017 年 1 月 20 日 17:42 山区时间(丹佛)之后发出的任何请求。

前置请求断言​

Before 路由谓词工厂接收一个参数 datetime(这是一个 Java ZonedDateTime)。此谓词匹配发生在指定 datetime 之前的请求。datetime 可以指定为 ZonedDateTime 字符串或纪元毫秒数。以下示例配置了一个 before 路由谓词:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]

日期时间也可以指定为纪元毫秒数:

spring:
cloud:
gateway:
mvc:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=1484968967789
import java.time.ZonedDateTime;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.before;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsBefore() {
return route("before_route")
.route(before(ZonedDateTime.parse("2017-01-20T17:42:47.789-07:00[America/Denver]")), http()
.before(uri("https://example.org"))
.build();
}
}

该路由匹配2017年1月20日17:42(丹佛山区时间)之前发出的任何请求。

介于请求谓词​

Between 路由谓词工厂接受两个参数:datetime1 和 datetime2,它们都是 Java ZonedDateTime 对象。此谓词匹配发生在 datetime1 之后且 datetime2 之前的请求。datetime2 参数必须在 datetime1 之后。日期时间可以指定为 ZonedDateTime 字符串或纪元毫秒数。以下示例配置了一个 between 路由谓词:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

日期时间也可以指定为纪元毫秒数:

spring:
cloud:
gateway:
mvc:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=1484968967789, 1485055367789
import java.time.ZonedDateTime;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.between;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsBetween() {
return route("between_route")
.route(between(ZonedDateTime.parse("2017-01-20T17:42:47.789-07:00[America/Denver]"),
ZonedDateTime.parse("2017-01-21T17:42:47.789-07:00[America/Denver]")), http())
.before(uri("https://example.org"))
.build();
}
}

该路由匹配在2017年1月20日17:42(山区时间,丹佛)之后、2017年1月21日17:42(山区时间,丹佛)之前发出的任何请求。这对于维护窗口非常有用。

Cookie 路由谓词工厂接受两个参数:cookie name 和 regexp(这是一个 Java 正则表达式)。该谓词匹配具有给定名称且其值符合正则表达式的 cookie。以下示例配置了一个 cookie 路由谓词工厂:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.cookie;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsCookie() {
return route("cookie_route")
.route(cookie("chocolate", "ch.p"), http())
.before(uri("https://example.org"))
.build();
}
}

该路由匹配具有名为 chocolate 的 cookie 且其值符合 ch.p 正则表达式的请求。

头部请求谓词​

Header 路由断言工厂接收两个参数:header 和 regexp(这是一个 Java 正则表达式)。该断言会匹配具有指定名称且其值符合正则表达式的请求头。以下示例配置了一个基于请求头的路由断言:

spring:
cloud:
server:
webmvc:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.header;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsHeader() {
return route("header_route")
.route(header("X-Request-Id", "\\d+"), http())
.before(uri("https://example.org"))
.build();
}
}

该路由匹配的条件是请求包含名为 X-Request-Id 的标头,且其值符合 \d+ 正则表达式(即该值由一个或多个数字组成)。

主机请求谓词​

Host 路由断言工厂接收一个参数:主机名模式列表。模式是使用 . 作为分隔符的 Ant 风格模式。该断言会匹配与模式相符的 Host 请求头。以下示例配置了一个主机路由断言:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsHost() {
return route("host_route")
.route(host("**.somehost.org", "**.anotherhost.org"), http())
.before(uri("https://example.org"))
.build();
}
}

URI 模板变量(例如 {sub}.myhost.org)同样受支持。

该路由匹配的条件是请求的 Host 头部值为 www.somehost.org、beta.somehost.org 或 www.anotherhost.org。

该谓词将 URI 模板变量(例如前面示例中定义的 sub)提取为名称和值的映射,并将其放入 ServerRequest.attributes() 中,键由 MvcUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE 定义。这些值随后可供 Gateway Handler Filter Functions 使用。

方法请求谓词​

Method 请求谓词接受一个 methods 参数,该参数是一个或多个参数:要匹配的 HTTP 方法。以下示例配置了一个方法路由谓词:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
import org.springframework.http.HttpMethod;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.method;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsMethod() {
return route("method_route")
.route(method(HttpMethod.GET, HttpMethod.POST), http())
.before(uri("https://example.org"))
.build();
}
}

如果请求方法是 GET 或 POST,则此路由匹配。

GatewayRequestPredicates.method 是 RequestPredicates.methods 的一个简单别名。此外,RouterFunctions.Builder API 包含了一些便捷方法,这些方法结合了 method 和 path 这两个 RequestPredicates。

import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsMethodAndPath() {
return route("method_and_path_route")
.GET("/mypath", http())
.before(uri("https://example.org"))
.build();
}
}

该路由在请求方法为 GET 且路径为 /mypath 时匹配。

路径请求谓词​

Path 请求谓词接受两个参数:一个 Spring PathPattern patterns 列表。该请求谓词使用 RequestPredicates.path() 作为底层实现。以下示例配置了一个路径路由谓词:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.path;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsPath() {
return route("path_route")
.route(path("/red/{segment}", "/blue/{segment}"), http())
.before(uri("https://example.org"))
.build();
}
}

如果请求路径是以下示例之一,则该路由匹配:/red/1 或 /red/1/ 或 /red/blue 或 /blue/green。

该谓词提取URI模板变量(例如前面示例中定义的segment)作为名称和值的映射,并将其放入ServerRequest.attributes()中,键由RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE定义。然后,这些值可供网关处理程序过滤器函数使用。

一个实用方法(称为 get)可用于更便捷地访问这些变量。以下示例展示了如何使用 get 方法:

Map<String, Object> uriVariables = MvcUtils.getUriTemplateVariables(request);

String segment = uriVariables.get("segment");

查询请求谓词​

Query 路由断言工厂接收两个参数:必需的 param 和可选的 regexp(这是一个 Java 正则表达式)。以下示例配置了一个查询路由断言:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.query;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsQuery() {
return route("query_route")
.route(query("green"), http())
.before(uri("https://example.org"))
.build();
}
}

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

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.query;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsQuery() {
return route("query_route")
.route(query("red", "gree."), http())
.before(uri("https://example.org"))
.build();
}
}

如果请求中包含一个值为 red 的查询参数,并且该值匹配正则表达式 gree.,那么前面的路由就会匹配,因此 green 和 greet 都会匹配。

权重请求谓词​

Weight 路由谓词工厂接受两个参数:group 和 weight(一个 int 类型)。权重是按组计算的。以下示例配置了一个权重路由谓词:

spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.path;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.weight;

@Configuration
class RouteConfiguration {

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsWeights() {
return route("weight_high")
.route(weight("group1", 8).and(path("/**")), http())
.before(uri("https://weighthigh.org"))
.build().and(
route("weight_low")
.route(weight("group1", 2).and(path("/**")), http())
.before(uri("https://weightlow.org"))
.build());
}
}

该路由会将约 80% 的流量转发至 weighthigh.org,约 20% 的流量转发至 weightlow.org。