关键词:
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
- 本文是《Spring Cloud Gateway实战》系列的第四篇,咱们将已有的断言(predicate)的类型做个小结,今天的内容中,除了官方推荐的简化版配置,还给出了动态路由时该断言的JSON格式配置;
After
- After表示路由在指定时间之后才生效
- 配置文件,注意时间字符串的格式,+08:00表示东八区:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://127.0.0.1:8082
predicates:
- After=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
- 动态路由的JSON格式,注意args参数要用datetime:
[
"id": "after_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "After",
"args":
"datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
]
]
Before
- Before表示路由在指定时间之前才生效
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: before_route
uri: http://127.0.0.1:8082
predicates:
- Before=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
- 动态路由的JSON格式,注意args参数要用datetime:
[
"id": "before_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Before",
"args":
"datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
]
]
Between
- Between表示路由在指定时间段之内才生效,既然是时间段就是两个参数,注意它们的写法
- 配置文件:
spring:
application:
name: hello-gateway
cloud:
gateway:
routes:
- id: between_route
uri: http://127.0.0.1:8082
predicates:
- Between=2021-08-16T07:36:00.000+08:00[Asia/Shanghai], 2021-08-16T08:15:00.000+08:00[Asia/Shanghai]
- 动态路由的JSON格式,注意args参数,起始时间是datetime1,结束时间是datetime2:
[
"id": "path_route_addr",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Between",
"args":
"datetime1": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]",
"datetime2": "2021-08-16T08:18:00.000+08:00[Asia/Shanghai]"
]
]
Cookie
- Cookie表示cookie存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
- 动态路由的JSON格式,注意args参数:
[
"id": "cookie_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Cookie",
"args":
"name": "chocolate",
"regexp": "ch.p"
]
]
Header
- Header表示header存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
- 下面的例子要求header中必须存在X-Request-Id,并且值一定要是数字
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \\d+
- 动态路由的JSON格式,注意args参数是header和regexp,还要注意的是regexp的值里面有两个反斜杠(转义问题):
[
"id": "header_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Header",
"args":
"header": "X-Request-Id",
"regexp": "\\\\d+"
]
]
用postman测试的参数填写和结果如下:
Host
- Host表示请求的host要和指定的字符串匹配,并且对应的值符合指定正则表达式,才算匹配成功,可以同时指定多个host匹配表达式,下面的例子给了两个,其中第一个指定了端口:
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://127.0.0.1:8082
predicates:
- Host=test.com:8081,**.anotherhost.org
- 动态路由的JSON格式,注意args参数,另外通过实测发现,这里regex的值是个正则表达式,因此上面配置文件中的多个host,在此处要通过正则表达式的写法实现(json数组的写法,在反序列化的时候总是出现异常,无法解析成功):
[
"id": "header_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Host",
"args":
"regex": "test.com:8086"
]
]
Method
- Method非常好理解,匹配指定的方法类型(可以有多个)
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://127.0.0.1:8082
predicates:
- Method=GET,POST
- 动态路由的JSON格式,同样,由于个人水平问题,暂时只实践出指定单个方法的JSON写法,如果你知道如何指定过个方法,还望告知,谢谢:
[
"id": "method_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Method",
"args":
"methods": "GET"
]
]
Path
- Path很常用,匹配指定的方法类型(可以有多个)
- 配置文件,注意segment,表示该位置的真实值可以被提取出来,在filter中可以使用,这在后续的filter文章中会有说明:
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/segment,/lbtest/segment
- 动态路由的JSON格式,同样,由于个人水平问题,暂时只实践出指定单个方法的JSON写法,如果你知道如何指定过个方法,还望告知,谢谢:
[
"id": "path_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Path",
"args":
"pattern": "/hello/segment"
]
]
Query
- Query匹配的是请求中是否带有指定的参数,也能要求该参数等于指定的值(正则表达式)才被匹配上
- 配置文件,只要带有名为name的请求参数就被匹配:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://127.0.0.1:8082
predicates:
- Query=name
- 如下所示,还可以指定name参数的值必须aaa.,这个小数点表示匹配一个字符,例如name=aaa1或者name=aaa2都可以:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://127.0.0.1:8082
predicates:
- Query=name,aaa.
- 动态路由的JSON格式,注意参数名和参数值分别用param和regexp来设置:
[
"id": "query_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "Query",
"args":
"param": "name",
"regexp": "aaa."
]
]
- 测试如下:
RemoteAddr
- RemoteAddr很好理解,匹配的指定来源的请求
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://127.0.0.1:8082
predicates:
- RemoteAddr=192.168.50.134/24
- 动态路由的JSON格式,注意参数名和参数值分别用param和regexp来设置:
[
"id": "remoteaddr_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
"name": "RemoteAddr",
"args":
"sources": "192.168.50.134/24"
]
]
- 测试如下,注意测试的时候主机地址不要用localhost和127.0.0.1,这样会导致服务端判断来源的时候取得的网卡地址为0.0.0.0:
Weight
- Weight顾名思义,按照权重将请求分发到不同位置
- 配置文件:
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: http://192.168.50.134:8082
predicates:
- Weight=group1, 8
- id: weight_low
uri: http://192.168.50.135:8082
predicates:
- Weight=group1, 2
- 以上就是常用的断言类型了,可见功能已经很强大了,希望能给您一些参考
你不孤单,欣宸原创一路相伴
springcloudgateway实战之五:内置filter(代码片段)
...套源码):https://github.com/zq2599/blog_demos本篇概览作为《SpringCloudGateway实战》系列的第五篇,是时候了解过滤器(filter)的作用了,本篇咱们一起来了解SpringCloudGateway内置好的过滤器,真是种类繁多功能强大 查看详情
springcloudgateway实战之五:内置filter(代码片段)
...套源码):https://github.com/zq2599/blog_demos本篇概览作为《SpringCloudGateway实战》系列的第五篇,是时候了解过滤器(filter)的作用了,本篇咱们一起来了解SpringCloudGateway内置好的过滤器,真是种类繁多功能强大 查看详情
springcloudgateway自定义过滤器实战(观测断路器状态变化)(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第七篇,前面的文章咱们学习了各种内置过滤器,还在《SpringCloudGateway的断路器(CircuitBreaker)功能》一文深入研究了断路器 查看详情
springcloudgateway自定义过滤器实战(观测断路器状态变化)(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第七篇,前面的文章咱们学习了各种内置过滤器,还在《SpringCloudGateway的断路器(CircuitBreaker)功能》一文深入研究了断路器 查看详情
springcloudgateway实战之一:初探(代码片段)
...创(含配套源码):https://github.com/zq2599/blog_demos关于《SpringCloudGateway实战》系列《SpringCloudGateway实战》是欣宸在Java领域的系列原创,旨在通过项目实战与大家一起学习和掌握SpringCloudGateway,更好的为实际项 查看详情
springcloudgateway实战之一:初探(代码片段)
...创(含配套源码):https://github.com/zq2599/blog_demos关于《SpringCloudGateway实战》系列《SpringCloudGateway实战》是欣宸在Java领域的系列原创,旨在通过项目实战与大家一起学习和掌握SpringCloudGateway,更好的为实际项 查看详情
springcloudgateway内置的过滤器工厂(代码片段)
SpringCloudGateway内置的过滤器工厂内置的过滤器工厂这里简单将SpringCloudGateway内置的所有过滤器工厂整理成了一张表格。如下:过滤器工厂作用参数AddRequestHeader为原始请求添加HeaderHeader的名称及值AddRequestParameter为原始请求添... 查看详情
springcloudgateway限流实战(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第八篇,经过前面的学习,咱们对过滤器已了解得差不多,今天来补全过滤器的最后一个版块:限流(RequestRat 查看详情
springcloudgateway限流实战(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第八篇,经过前面的学习,咱们对过滤器已了解得差不多,今天来补全过滤器的最后一个版块:限流(RequestRat 查看详情
springcloudgateway实战之二:更多路由配置方式(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第二篇,通过前文咱们了解到SpringCloudGateway的核心是路由配置,然后在本地application.yml中配置了一条路由, 查看详情
springcloudgateway实战之二:更多路由配置方式(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第二篇,通过前文咱们了解到SpringCloudGateway的核心是路由配置,然后在本地application.yml中配置了一条路由, 查看详情
springcloud实战springcloudgateway服务网关
gitee地址:https://gitee.com/javaxiaobear/spring-cloud_study.git在线阅读地址:https://javaxiaobear.gitee.io/官网:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/该项目提供了一个用于在SpringWebFlux之上构 查看详情
springcloudgateway实战之三:动态路由(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第三篇,前文介绍了多种路由配置方式,它们存在一个共同问题:路由配置变更后必须重启Gateway应用才能生效,聪明的您一下就... 查看详情
springcloudgateway实战之三:动态路由(代码片段)
...源码):https://github.com/zq2599/blog_demos本篇概览本文是《SpringCloudGateway实战》系列的第三篇,前文介绍了多种路由配置方式,它们存在一个共同问题:路由配置变更后必须重启Gateway应用才能生效,聪明的您一下就... 查看详情
乐字节java8核心特性实战之四:方法引用
student_score表:+------+---------+-------+|name|subject|score|+------+---------+-------+|张三|语文|78||张三|数学|88||张三|英语|98||李四|语文|89||李四|数学|76||李四|英语|90||王五|语文|89||王五|数学|66||王五|英语|91|+------+---------+----- 查看详情
springcloudgateway学习笔记-使用内置过滤器添加请求头响应头(代码片段)
SpringCloudGateway学习笔记-使用内置过滤器添加请求头、响应头Gateway配置测试接口结果请求参数请求头响应头参考资料Gateway配置spring:cloud:gateway:#路由规则routes:-id:aaa_header_route#匹配以下路径predicates:-Path=/hi/name#转发到:uri:http... 查看详情
springcloudgateway学习笔记-使用内置过滤器添加请求头响应头(代码片段)
SpringCloudGateway学习笔记-使用内置过滤器添加请求头、响应头Gateway配置测试接口结果请求参数请求头响应头参考资料Gateway配置spring:cloud:gateway:#路由规则routes:-id:aaa_header_route#匹配以下路径predicates:-Path=/hi/name#转发到:uri:http... 查看详情
springcloudgateway限流实战,终于有人写清楚了!(代码片段)
话说在SpringCloudGateway问世之前,SpringCloud的微服务世界里,网关一定非NetflixZuul莫属。但是由于Zuul1.x存在的一些问题,比如阻塞式的API,不支持WebSocket等,一直被人所诟病,而且Zuul升级新版本依赖于Netflix... 查看详情