当前位置:网站首页>[micro service SCG] use of predict
[micro service SCG] use of predict
2022-07-04 21:02:00 【Bulst】
List of articles
To put it bluntly Predicate Just to implement a set of matching rules , It is convenient for the request to come and find the corresponding one Route To deal with , So let's move on Spring Cloud GateWay Built in several Predicate Use .
Through time matching
Predicate Support for setting a time , When the request is forwarded , It can be judged that before this time 、 Forward later or between . So let's say that we're only setting in 2022 year 7 month 5 Will be forwarded to my website , No forwarding until then , I can configure it like this :
spring:
cloud:
gateway:
routes:
- id: time_route
uri: http://issavior.com
predicates:
- After=2022-07-05T06:06:06+08:00[Asia/Shanghai]
Spring It's through ZonedDateTime To compare the time ,ZonedDateTime yes Java 8 In the date and time function , Class used to represent date and time information with time zone ,ZonedDateTime Support for setting time by time zone , China's time zone is :Asia/Shanghai.
After Route Predicate Requests after this time are forwarded to the destination address . The above example refers to , Request time is 2022 year 7 month 5 Japan 6 spot 6 branch 6 All requests after seconds are forwarded to the address http://issavior.com.+08:00 It means time and UTC The time difference is eight hours , The time area is Asia/Shanghai.
After adding the routing rules , Access address http://localhost:8080 Automatically forwarded to http://issavior.com.
Before Route Predicate Just the opposite , Requests made prior to a certain time are forwarded . We put the above routing rules in After Change it to Before, as follows :
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http:http://issavior.com
predicates:
- Before=2022-07-05T06:06:06+08:00[Asia/Shanghai]
That means you can route before that time , Stop routing after this time , After the modification, restart the project and visit the address again http://localhost:8080, The page will say 404 No address found .
Except before or after time ,Gateway It also supports restricting the routing of requests within a certain time frame , have access to Between Route Predicate To achieve .
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://issavior.com
predicates:
- Between=2022-07-05T06:06:06+08:00[Asia/Shanghai], 2022-07-09T06:06:06+08:00[Asia/Shanghai]
This setting means that the path can be matched within this time period , No match will be made beyond this time period . The ability to route by time matching is cool , Can be used in flash sale In some scenes .
adopt Cookie matching
Cookie Route Predicate You can take two parameters , One is Cookie name , One is a regular expression , The routing rules are retrieved by the corresponding Cookie name The value matches the regular expression , If there is a match, the route is executed , If there is no match, it is not executed .
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://itsmdev.com
predicates:
- Cookie=issavior, cool
Use curl test , Command line input :
curl http://localhost:8080 --cookie “issavior=cool”
Returns the page code , If you remove –cookie --cookie “issavior=cool”, Background report 404 error .
Header Route Predicate and Cookie Route Predicate equally , It's also reception 2 Parameters , One header Property name and a regular expression , This property value matches the regular expression .
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://itsmdev.com
predicates:
- Header=X-Request-Id, \d+
Use curl test , Command line input :
curl http://localhost:8080 -H “X-Request-Id:666666”
Returns the page code to prove that the match was successful . The parameter -H "X-Request-Id:666666" Change it to -H "X-Request-Id:neo" Returns when executed again 404 There is no match .
adopt Host matching
Host Route Predicate Receives a set of parameters , A list of matched domain names , This template is one ant Separate templates , use . Sign as the separator . It USES the host address in the parameter as the matching rule .
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://itsmdev.com
predicates:
- Host=**itsmdev.com
Use curl test , Command line input :
curl http://localhost:8080 -H “Host: www.itsmdev.com”
curl http://localhost:8080 -H “Host: mditsmdev.com”
Both were tested host Can be matched to host_route route , Get rid of host The parameter will report 404 error .
Match by request
It can be done by POST、GET、PUT、DELETE And so on different request way to route .
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://itsmdev.com
predicates:
- Method=GET
Use curl test , Command line input :
#curl The default is GET The way to request
curl http://localhost:8080
Test the return page code , Prove a match to route , Let's use POST Way to request a test .
#curl The default is GET The way to request
curl -X POST http://localhost:8080
return 404 Can't find , Prove no match by road
Match by request path
Path Route Predicate Receive a parameter that matches the path to determine whether to walk by .
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://itsmdev.com
predicates:
- Path=/foo/{
segment}
If the request path meets the requirements , The path will match , for example :/foo/1 perhaps /foo/bar.
Use curl test , Command line input :
curl http://localhost:8080/foo/1
curl http://localhost:8080/foo/xx
curl http://localhost:8080/boo/xx
After testing the first and second commands can get the return value of the page normally , Last order 404, Proves that a route is matched by a specified route .
Matches by request parameters
Query Route Predicate Support for passing in two parameters , One is the property name and one is the property value , Property values can be regular expressions .
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://itsmdev.com
predicates:
- Query=smile
This configuration , As long as the request contains smile Property to match the route .
Use curl test , Command line input :
curl localhost:8080?smile=x&id=2
Testing found that as long as the request summary has smile The parameters will then match the route , No smile The arguments don't match .
Can also be Query Is configured as a key-value pair , This will match the property values to the regularization when the request comes through , Match up to be able to walk by .
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://itsmdev.com
predicates:
- Query=keep, pu.
This is as long as included in the request keep Property and the parameter value is pu Strings that start with a length of three digits are matched and routed .
Use curl test , Command line input :
curl localhost:8080?keep=pub
Tests can return page code , take keep The attribute value of pubx Another visit will be reported 404, Prove that the route needs to match a regular expression to be routed .
By request ip Address matching
Predicate Also supports setting something ip Interval segment requests are routed ,RemoteAddr Route Predicate Accept cidr Symbol (IPv4 or IPv6) List of strings ( The minimum size is 1), for example 192.168.0.1/16 ( among 192.168.0.1 yes IP Address ,16 It's the subnet mask ).
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://itsmdev.com
predicates:
- RemoteAddr=192.168.1.1/24
This address can be set to native ip Address test .
As a result, the requested remote address is 192.168.1.10, The path will match .
Weight routing
This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weighlow.org.
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
The XForwarded Remote Addr Route Predicate Factory
This route matches if the X-Forwarded-For header contains, for example, 192.168.1.10.
spring:
cloud:
gateway:
routes:
- id: xforwarded_remoteaddr_route
uri: https://example.org
predicates:
- XForwardedRemoteAddr=192.168.1.1/24
Use a combination of
The above is to demonstrate each one Predicate Use , We did the configuration tests individually , You can actually combine all of these Predicate Combine and use together .
for example :
spring:
cloud:
gateway:
routes:
- id: host_foo_path_headers_to_httpbin
uri: http://itsmdev.com
predicates:
- Host=**.foo.org
- Path=/headers
- Method=GET
- Header=X-Request-Id, \d+
- Query=foo, ba.
- Query=baz
- Cookie=chocolate, ch.p
- After=2022-1-01T06:06:06+08:00[Asia/Shanghai]
Various Predicates Exists simultaneously on the same route , The request must satisfy all the criteria at the same time to be matched by this route .
When a request satisfies predicate conditions for multiple routes , Requests are forwarded only by the first successfully matched route .
边栏推荐
- BFC面试简述
- 二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
- Common verification rules of form components -1 (continuously updating ~)
- Ten years' experience of byte test engineer directly hits the pain point of UI automation test
- 伦敦银走势图分析的新方法
- 网络命名空间
- Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- Redis分布式锁的实现
- [1200. Différence absolue minimale]
猜你喜欢
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
Pytorch---使用Pytorch实现LinkNet进行语义分割
看腾讯大老如何做接口自动化测试
idea配置标准注释
[server data recovery] a case of RAID5 data recovery stored in a brand of server
托管式服务网络:云原生时代的应用体系架构进化
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
Common verification rules of form components -1 (continuously updating ~)
随机推荐
acwing 3302. 表达式求值
测试员的算法面试题-找众数
go语言笔记(2)go一些简单运用
Win11无法将值写入注册表项如何解决?
Managed service network: application architecture evolution in the cloud native Era
接口设计时的一些建议
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
LeetCode 8. 字符串转换整数 (atoi)
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
GVM使用
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
网络命名空间
BFC面试简述
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
HWiNFO硬件检测工具v7.26绿色版
【1200. 最小絕對差】
[Shenbo introduction] VI How to contact your favorite doctoral tutor
电脑怎么保存网页到桌面上使用
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
强化学习-学习笔记2 | 价值学习