Spring Cloud全解析:网关之GateWay断言

server/2024/10/11 13:29:19/

GateWay断言

断言Predicate

gateWay网关中提供了多种断言方式

After断言

Loaded RoutePredicateFactory [After]

After匹配在当前日期时间之后发生的请求

spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- After=2021-09-06T16:02:25.738+08:00[Asia/Shanghai]
Before断言

Loaded RoutePredicateFactory [Before]

Before匹配在当前日期时间之前发生的请求

spring:cloud:gateway:routes:- id: before_routeuri: https://example.orgpredicates:- Before=2021-09-06T16:02:25.738+08:00[Asia/Shanghai]
Between断言

Loaded RoutePredicateFactory [Between]

Between匹配在两个日期之间发生的请求

spring:cloud:gateway:routes:- id: between_routeuri: https://example.orgpredicates:- Between=2021-09-06T16:02:25.738+08:00[Asia/Shanghai], 2021-09-07T16:02:25.738+08:00[Asia/Shanghai]
Cookie断言

Loaded RoutePredicateFactory [Cookie]

Cookie会根据cookie的name以及值的正则表达式进行匹配

spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookie=userName, zhangsan  # userName为Cookie的name,zhangsan为cookie的值的正则表达式
Header断言

Loaded RoutePredicateFactory [Header]

Header是匹配请求头的name以及值的正则表达式进行匹配

spring:cloud:gateway:routes:- id: header_routeuri: https://example.orgpredicates:- Header=X-Request-Id, \d+  #X-Request-Id是请求头的name,\d+为请求头所对应的值的正则表达式
Host断言

Loaded RoutePredicateFactory [Host]

Host进行域名匹配,Ant模式匹配

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

Loaded RoutePredicateFactory [Method]

Method 匹配HTTP方法

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

Loaded RoutePredicateFactory [Path]

Path 进行路径匹配

spring:cloud:gateway:routes:- id: host_routeuri: https://example.orgpredicates:- Path=/foo/{segment},/bar/{segment}
Query断言

Loaded RoutePredicateFactory [Query]

Query 查询参数匹配

spring:cloud:gateway:routes:- id: query_routeuri: https://example.orgpredicates:- Query=foo, ba.  #如果查询参数为foo并且其值符合ba.正则表达式
ReadBodyPredicateFactory断言

Loaded RoutePredicateFactory [ReadBodyPredicateFactory]

RemoteAddr断言

Loaded RoutePredicateFactory [RemoteAddr]

RemoteAddr 远程地址匹配

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

Loaded RoutePredicateFactory [Weight]

Weight 权重匹配,接收group和weight两个参数,权重是按照组进行计算的

spring:cloud:gateway:routes:- id: weight_highuri: https://weighthigh.orgpredicates:- Weight=group1, 8- id: weight_lowuri: https://weightlow.orgpredicates:- Weight=group1, 2     #group1是组名, 会有80%的请求访问 https://weighthigh.org,20%的请求访问https://weightlow.org
CloudFoundryRouteService断言

Loaded RoutePredicateFactory [CloudFoundryRouteService]

配置方式

gateway配置断言有两种方式

以Cookie断言为例,Cookie断言的配置类如下

java">public static class Config {@NotEmptyprivate String name;@NotEmptyprivate String regexp;public String getName() {return name;}public Config setName(String name) {this.name = name;return this;}public String getRegexp() {return regexp;}public Config setRegexp(String regexp) {this.regexp = regexp;return this;}}
  • 简短配置

    断言名称和参数之间是=,多个参数之间,相隔,按照定义的顺序依次赋值

    spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- Cookie=mycookie,mycookievalue
    
  • 全面扩展参数

    每个参数都使用 字段名:字段值 表示

    spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- name: Cookieargs:name: mycookieregexp: mycookievalue
    

https://zhhll.icu/2021/框架/微服务/springcloud/网关/GateWay/2.GateWay断言/


http://www.ppmy.cn/server/107666.html

相关文章

SpringBoot下获取resources目录下文件的常用方法

哈喽,大家好,今天给大家带来SpringBoot获取resources目录下文件的常用方法,示例中的方法是读取resources目录下的txt和xlsx文件,并将xlsx导出到excel的简单写法。完整代码放在最后。 通过this.getClass()方法获取 method1 - met…

Verilog刷题笔记60

题目: Exams/2013 q2bfsm Consider a finite state machine that is used to control some type of motor. The FSM has inputs x and y, which come from the motor, and produces outputs f and g, which control the motor. There is also a clock input called …

【YOLOv8改进[Conv]】 感受野注意力卷积RFAConv(2024.3)| 使用RFAConv改进C2f + 含全部代码和详细修改方式

本文将进行在YOLOv8中使用 感受野注意力卷积RFAConv改进C2f 的实践,助力YOLOv8目标检测效果,文中含全部代码、详细修改方式。助您轻松理解改进的方法。

C++练习题:进阶算法——二分查找

第一部分:考点与作答区 考点: 查找算法的概念二分查找的原理二分查找的实现 作答区: 编写一个C程序,完成以下要求: 使用二分查找算法在一个整型数组中查找一个元素。打印查找结果。 请在下方空白处编写代码&#…

景芯SoC A72实战反馈

先说结论: 内容非常全面,讲解到位,会有专门的工程师一对一答疑,整个项目跑下来提升非常大,绝对物超所值! 一些细节: 本人微电子专业研一在读,有过两次简单的数字芯片流片经历&…

Systemc example based on VCS

README VCS example path: $VCS_HOME/doc/examples/systemc/ SYSTEMC_HOME: module load systemc(or 自己download systemc, VCS_HOME下应该也有:$VCS_HOME/include/systemc233/tlm_utils) gcc需要是7.3.0版本,module load gcc/7.3.0 注意事项 vcs …

MYSQL:简述对B树和B+树的认识

MySQL的索引使用B树结构。 1、B树 在说B树之前,先说说B树,B树是一个多路平衡查找树,相较于普通的二叉树,不会发生极度不平衡的状况,同时也是多路的。 B树的特点是:他会将数据也保存在非叶子节点。而这个…

AI智能大数据分析足球AIAutoPrediction,提高足球比赛预测准确度的新方法

本文摘要:一、I智能大数据分析足球的原理I智能大数据分析足球的原理是利用机器学习和大数据分析技术,对足球比赛的各种数据进行分析和预测。这些数据包括球队历史成绩、球员数据、场地... 一、I智能大数据分析足球的原理 I智能大数据分析足球的原理是利…