Spring Cloud之服务入口Gateway之Route Predicate Factories

目录

Route Predicate Factories

Predicate

实现Predicate接口

测试运行

Predicate的其它实现方法

匿名内部类

lambda表达式

Predicate的其它方法

源码详解

代码示例

Route Predicate Factories

The After Route Predicate Factory

The Before Route Predicate Factory

The Between Route Predicate Factory

The Header Route Predicate Factory

The Host Route Predicate Factory

The Method Route Predicate Factory

The Path Route Predicate Factory

The RemoteAddr Route Predicate Factory


Route Predicate Factories

Predicate

Predicate是Java 8提供的⼀个函数式编程接⼝, 它接收⼀个参数并返回⼀个布尔值, ⽤于条件过滤, 请求参数的校验.

@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}
实现Predicate接口
package predicate;import java.util.function.Predicate;/*** 判断字符串是否为空* 空  -true* 非空 -false*/
public class StringPredicate implements Predicate<String> {@Overridepublic boolean test(String s) {return s == null || s.isEmpty();}
}
测试运行
package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {@Testpublic void test(){Predicate<String> predicate = new StringPredicate();System.out.println(predicate.test(""));System.out.println(predicate.test("aa"));}
}

运行结果:

Predicate的其它实现方法
匿名内部类
package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {    @Testpublic void test2(){Predicate<String> predicate = new Predicate<String>() {@Overridepublic boolean test(String s) {return s == null || s.isEmpty();}};System.out.println(predicate.test(""));System.out.println(predicate.test("aa"));}
}

运行结果:

lambda表达式
package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {    @Testpublic void test3(){Predicate<String> predicate = s -> s == null || s.isEmpty();System.out.println(predicate.test(""));System.out.println(predicate.test("aa"));}
}

运行结果:

Predicate的其它方法

1.isEqual(Object targetRef) :比较两个对象是否相等,参数可以为Null
2.and(Predicate other): 短路与操作,返回⼀个组成Predicate
3.or(Predicate other) :短路或操作,返回⼀个组成Predicate
4.test(T t) :传⼊⼀个Predicate参数,⽤来做判断
5.negate() : 返回表⽰此Predicate逻辑否定的Predicate

源码详解

当前类的test方法&&other类的test方法

对当前类的test方法进行取非

当前类的test方法 || other类的test方法

判断两个对象是否相等 

代码示例

代码示例1

package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {    @Testpublic void test4(){Predicate<String> predicate = s -> s == null || s.isEmpty();System.out.println(predicate.negate().test(""));System.out.println(predicate.negate().test("aa"));}
}

运行结果:

代码示例2

package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {    @Testpublic void test5(){Predicate<String> predicate = s-> "aa".equals(s);Predicate<String> predicate2 = s-> "bb".equals(s);System.out.println(predicate.or(predicate2).test(""));System.out.println(predicate.or(predicate2).test("aa"));}
}

运行结果:

代码示例3

package predicate;import org.junit.Test;import java.util.function.Predicate;public class PredicateTest {    @Testpublic void test6(){Predicate<String> predicate = s-> s!=null && !s.isEmpty();Predicate<String> predicate2 = s-> s!=null && s.chars().allMatch(Character::isDigit);System.out.println(predicate.and(predicate2).test("aa"));System.out.println(predicate.and(predicate2).test("123"));}
}

运行结果:

Route Predicate Factories

Route Predicate Factories (路由断⾔⼯⼚, 也称为路由谓词⼯⼚, 此处谓词表⽰⼀个函数), 在Spring Cloud Gateway中, Predicate提供了路由规则的匹配机制.

我们在配置⽂件中写的断⾔规则只是字符串, 这些字符串会被Route Predicate Factory读取并处理, 转变为路由判断的条件。
这个规则是由org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateF
actory 来实现的.

Spring Cloud Gateway 默认提供了很多Route Predicate Factory, 这些Predicate会分别匹配HTTP请求的不同属性, 并且多个Predicate可以通过and逻辑进⾏组合.

详细介绍

The After Route Predicate Factory

The After route predicate factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen after the specified datetime. The following example configures an after route predicate:

这个⼯⼚需要⼀个⽇期时间(Java的 ZonedDateTime对象), 匹配指定⽇期之后的请求

The Before Route Predicate Factory

The Before route predicate factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen before the specified datetime. The following example configures a before route predicate:

匹配指定⽇期之前的请求

The Between Route Predicate Factory

The Between route predicate factory takes two parameters, datetime1 and datetime2 which are java ZonedDateTime objects. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1. The following example configures a between route predicate:

匹配两个指定时间之间的请求datetime2 的参数必须在datetime1 之后

The Cookie route predicate factory takes two parameters, the cookie name and a regexp (which is a Java regular expression). This predicate matches cookies that have the given name and whose values match the regular expression. The following example configures a cookie route predicate factory:

请求中包含指定Cookie, 且该Cookie值符合指定的正则表达式

The Header Route Predicate Factory

The Header route predicate factory takes two parameters, the header and a regexp (which is a Java regular expression). This predicate matches with a header that has the given name whose value matches the regular expression. The following example configures a header route predicate:

请求中包含指定Header, 且该Header值符合指定的正则表达式

The Host Route Predicate Factory

The Host route predicate factory takes one parameter: a list of host name patterns. The pattern is an Ant-style pattern with . as the separator. This predicates matches the Host header that matches the pattern. The following example configures a host route predicate:

请求必须是访问某个host(根据请求中的Host字段进⾏匹配)

The Method Route Predicate Factory

The Method Route Predicate Factory takes a methods argument which is one or more parameters: the HTTP methods to match. The following example configures a method route predicate:

匹配指定的请求⽅式

The Path Route Predicate Factory

The Path Route Predicate Factory takes two parameters: a list of Spring PathMatcher patterns and an optional flag called matchTrailingSlash (defaults to true). The following example configures a path route predicate:

匹配指定规则的路径
The RemoteAddr Route Predicate Factory

The RemoteAddr route predicate factory takes a list (min size 1) of sources, which are CIDR-notation (IPv4 or IPv6) strings, such as 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask). The following example configures a RemoteAddr route predicate:

请求者的IP必须为指定范围

欢迎大家来访问我的博客主页-----》博客链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/33.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

下载安装Node.js及其他环境

提示&#xff1a;从Node版本降级到Vue项目运行 文章目录 下载Node.js环境配置配置环境变量 安装 cnpm&#xff08;我需要安装&#xff09;安装脚手架安装依赖安装淘宝镜像&#xff08;注意会更新&#xff09;cnpm vs npm 与新旧版本核心差异包管理器不同功能差异如何选择&#…

C++抽卡模拟器

近日在学校无聊&#xff0c;写了个抽卡模拟器供大家娱乐。 代码实现以下功能&#xff1a;抽卡界面&#xff0c;抽卡判定、动画播放、存档。 1.抽卡界面及判定 技术有限&#xff0c;不可能做的和原神一样精致。代码如下&#xff08;注&#xff1a;这不是完整代码&#xff0c;…

Redis 热key问题怎么解决?

Redis 热 Key 问题分析与解决方案 热 Key(Hot Key)是指被高频访问的某个或多个 Key,导致单个 Redis 节点负载过高,可能引发性能瓶颈甚至服务崩溃。以下是常见原因及解决方案: 1. 热 Key 的常见原因 突发流量:如明星八卦、秒杀商品、热门直播等场景。缓存设计不合理:如全…

第十四届蓝桥杯省赛真题解析(含C++详细源码)

第十四届蓝桥杯省赛 整数删除满分思路及代码solution1 &#xff08;40% 双指针暴力枚举&#xff09;solution 2&#xff08;优先队列模拟链表 AC&#xff09; 冶炼金属满分代码及思路 子串简写满分思路及代码solution 1&#xff08;60% 双指针&#xff09;solution 2&#xff0…

Matlab:三维绘图

目录 1.三维曲线绘图命令&#xff1a;plot3 实例——绘制空间直线 实例——绘制三角曲线 2.三维曲线绘图命令&#xff1a;explot3 3.三维网格命令&#xff1a;mesh 实例——绘制网格面 实例——绘制山峰曲面 实例——绘制函数曲线 1.三维曲线绘图命令&#xff1a;plot3 …

(51单片机)独立按键控制流水灯LED流向(独立按键教程)(LED使用教程)

源代码 如上图将7个文放在Keli5 中即可&#xff0c;然后烧录在单片机中就行了 烧录软件用的是STC-ISP&#xff0c;不知道怎么安装的可以去看江科大的视频&#xff1a; 【51单片机入门教程-2020版 程序全程纯手打 从零开始入门】https://www.bilibili.com/video/BV1Mb411e7re?…

React框架的Concurrent Mode

以下是关于 Concurrent Mode 的系统梳理: 一、Concurrent Mode 的诞生背景 传统渲染的局限性 同步阻塞:React 15 的 Stack Reconciler 无法中断渲染流程优先级缺失:用户交互与后台任务同等对待资源竞争:网络请求与渲染任务无法智能调度核心设计目标 可中断渲染:允许高优先…

Java 集合框架与 Stream 流深入剖析(重点详细讲解)

目录 引言 一、ArrayList 1. 概述 2. 特点 动态扩容 初始容量 扩容倍数 随机访问高效 插入和删除效率低 3. 代码示例 4. 分析 二、HashSet 1. 概述 2. 特点 唯一性 插入、删除和查找效率高 无序性 3. 代码示例 4. 分析 三、HashMap 1. 概述 2. 特点 键唯…

Golang的Goroutine(协程)与runtime

目录 Runtime 包概述 Runtime 包常用函数 1. GOMAXPROCS 2. Caller 和 Callers 3. BlockProfile 和 Stack 理解Golang的Goroutine Goroutine的基本概念 特点&#xff1a; Goroutine的创建与启动 示例代码 解释 Goroutine的调度 Gosched的作用 示例代码 输出 解…

【51单片机】3-3【定时器/计数器/中断】超声波测距模块测距

1.硬件 51最小系统超声波测距模块 2.软件 #include "reg52.h"//距离小于10cm,D5亮&#xff0c;D6灭&#xff0c;反之相反现象sbit D5 P3^7;//根据原理图&#xff08;电路图&#xff09;&#xff0c;设备变量led1指向P3组IO口的第7口 sbit D6 P3^6;//根据原理图&…

2-Visual Studio 2022 NET开发Windows桌面软件并连接SQL Server数据库

引言 今天尝试Visual Studio 2022 NET开发一个NET桌面软件&#xff0c;并尝试连接SQL Server的数据库&#xff0c;此文章为开发笔记。 --------------------------------------------------------------------------------------------------------------------------------- …

Kafka 漏消费和重复消费问题

Kafka 虽然是一个高可靠、高吞吐的消息系统&#xff0c;但如果使用不当&#xff0c;**“漏消费”和“重复消费”**问题是非常容易发生的&#xff0c;尤其在业务系统中会造成数据丢失、重复写库等严重问题。 &#x1f3af; 一句话理解&#xff1a; Kafka 本身提供 “至多一次”…