【转载】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实战) 今天我们就来聊一聊另一个 ...
随机推荐
- KubeSphere 社区双周报 | 杭州站 Meetup 议题征集中 | 2023.04.14-04.27
KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...
- 欢迎来到IoT解忧杂货铺
这是一间特殊的杂货铺 门面不大,却包罗万物 如果你也遇到一些烦恼 欢迎来到,IoT解忧杂货铺 解忧秘方·工业 厂里的几十台设备真让人头疼 协议种类太多太复杂 设备没法全联网 产线故障了也不知道 自己出 ...
- 国产东方通消息队列TongLINKQ8.1服务端安装步骤
一.服务端安装 groupadd tlq # 新建组 useradd -m -g tlq tlq # 新建tlq用户并指定组tlq cd /home/tlq/ # 切换到安装目录并上传安装包 tar ...
- 在使用asm包进行动态类加载的时候的打包问题
如图所示,开发时使用的jdk包下面的asm包,在进行打包时提示asm包不存在,打包方式使用如下: 目前提供两种解决方案: 1:修改打包方式,将jdk的包也打进去: <plugin> < ...
- Diffuision Policy + RL -------个人博客_ZSY_20241101
Diffusion Policy: Visuomotor Policy Learning via Action Diffusion Cheng Chi, Zhenjia Xu, Siyuan Feng ...
- 深度学习系列之1----直观解释Transformer
Abstract 这个系列主要用来记录我自己这种的AI小白的学习之路,通过将所学所知总结下来,记录下来.之前总喜欢记录在笔记本上,或者ipad上,或者PC端的Typora上,但总是很难回头检索到一些系 ...
- 认识鸿蒙Context
Context是应用中对象的上下文,其提供了应用的一些基础信息,例如resourceManager(资源管理).applicationInfo(当前应用信息).dir(应用文件路径).area(文件分 ...
- c语言小练习——字符串长度、拷贝、拼接、比较
/* 使用c语言知识实现下面程序: 1,实现strlen函数的功能 2,实现strcpy函数的功能 3,实现strcat函数的功能 4,实现strcmp函数的功能 不允许使用已有的str函数*/ 1 ...
- 不同团队如何实现登录系统 (just for fun)
某一天 ceo 需要一个登录系统,找了开发团队 控制狂团队 领导点了卡布奇诺,打开了自己轻薄的 mac book, 点开 word 文档, 开始编写: 1. 项目背景 2. 名词解析 3. 数据表设计 ...
- Halo 正式开源: 使用可穿戴设备进行开源健康追踪
在飞速发展的可穿戴技术领域,我们正处于一个十字路口.市场上充斥着各式时尚.功能丰富的设备,声称能够彻底改变我们对健康和健身的方式.然而,在这些光鲜的外观和营销宣传背后,隐藏着一个令人担忧的现实:大多数 ...