Spring Cloud Gateway内置Predicate

本文介绍了Spring Cloud Gateway中的内置Predicate,包括匹配请求时间、Cookie、Header、Host、请求方法、请求路径、请求参数、请求者IP地址以及路由权重等,详细解析了每个Predicate的使用方法和配置示例。
摘要由CSDN通过智能技术生成

参考:官方文档

https://www.cnblogs.com/babycomeon/p/11161073.html

Spring Cloud版本:Hoxton.SR5

Spring Cloud Gateway版本:2.2.3.RELEASE

1 简介

Spring Cloud Gateway内置了很多Predicate,用来制定路由匹配规则。

Predicate来源于Java 8,是Java 8中引入的一个函数,Predicate接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与、或、非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。

Spring Cloud GatewaySpring利用Predicate的特性实现了各种路由匹配规则,有通过Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。

下图总结了内置的Predicate

在这里插入图片描述

2 内置Predicate

2.1 匹配请求时间

2.1.1 AfterRoutePredicateFactory

AfterRoutePredicateFactory可以配置一个时间datetime,类型是ZonedDateTime。这个Predicate会匹配那些在设定的时间之后到达的请求。

ZonedDateTimeJava 8中日期时间功能类,用于表示带时区的日期与时间信息的类,ZonedDateTime支持通过时区来设置时间,中国的时区是:Asia/Shanghai

下面是在application.yml文件中配置该Predicate的例子:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2020-06-26T00:00:00+08:00[Asia/Shanghai]

2.1.2 BeforeRoutePredicateFactory

BeforeRoutePredicateFactory配置一个datetime时间。这个Predicate会匹配那些在设定时间之前到达的请求。

spring:
  cloud:
    gateway:
      routes:
      - id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值