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

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> {
    @Override
    public boolean test(String s) {
        return s == null || s.isEmpty();
    }
}
测试运行
package predicate;

import org.junit.Test;

import java.util.function.Predicate;

public class PredicateTest {
    @Test
    public 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 {    
    @Test
    public void test2(){
        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public 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 {    
    @Test
    public 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 {    
    @Test
    public 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 {    
    @Test
    public 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 {    
    @Test
    public 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必须为指定范围

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

相关文章:

  • 利用解析差异SSRF + sqlite注入 + waf逻辑漏洞 -- xyctf 2025 fate WP
  • Python高阶函数-sorted(深度解析从原理到实战)
  • 行星际介质与等离子体环境
  • Day20 -实例:红蓝队优秀集成式信息打点工具的配置使用
  • 1990-2019年各地级市GDP数据
  • XC7K160T-2FFG676I Kintex‑7系列 Xilinx 赛灵思 FPGA 详细技术规格
  • QML菜单控件:菜单的常规用法
  • aws s3api 常用命令
  • 创意 Python 爱心代码
  • ⭐算法OJ⭐滑动窗口最大值【双端队列(deque)】Sliding Window Maximum
  • 【玩转全栈】—— Django 连接 vue3 保姆级教程,前后端分离式项目2025年4月最新!!!
  • 交换机可以代替路由器的功能吗
  • 低代码开发革命:用 ZKmall开源商城可视化逻辑编排实现业务流程再造
  • 【计网】TCP协议的拥塞控制与流量控制
  • 【数据库系统原理】知识点
  • 一个简单的php加密的理解
  • 数据结构实验3.2:链栈的基本操作与括号匹配问题
  • 《Java八股文の文艺复兴》第十一篇:量子永生架构——对象池的混沌边缘(终极试炼·完全体)
  • 文本情感分析预处理教程:从数据采集到可视化
  • VBA之Word应用:利用Range方法进行字体及对齐方式设置
  • 保定百度网站建设/站长工具seo查询软件
  • 做衣服的网站/爱站数据
  • 怎么做类似豆瓣的网站/上海seo公司排名榜
  • 都匀网站建设/制作网站推广
  • 多php网站建设/做网站找哪个公司好
  • 济南市网站建设/软文广告代理平台