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)的更多相关文章

  1. SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断

    1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...

  2. Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...

  3. Spring Cloud Gateway - 路由法则

    1. After Route Predicate Factory 输入一个参数:时间,匹配该时间之后的请求,示例配置: spring: cloud: gateway: routes: - id: af ...

  4. (转载)spring单例和多例详解。如何在单例中调用多例对象

    spring生成对象默认是单例的.通过scope属性可以更改为多例. <bean id="user" class="modle.User" scope=& ...

  5. Spring Cloud Gateway(六):路由谓词工厂 RoutePredicateFactory

    本文基于 spring cloud gateway 2.0.1 1.简介 Spring Cloud Gateway 创建 Route 对象时, 使用 RoutePredicateFactory 创建 ...

  6. 微服务网关 Spring Cloud Gateway

    1.  为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...

  7. Spring Cloud gateway 网关服务二 断言、过滤器

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  8. Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂

    Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...

  9. spring cloud gateway 深入了解 - Predicate

    文章来源 spring cloud gateway 通过谓词(Predicate)来匹配来自用户的请求 为了方便,使用postman测试不同的谓词的效果 路径谓词(Predicate)—— 最简单的谓 ...

  10. 看完就会的Spring Cloud Gateway

    在前面几节,我给大家介绍了当一个系统拆分成微服务后,会产生的问题与解决方案:服务如何发现与管理(Nacos注册中心实战),服务与服务如何通信(Ribbon, Feign实战) 今天我们就来聊一聊另一个 ...

随机推荐

  1. iOS使用SourceTree回滚回滚小结

    代码回滚,适用于的场景: 1.提交错代码,想放弃刚刚提交的部分:2.代码发生冲突,处理比较麻烦,为了代码安全,直接回滚到之前干净的代码.我个人理解,可以分为本地回滚和远程回滚: 一.本地回滚,回滚自己 ...

  2. C# 的显示转换 *.Parse(string) Convert.ToInt32(double value)

    // 显式转换 (类型) /// (int)表示使用强制的显示转换,是一种类型转换,C#默认的整形是 int32 , /// 因此使用此方法转成int32 不遵循四舍五入,直截取整数部分 /// (i ...

  3. DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function (header, parser) { header = normalizeHeader(header);

    场景:token过期,然后更新了token 重新发起请求获取数据 : 代码:使用上一次的错误请求配置报错     return request(error.config) : 解决 :  return ...

  4. kotlin基础——>基本数据类型、控制流、返回和跳转

    1.对于数字的定义,支持java 8的下划线分割的方式 val a = 1_2_3 与 val a = 123 是相同的 2.如果要指定数字类型有两种方式 val a : Float = 1 或者 v ...

  5. 手写MSMQ微软消息队列收发工具类

    一.MSMQ介绍 MSMQ(Microsoft Message Queuing)是微软开发的消息队列技术,支持事务,支持异步发送和接收消息. 两个重要的概念:队列和消息.队列是存放消息的容器和传输消息 ...

  6. Java学习十六—掌握注解:让编程更简单

    一.关于注解 1.1 简介 Java 注解(Annotation)是一种特殊的语法结构,可以在代码中嵌入元数据.它们不直接影响代码的运行,但可以通过工具和框架提供额外的信息,帮助在编译.部署或运行时进 ...

  7. macOS安装使用OpenConnect客户端替代cisco连接公司内网环境

    mac_os安装openconnect服务 brew install openconnect 使用OpenConnect客户端拨通VPN,打开终端执行以下命令: sudo openconnect -u ...

  8. manim边做边学--圆环面

    Torus类在制作数学.物理或工程领域的动画时具有广泛的应用场景. 比如,通过动态演示环面的拓扑变换(如内外翻转.扭曲等),帮助我们直观地理解拓扑不变量和同胚等概念: 此外,也可以模拟磁场线在环面导体 ...

  9. canvas绘制--圆角多边形

    context.arcTo() arcTo() 方法在画布上创建介于两个切线之间的弧/曲线. JavaScript 语法: context.arcTo(x1,y1,x2,y2,r); 参数描述 参数 ...

  10. 【昌哥IT课堂】MySQL8.0新特性之不可见主键

    一.概述作为 MySQL DBA ,相信大家都经历过在复制模式下,如果没有主键,遇到 load data ,大事务,ddl 等有大量表数据行扫描的行为时,会带来严重的主从延迟,给数据库稳定性和数据一致 ...