
Spring Cloud Gateway 是 Spring 生态系统中用于构建 API 网关的框架,它基于 Project Reactor 和 Netty 构建,旨在提供一种高效且灵活的方式来处理 HTTP 请求和响应。
Spring Cloud Gateway 的路由配置中,predicates(断言)用于定义哪些请求应该匹配特定的路由规则。
断言是Gateway在进行路由时,根据具体的请求信息如请求路径、请求方法、请求参数等进行匹配的规则。当一个请求的信息符合断言设置的条件时,Gateway就会将该请求路由到对应的服务上。

根据源码可以看到Spring cloud gateway 的内置断言,可以看到核心10个内置Predicate

在 Spring Cloud Gateway 的配置中,Predicate 通常通过 predicates 字段定义,配合路由配置一起使用。它可以基于以下方面来进行请求匹配:
Spring Cloud Gateway除了提供一系列内置的断言工厂,同时也支持自定义断言。

Path Predicate 用于根据请求的路径进行匹配。
predicates:
- Path=/api/** # 匹配以 /api/ 开头的请求路径Path=/api/** 匹配请求路径以 /api/ 开头的所有请求,包括 /api/v1/users、/api/v2/orders 等。** 表示匹配任意路径段。* 表示匹配单个路径段。示例:
spring:
application:
name: spring-cloud-gateway-sample
cloud:
gateway:
routes:
- id: blog
uri: http://blog.abc.com
predicates:
# 匹配路径转发
- Path=/api-boot-datasource-switch.html
# 端口号
server:
port: 9090
在上面的配置中,当访问http://localhost:9090/api-boot-datasource-switch.html时就会被自动转发到http://blog.abc.com/api-boot-datasource-switch.html,这里要注意完全匹配Path的值时才会进行路由转发
Method Predicate 用于根据请求的方法类型(GET、POST、PUT 等)进行匹配。
predicates:
- Method=GET # 仅匹配 GET 请求Method=POST 只会匹配 HTTP POST 方法的请求。
Header Predicate 用于根据请求中的某些头信息进行匹配。
predicates:
- Header=Content-Type=application/json # 匹配 Content-Type 头为 application/json 的请求Header=X-Custom-Header=foo 匹配请求头中 X-Custom-Header 值为 foo 的请求。
Query Param Predicate 用于根据请求 URL 中的查询参数进行匹配。
predicates:
- Query=type=admin # 匹配 URL 中包含 ?type=admin 的请求Query=user=admin 匹配 URL 中查询参数 user=admin 的请求,如 https://example.com?user=admin。
Host Predicate 用于根据请求的 Host 头进行匹配。
predicates:
- Host=example.com # 匹配 Host 头为 example.com 的请求Host=*.example.com 匹配 example.com 域名下的所有子域名,如 api.example.com、blog.example.com 等。
IP Predicate 用于根据请求源 IP 地址进行匹配。
predicates:
- Ip=192.168.1.0/24 # 匹配来自 192.168.1.0/24 网段的请求Ip=10.0.0.0/8 匹配来自 10.0.0.0/8 网段的请求。Accept Predicate 用于根据请求的 Accept 头部进行匹配。
predicates:
- Accept=application/json # 匹配 Accept 头为 application/json 的请求Accept=text/html 匹配 Accept 头为 text/html 的请求。
你可以将多个 Predicate 组合成一个复合条件,使用 and、or 等逻辑操作符。
predicates:
- Path=/api/** and Method=POST # 请求路径以 /api/ 开头,且请求方法为 POST
- Path=/api/** or Query=user=admin # 请求路径以 /api/ 开头,或查询参数 user=admin
chocolate 且值符合正则表达式 choco\.value 的 Cookie。
After 和 Before,匹配某一时间段内的请求。
Spring Cloud Gateway 允许你通过逻辑运算符(and、or)组合多个 Predicate,定义更复杂的路由匹配规则。你可以组合不同的 Predicate 来匹配路径、方法、头部、参数等。
示例 1:匹配路径为 /api/** 且请求方法为 POST 的请求:
predicates:
- Path=/api/** and Method=POST示例 2:匹配路径为 /api/** 或者查询参数 user=admin 的请求:
predicates:
- Path=/api/** or Query=user=admin示例 3:使用多个 and 来组合复杂的匹配条件:
predicates:
- Path=/api/** and Method=GET and Header=Authorization=Bearer这表示只有当请求路径为 /api/** 且请求方法为 GET,且请求头中包含 Authorization: Bearer 时,才会匹配此路由。
Authorization 头predicates:
- Method=GET and Header=Authorization=Bearerpredicates:
- Host=api.example.com and Query=type=adminpredicates:
- Path=/products/** and Method=GETpredicates:
- Path=/api/** and Ip=192.168.1.0/24
除了使用内置的断言工厂外,还可以通过实现自己的 GatewayFilterFactory 来创建自定义断言。这使得你可以根据业务逻辑添加更加复杂的匹配条件。
示例:自定义断言
假设我们想要创建一个名为 Custom 的自定义断言,它可以根据请求体中的 JSON 字段进行匹配。
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import reactor.core.publisher.Mono;
public class CustomPredicateFactory extends AbstractRoutePredicateFactory<CustomPredicateFactory.Config> {
public CustomPredicateFactory() {
super(Config.class);
}
@Override
public Mono<Boolean> applyAsync(Config config, ServerHttpRequest request) {
// 实现具体的匹配逻辑
return Mono.just(request.getHeaders().containsKey("Custom-Header"));
}
public static class Config {
private String key;
// Getters and setters
}
}然后在配置文件中引用这个自定义断言:
predicates:
- name: Custom
args:
key: value
总结
Predicate 是 Spring Cloud Gateway 中定义路由匹配条件的核心组件,通过多个 Predicate 条件,你可以灵活地匹配请求。Predicate 以构建复杂的路由规则。and、or)来满足不同的路由需求。通过灵活使用 Predicate,你可以对不同的请求进行精准路由控制,从而实现高效的请求管理和流量分发。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。