【转载】Spring Cloud Gateway-路由谓词工厂详解(Route Predicate Factories)
http://www.imooc.com/article/290804
TIPS
本文基于Spring Cloud Greenwich SR2编写,兼容Spring Cloud Finchley及更高版本。
这一节来详细探讨Spring Cloud Gateway的路由谓词工厂 (Route Predicate Factories),路由谓词工厂的作用是:符合Predicate的条件,就使用该路由的配置,否则就不管。 只要掌握这一句,掌握路由谓词工厂就比较轻松了。
TIPS
Predicate是Java 8提供的一个函数式编程接口。
本文探讨了Spring Cloud Gateway中内置的谓词工厂,包括:
| 谓词工厂 |
|---|
| After |
| Before |
| Between |
| Cookie |
| Header |
| Host |
| Method |
| Path |
| Query |
| RemoteAddr |
路由配置的两种形式
先来探讨Spring Cloud Gateway路由配置的两种姿势:
路由到指定URL
示例1:通配
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: http://www.itmuch.com
表示访问 GATEWAY_URL/** 会转发到 http://www.itmuch.com/**
TIPS
这段配置不能直接使用,需要和下面的Predicate配合使用才行。
示例2:精确匹配
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/
表示访问 GATEWAY_URL/spring-cloud/spring-cloud-stream-pan-ta/ 会转发到 http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/
TIPS
这段配置不能直接使用,需要和下面的Predicate配合使用才行。
路由到服务发现组件上的微服务
示例1:通配
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: lb://user-center
表示访问 GATEWAY_URL/** 会转发到 user-center 微服务的 /**
TIPS
这段配置不能直接使用,需要和下面的Predicate配合使用才行。
示例2:精确匹配
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: lb://user-center/shares/1
表示访问 GATEWAY_URL/shares/1 会转发到 user-center 微服务的 /shares/1
TIPS
这段配置不能直接使用,需要和下面的Predicate配合使用才行。
谓词工厂详解
下面正式探讨路由谓词工厂。Spring Cloud Gateway提供了十来种路由谓词工厂。为网关实现灵活的转发提供了基石。
After
示例:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: lb://user-center
predicates:
# 当且仅当请求时的时间After配置的时间时,才会转发到用户微服务
# 目前配置不会进该路由配置,所以返回404
# 将时间改成 < now的时间,则访问localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- After=2030-01-20T17:42:47.789-07:00[America/Denver]
TIPS
- 技巧:时间可使用
System.out.println(ZonedDateTime.now());打印,然后即可看到时区。例如:2019-08-10T16:50:42.579+08:00[Asia/Shanghai]- 时间格式的相关逻辑:
- 默认时间格式:org.springframework.format.support.DefaultFormattingConversionService#addDefaultFormatters
- 时间格式注册:org.springframework.format.datetime.standard.DateTimeFormatterRegistrar#registerFormatters
Before
示例:
spring:
cloud:
gateway:
routes:
- id: before_route
uri: lb://user-center
predicates:
# 当且仅当请求时的时间Before配置的时间时,才会转发到用户微服务
# 目前配置不会进该路由配置,所以返回404
# 将时间改成 > now的时间,则访问localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Before=2018-01-20T17:42:47.789-07:00[America/Denver]
Between
示例:
spring:
cloud:
gateway:
routes:
- id: between_route
uri: lb://user-center
predicates:
# 当且仅当请求时的时间Between配置的时间时,才会转发到用户微服务
# 因此,访问localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver]
Cookie
示例:
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: lb://user-center
predicates:
# 当且仅当带有名为somecookie,并且值符合正则ch.p的Cookie时,才会转发到用户微服务
# 如Cookie满足条件,则访问http://localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Cookie=somecookie, ch.p
Header
spring:
cloud:
gateway:
routes:
- id: header_route
uri: lb://user-center
predicates:
# 当且仅当带有名为X-Request-Id,并且值符合正则\d+的Header时,才会转发到用户微服务
# 如Header满足条件,则访问http://localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Header=X-Request-Id, \d+
Host
spring:
cloud:
gateway:
routes:
- id: host_route
uri: lb://user-center
predicates:
# 当且仅当名为Host的Header符合**.somehost.org或**.anotherhost.org时,才会转发用户微服务
# 如Host满足条件,则访问http://localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Host=**.somehost.org,**.anotherhost.org
Method
spring:
cloud:
gateway:
routes:
- id: method_route
uri: lb://user-center
predicates:
# 当且仅当HTTP请求方法是GET时,才会转发用户微服务
# 如请求方法满足条件,访问http://localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Method=GET
Path
spring:
cloud:
gateway:
routes:
- id: path_route
uri: lb://user-center
predicates:
# 当且仅当访问路径是/users/*或者/some-path/**,才会转发用户微服务
# segment是一个特殊的占位符,单层路径匹配
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- Path=/users/{segment},/some-path/**
TIPS
建议大家看下这一部分的官方文档,里面有个segment编程技巧。比较简单,留个印象。
https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_path_route_predicate_factory
Query
示例1:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: lb://user-center
predicates:
# 当且仅当请求带有baz的参数,才会转发到用户微服务
# eg. 访问http://localhost:8040/users/1?baz=xx -> user-center的/users/1
- Query=baz
示例2:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: lb://user-center
predicates:
# 当且仅当请求带有名为foo的参数,且参数值符合正则ba.,才会转发到用户微服务
# eg. 访问http://localhost:8040/users/1?baz=baz -> user-center的/users/1?baz=baz
- Query=foo, ba.
RemoteAddr
示例:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: lb://user-center
predicates:
# 当且仅当请求IP是192.168.1.1/24网段,例如192.168.1.10,才会转发到用户微服务
# eg. 访问http://localhost:8040/users/1 -> user-center的/users/1
- RemoteAddr=192.168.1.1/24
TIPS
建议大家看下这一部分的官方文档,有个小编程技巧。比较简单,留个印象。
https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_remoteaddr_route_predicate_factory
相关代码

本文首发
【转载】Spring Cloud Gateway-路由谓词工厂详解(Route Predicate Factories)的更多相关文章
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂
在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...
- Spring Cloud Gateway - 路由法则
1. After Route Predicate Factory 输入一个参数:时间,匹配该时间之后的请求,示例配置: spring: cloud: gateway: routes: - id: af ...
- (转载)spring单例和多例详解。如何在单例中调用多例对象
spring生成对象默认是单例的.通过scope属性可以更改为多例. <bean id="user" class="modle.User" scope=& ...
- Spring Cloud Gateway(六):路由谓词工厂 RoutePredicateFactory
本文基于 spring cloud gateway 2.0.1 1.简介 Spring Cloud Gateway 创建 Route 对象时, 使用 RoutePredicateFactory 创建 ...
- 微服务网关 Spring Cloud Gateway
1. 为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...
- Spring Cloud gateway 网关服务二 断言、过滤器
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂
Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...
- spring cloud gateway 深入了解 - Predicate
文章来源 spring cloud gateway 通过谓词(Predicate)来匹配来自用户的请求 为了方便,使用postman测试不同的谓词的效果 路径谓词(Predicate)—— 最简单的谓 ...
- 看完就会的Spring Cloud Gateway
在前面几节,我给大家介绍了当一个系统拆分成微服务后,会产生的问题与解决方案:服务如何发现与管理(Nacos注册中心实战),服务与服务如何通信(Ribbon, Feign实战) 今天我们就来聊一聊另一个 ...
随机推荐
- element输入天数,获取当前时间加上天数 【时间获取】
handleInput (val) { // console.log(this.formModel.ITEM_PM) if (!(/[^\d]/g).test(val)) { // console.l ...
- 57.dom递归退出循环的时机
递归的终止条件一般定义在递归函数内部,在递归调用前要做一个条件判断,根据判断的结果选择是继续调用自身,还是return:返回终止递归. 终止的条件: 1.判断递归的次数是否达到某一限定值 2.判断运算 ...
- 云原生爱好者周刊:VMware Tanzu 社区办发布,无任何限制!
云原生一周动态要闻: VMware Tanzu 推出社区版 Kubernetes Cluster API 1.0 版已生产就绪 Linkerd 2.11 发布 Cartografos 工作组推出云原生 ...
- Ubuntu 22.04 解决和 Windows 共享蓝牙设备的问题
我有一个 Airpods,连接到 WIndows 可以正常工作,但连接到 ubuntu 后会无法连接,只能删除设备选择重联,但是这又会导致 Windows 不能连接到耳机,只能也删除重新连接,费神费力 ...
- 为Markdown/HTML文档生成一个简易目录
现在阅览文章的网页往往都带有一个目录,方便点击跳转.目录一般都是根据文档中的标题级别直接生成的. 现在我们也来模仿一个简单的,无非就是把<h1><h2>...的序列转化成树嘛 ...
- 【2024有效】WordPress忘记密码找回登录密码的最简单有效的方法
这个找回Wordpress后台密码密的方法,前提是,可以操作数据. 最近忘记了极客侠网站登陆密码,还是按照以前的方法,进入数据库直接修改数据库, 但是现在wordpress密码的加密不是简单的MD5所 ...
- C语言(从入门到入土)
1.C语言整体上需要记住的 总体上必须清楚的: 1)程序结构是三种: 顺序结构 , 循环结构 (三个循环结构), 选择结构 (if 和 switch) 2)读程序都要从main()入口, 然后从最上面 ...
- Ubuntu安装Edge浏览器,好用的浏览器!!
秉持着简介的原则,我这里把重要的步骤记录下来,减少废话的使用量,大大缩短你们看的时间,好吧.. 步骤 首先,使用以下命令更新您的系统: sudo apt update 然后,使用以下命令安装Micro ...
- base64编码图片二进制数据后直接保存在html文件中
相关内容: 在markdown编辑器中嵌入base64图片 看到一个帖子,那就是base64编码用来http服务中对二进制文件编码,那么可以不可以直接在html文件中使用base64编码后的字符串来表 ...
- 5.Kubeadm和二进制方式对比
Kubeadm方式搭建K8S集群 安装虚拟机,在虚拟机安装Linux操作系统[3台虚拟机] 对操作系统初始化操作 所有节点安装Docker.kubeadm.kubelet.kubectl[包含mast ...