当前位置: 首页 > news >正文

SpringCloud Gateway中Route Predicate Factories详细说明

官网地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories

在这里插入图片描述

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。

Spring Cloud Gateway 包含许多内置的Route Predicate Factories。所有这些Predicate 都匹配HTTP请求的不同属性。多种Predicate 工厂可以通过逻辑and进行组合。

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

如上图所示,gateway默认提供了11中路由断言工厂供用户组合使用。

【1】 After Route Predicate Factory

匹配发生在指定日期的请求。

The after route predicate factory takes one parameter, a datetime. This predicate matches requests that happen after the specified datetime.

- id: payment_routh2 # 路由id,没有uri:  lb://cloud-payment-servicepredicates:- After=2024-02-05T15:10:03.685+08:00[Asia/Shanghai]         # 断言,路径相匹配的进行路由

其中After后的时间串可以如下得到:

public class ZonedDateTimeDemo
{public static void main(String[] args){ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区System.out.println(zbj);
//        ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
//        System.out.println(zny);}
}

【2】Before Route Predicate Factory

匹配发生在指定日期时间 的请求。

- id: payment_routh2 # 路由id,没有uri:  lb://cloud-payment-servicepredicates:- Before=2024-02-05T15:10:03.685+08:00[Asia/Shanghai]         # 断言,路径相匹配的进行路由

【3】Between Route Predicate Factory

匹配发生在两个指定日期时间 中间的请求。

- id: payment_routh2 # 路由id,没有uri:  lb://cloud-payment-servicepredicates:- Between=2024-02-05T15:10:03.685+08:00[Asia/Shanghai],2024-02-25T15:10:03.685+08:00[Asia/Shanghai]        # 断言,路径相匹配的进行路由

【4】Cookie Route Predicate Factory

Cookie Route Predicate需要两个参数,一个是 Cookie name ,一个是正则表达式。

路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行

spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookie=chocolate, ch.p

【5】Header Route Predicate Factory

请求头匹配,需要两个参数:请求头名称和正则表达式。如果请求头属性值和正则表达式匹配,则执行路由。

spring:cloud:gateway:routes:- id: header_routeuri: https://example.orgpredicates:- Header=X-Request-Id, \d+

【6】Host Route Predicate Factory

主机地址匹配,只有一个参数:一组主机列表,其是Ant风格,以.为分隔符。如果请求头中的Host匹配主机列表,则执行路由。

spring:cloud:gateway:routes:- id: host_routeuri: https://example.orgpredicates:- Host=**.somehost.org,**.anotherhost.org

参数值同样支持URI 模板变量,比如{sub}.myhost.org。如果一个请求有请求头Host且值比如www.somehost.org或beta.somehost.org或www.anotherhost.org均会执行路由。

【7】Method Route Predicate Factory

请求方法匹配,参数值为一个或多个请求方法。

spring:cloud:gateway:routes:- id: method_routeuri: https://example.orgpredicates:- Method=GET,POST

【8】Path Route Predicate Factory

路径匹配,支持通配符和URI template variables。

spring:cloud:gateway:routes:- id: host_routeuri: https://example.orgpredicates:- Path=/red/{segment},/blue/{segment}

{segment},其实就是ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE得到的map中,map.get("segment")对应的值。

【9】Query Route Predicate Factory

查询参数匹配,两个参数:参数名和值正则表达式(可选)。

  • 如果只有一个参数,参数名,则表示查询参数必须包含该参数
  • 如果有两个参数,则表示参数值还要符合正则表达式

一个参数实例:

spring:cloud:gateway:routes:- id: query_routeuri: https://example.orgpredicates:- Query=green

两个参数实例:

spring:cloud:gateway:routes:- id: query_routeuri: https://example.orgpredicates:- Query=red, gree.

【10】The RemoteAddr Route Predicate Factory

请求目标地址匹配,只有一个参数:为一组IPV4或IPV6的地址,比如192.168.0.1/16。

spring:cloud:gateway:routes:- id: remoteaddr_routeuri: https://example.orgpredicates:- RemoteAddr=192.168.1.1/24

【11】Weight Route Predicate Factory

The weight route predicate factory takes two arguments: group and weight. The weights are calculated per group.

spring:cloud:gateway:routes:- id: weight_highuri: https://weighthigh.orgpredicates:- Weight=group1, 8- id: weight_lowuri: https://weightlow.orgpredicates:- Weight=group1, 2

This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weighlow.org

http://www.lryc.cn/news/370766.html

相关文章:

  • 计算机视觉全系列实战教程:(八)图像变换-点运算、灰度变换、直方图变换
  • 4.MongoDB sharding Cluster 分片集群
  • PDF转图片工具
  • Day 19:419. 甲板上的战舰
  • Web前端专科实习:技能提升、实践挑战与职业展望
  • 简单脉冲动画效果实现
  • apache poi 插入“下一页分节符”并设置下一节纸张横向的一种方法
  • 【React】useCallback和useMemo使用指南
  • XMind软件下载-详细安装教程视频
  • 一个小的画布Canvas页面,记录点的轨迹
  • docker-compose教程
  • 结果出乎意料!MySQL和MariaDB谁快?MySQL 8.0比MySQL 5.6快吗?
  • Alienware外星人X17R2 原装Win11系统镜像下载 带SupportAssist OS Recovery一键恢复
  • 【NI国产替代】高速数据采集模块,最大采样率为 125 Msps,支持 FPGA 定制化
  • 【网络安全的神秘世界】2024.6.6 Docker镜像停服?解决最近Docker镜像无法拉取问题
  • 【Python入门与进阶】1基本输入和输出
  • CTF Show MISC做题笔记
  • 【QT5】<总览二> QT信号槽、对象树及常用函数
  • Button按钮类
  • 代码随想录-二叉树 | 111 二叉树的最小深度
  • PCA降维算法
  • Fast R-CNN 与 R-CNN的不同之处
  • 前端开发环境:Vue、Element Plus、Axios
  • 我的创作纪念日-在SCDN的5年
  • AI-知识库搭建(二)GPT-Embedding模型使用
  • qt网络事件之QSocketNotifier
  • 如何统计EXCEL中的数据透视表的信息?
  • 日本结构型产品及衍生品业务变迁报告
  • 解决Mac无法上网/网络异常的方法,重置网络
  • [12] 使用 CUDA 进行图像处理