当前位置:网站首页>[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 .
边栏推荐
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- 看腾讯大老如何做接口自动化测试
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- The concept and application of hash table
- 奏响青春的乐章
- Golang中UTF编码和字符集
- 【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
- 五子棋 上班摸鱼工具 可局域网/人机
- word中插入图片后,图片上方有一空行,且删除后布局变乱
- 【解决方案】PaddlePaddle 2.x调用静态图模式
猜你喜欢
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Idea restore default shortcut key
How does wincc7.5 SP1 find variables and their positions through cross indexing?
How does the computer save web pages to the desktop for use
网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
LeetCode+ 81 - 85 单调栈专题
Sword finger offer II 80-100 (continuous update)
多模輸入事件分發機制詳解
How does win11 search for wireless displays? Win11 method of finding wireless display device
随机推荐
Hands on deep learning (III) -- convolutional neural network CNN
Redis分布式锁的实现
MySQL - database query - use of aggregate function, aggregate query, grouping query
Alibaba testers use UI automated testing to achieve element positioning
哈希表、哈希函数、布隆过滤器、一致性哈希
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
See how Tencent does interface automation testing
ACM组合计数入门
How to solve the problem that win11 cannot write the value to the registry key?
嵌入式TC 测试用例
idea恢复默认快捷键
Win11无法将值写入注册表项如何解决?
Automatic insertion of captions in word
go语言笔记(2)go一些简单运用
How does the computer save web pages to the desktop for use
卷积神经网络在深度学习中新发展的5篇论文推荐
字节测试工程师十年经验直击UI 自动化测试痛点
The concept and application of hash table
伦敦银走势图分析的新方法
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”