Spring Cloud Gateway中Route Predicate Factories(路由断言工厂)的详细介绍

发布于:2025-04-02 ⋅ 阅读:(82) ⋅ 点赞:(0)

在 Spring Cloud Gateway 中,Route Predicate Factories(路由断言工厂) 用于 匹配请求是否符合路由的条件。如果请求满足某些断言,网关就会将其转发到相应的目标服务。Spring Cloud Gateway 提供了 多个内置的 Predicate Factories,并允许你自定义断言。

1. Predicate 断言介绍

📌 在 Spring Cloud Gateway 中,Predicate 代表的是一个布尔函数,用于判断 HTTP 请求是否匹配某个路由。

  • 断言返回 true ➝ 请求被路由

  • 断言返回 false ➝ 请求被丢弃

可以组合多个 Predicate,只要一个路由的所有断言都满足,该路由才会被匹配

配置方式:

  • YAML 配置

  • Java 代码配置

2. 内置断言规则(15种)

📌Spring Cloud Gateway 内置了 15+ 种 Predicate Factories,可以按请求路径、方法、Header、Query 参数等进行匹配。

2.1 After Route Predicate Factory

作用:匹配请求时间是否在指定时间 之后

YAML 配置:

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - After=2024-03-29T10:15:30+08:00[Asia/Shanghai]

Java 配置:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("after_route", r -> r.after(ZonedDateTime.parse("2024-03-29T10:15:30+08:00[Asia/Shanghai]"))
            .uri("http://example.org"))
        .build();
}

2.2 Before Route Predicate Factory

作用:匹配请求时间是否在指定时间 之前

predicates:
  - Before=2025-01-01T00:00:00+08:00[Asia/Shanghai]

2.3 Between Route Predicate Factory

作用:匹配请求时间是否在 两个时间点之间

predicates:
  - Between=2024-03-29T08:00:00+08:00, 2024-03-29T18:00:00+08:00

2.4 Cookie Route Predicate Factory

作用:根据 Cookie 值进行匹配

predicates:
  - Cookie=SESSIONID, ^[A-Za-z0-9]+$

匹配 SESSIONID 的值是否符合正则表达式 1+$。

2.5 Header Route Predicate Factory

作用:根据 HTTP Header 进行匹配

predicates:
  - Header=X-Request-Id, \d+

匹配 X-Request-Id 头部是否为数字。

2.6 Host Route Predicate Factory

作用:匹配 Host 头(支持通配符)

predicates:
  - Host=**.example.com

匹配 *.example.com,例如 api.example.com。

2.7 Method Route Predicate Factory

作用:匹配 HTTP 方法

predicates:
  - Method=GET, POST

匹配 GET 和 POST 请求。

2.8 Path Route Predicate Factory

作用:匹配请求路径(支持 * 和 ** 通配符)

predicates:
  - Path=/api/v1/users/**

匹配 /api/v1/users/ 开头的路径。

2.9 Query Route Predicate Factory

作用:匹配 URL 查询参数

predicates:
  - Query=version, v1

匹配 ?version=v1 的请求。

2.10 RemoteAddr Route Predicate Factory

作用:匹配请求的 IP 地址

predicates:
  - RemoteAddr=192.168.1.1/24

匹配 192.168.1.x 的 IP 地址。

2.11 Weight Route Predicate Factory

作用:基于权重的流量分发(用于灰度发布)

predicates:
  - Weight=group1, 5

将 group1 权重设为 5,影响负载均衡策略。

3. 组合多个 Predicate

📌 可以组合多个断言来匹配更复杂的请求规则,例如:

predicates:
  - Path=/api/**
  - Method=GET
  - Header=X-User, \d+
  - Query=debug, true

这个规则要求:

  • 请求路径以 /api/ 开头

  • 仅限 GET 方法

  • X-User 头部值必须是数字

  • debug=true 必须出现在 Query 参数中

4. 自定义 Predicate Factory

📌 如果内置的 Predicate 不能满足需求,可以自己扩展:

@Component
public class CustomPredicateFactory extends AbstractRoutePredicateFactory<CustomPredicateFactory.Config> {

    public CustomPredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return exchange -> {
            String paramValue = exchange.getRequest().getQueryParams().getFirst(config.getParam());
            return paramValue != null && paramValue.contains(config.getValue());
        };
    }

    public static class Config {
        private String param;
        private String value;

        // getter and setter
    }
}

在 application.yml 配置:

predicates:
  - Custom=myParam, hello

匹配 ?myParam=hello 的请求。

5. 总结

Predicate 作用
After 请求时间 在指定时间后
Before 请求时间 在指定时间前
Between 请求时间 在某时间范围内
Cookie 按 Cookie 值 匹配
Header 按 HTTP 头部 匹配
Host 按 Host 头 匹配(支持通配符)
Method 按 HTTP 方法 匹配
Path 按 URL 路径 匹配(支持 * 和 **)
Query 按 查询参数 匹配
RemoteAddr 按 请求 IP 匹配
Weight 按 流量权重 进行负载均衡

👉 可以在 YAML 或 Java 配置路由匹配规则,结合多个 Predicate 进行灵活匹配!

在这里插入图片描述


别害怕付出没有回报,那些看似平淡无奇的日子,都是成长的伏笔。



  1. A-Za-z0-9 ↩︎


网站公告

今日签到

点亮在社区的每一天
去签到