欢送拜访我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;
本篇概览
- 本文是《Spring Cloud Gateway实战》系列的第四篇,咱们将已有的断言(predicate)的类型做个小结,明天的内容中,除了官网举荐的简化版配置,还给出了动静路由时该断言的JSON格局配置;
After
- <font color="blue">After</font>示意路由在指定工夫之后才失效
- 配置文件,留神工夫字符串的格局,<font color="blue">+08:00</font>示意东八区:
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格局,留神<font color="blue">args</font>参数要用<font color="red">datetime</font>:
[ { "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
- <font color="blue">Before</font>示意路由在指定工夫之前才失效
- 配置文件:
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格局,留神<font color="blue">args</font>参数要用<font color="red">datetime</font>:
[ { "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
- <font color="blue">Between</font>示意路由在指定时间段之内才失效,既然是时间段就是两个参数,留神它们的写法
- 配置文件:
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格局,留神<font color="blue">args</font>参数,起始工夫是<font color="red">datetime1</font>,完结工夫是<font color="red">datetime2</font>:
[ { "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
- <font color="blue">Cookie</font>示意cookie存在指定名称,并且对应的值合乎指定正则表达式,才算匹配胜利
配置文件:
spring:cloud: gateway: routes: - id: cookie_route uri: https://example.org predicates: - Cookie=chocolate, ch.p
- 动静路由的JSON格局,留神<font color="blue">args</font>参数:
[ { "id": "cookie_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Cookie", "args": { "name": "chocolate", "regexp": "ch.p" } } ] }]
Header
- <font color="blue">Header</font>示意header存在指定名称,并且对应的值合乎指定正则表达式,才算匹配胜利
- 上面的例子要求header中必须存在<font color="blue">X-Request-Id</font>,并且值肯定要是数字
- 配置文件:
spring: cloud: gateway: routes: - id: header_route uri: https://example.org predicates: - Header=X-Request-Id, \d+
- 动静路由的JSON格局,留神<font color="blue">args</font>参数是<font color="red">header</font>和<font color="red">regexp</font>,还要留神的是regexp的值外面有两个反斜杠(本义问题):
[ { "id": "header_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Header", "args": { "header": "X-Request-Id", "regexp": "\\d+" } } ] }]
- 用postman测试的参数填写和后果如下:
Host
- <font color="blue">Host</font>示意申请的host要和指定的字符串匹配,并且对应的值合乎指定正则表达式,才算匹配胜利,能够同时指定多个host匹配表达式,上面的例子给了两个,其中第一个指定了端口:
- 配置文件:
spring: cloud: gateway: routes: - id: host_route uri: http://127.0.0.1:8082 predicates: - Host=test.com:8081,**.anotherhost.org
- 动静路由的JSON格局,留神<font color="blue">args</font>参数,另外通过实测发现,这里regex的值是个正则表达式,因而下面配置文件中的多个host,在此处要通过正则表达式的写法实现(json数组的写法,在反序列化的时候总是出现异常,无奈解析胜利):
[ { "id": "header_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Host", "args": { "regex": "test.com:8086" } } ] }]
Method
- <font color="blue">Method</font>十分好了解,匹配指定的办法类型(能够有多个)
- 配置文件:
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
- <font color="blue">Path</font>很罕用,匹配指定的办法类型(能够有多个)
- 配置文件,留神<font color="blue">{segment}</font>,示意该地位的实在值能够被提取进去,在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
- <font color="blue">Query</font>匹配的是申请中是否带有指定的参数,也能要求该参数等于指定的值(正则表达式)才被匹配上
- 配置文件,只有带有名为<font color="blue">name</font>的申请参数就被匹配:
spring: cloud: gateway: routes: - id: query_route uri: http://127.0.0.1:8082 predicates: - Query=name
- 如下所示,还能够指定name参数的值必须<font color="blue">aaa.</font>,这个小数点示意匹配一个字符,例如<font color="blue">name=aaa1</font>或者<font color="blue">name=aaa2</font>都能够:
spring: cloud: gateway: routes: - id: query_route uri: http://127.0.0.1:8082 predicates: - Query=name,aaa.
- 动静路由的JSON格局,留神参数名和参数值别离用<font color="blue">param</font>和<font color="blue">regexp</font>来设置:
[ { "id": "query_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Query", "args": { "param": "name", "regexp": "aaa." } } ] }]
- 测试如下:
RemoteAddr
- <font color="blue">RemoteAddr</font>很好了解,匹配的指定起源的申请
- 配置文件:
spring: cloud: gateway: routes: - id: remoteaddr_route uri: http://127.0.0.1:8082 predicates: - RemoteAddr=192.168.50.134/24
- 动静路由的JSON格局,留神参数名和参数值别离用<font color="blue">param</font>和<font color="blue">regexp</font>来设置:
[ { "id": "remoteaddr_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "RemoteAddr", "args": { "sources": "192.168.50.134/24" } } ] }]
- 测试如下,留神测试的时候主机地址不要用<font color="blue">localhost</font>和<font color="blue">127.0.0.1</font>,这样会导致服务端判断起源的时候获得的网卡地址为0.0.0.0:
Weight
- <font color="blue">Weight</font>顾名思义,依照权重将申请散发到不同地位
- 配置文件:
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
- 以上就是罕用的断言类型了,可见性能曾经很弱小了,心愿能给您一些参考
你不孤独,欣宸原创一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 数据库+中间件系列
- DevOps系列
欢送关注公众号:程序员欣宸
微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界...
https://github.com/zq2599/blog_demos