Welcome to my GitHub
https://github.com/zq2599/blog_demos
Content: Classification and summary of all original articles and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Overview of this article
- This article is the fourth in the "Spring Cloud Gateway Actual Combat" series. Let's make a summary of the types of predicates. In today's content, in addition to the simplified version of the official recommended configuration, the dynamic routing is also given. JSON format configuration of assertion;
After
- <font color="blue">After</font> means that the route takes effect after the specified time
- Configuration file, pay attention to the format of the time string, <font color="blue">+08:00</font> means Dongba District:
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]
- The JSON format of dynamic routing. Note that the parameter of <font color="blue">args</font> should use <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> means that the route takes effect before the specified time
- Configuration file:
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]
- The JSON format of dynamic routing. Note that the parameter of <font color="blue">args</font> should use <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> means that the routing takes effect within the specified time period. Since the time period is the two parameters, pay attention to their writing
- Configuration file:
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]
- The JSON format of dynamic routing, pay attention to the <font color="blue">args</font> parameter, the start time is <font color="red">datetime1</font>, and the end time is <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> means that the cookie has the specified name and the corresponding value meets the specified regular expression, then the match is considered successful
Configuration file:
spring: cloud: gateway: routes: - id: cookie_route uri: https://example.org predicates: - Cookie=chocolate, ch.p
- The JSON format of dynamic routing, pay attention to the <font color="blue">args</font> parameter:
[
{
"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> means that the header has the specified name and the corresponding value meets the specified regular expression, the match is considered successful
- The following example requires that <font color="blue">X-Request-Id</font> must exist in the header, and the value must be a number
- Configuration file:
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
- The JSON format of dynamic routing, note that the <font color="blue">args</font> parameters are <font color="red">header</font> and <font color="red">regexp</font>, Also note that there are two backslashes in the value of regexp (escape problem):
[
{
"id": "header_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Header",
"args": {
"header": "X-Request-Id",
"regexp": "\\d+"
}
}
]
}
]
- Fill in the parameters tested with postman and the results are as follows:
Host
- <font color="blue">Host</font> indicates that the requested host must match the specified string, and the corresponding value meets the specified regular expression, then the match is considered successful. You can specify multiple host matching expressions at the same time. The following example gives two, the first of which specifies the port:
- Configuration file:
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://127.0.0.1:8082
predicates:
- Host=test.com:8081,**.anotherhost.org
- The JSON format of dynamic routing. Pay attention to the <font color="blue">args</font> parameter. In addition, through actual measurement, it is found that the value of regex here is a regular expression. Therefore, multiple hosts in the above configuration file are required here Realized by the writing of regular expressions (the writing of json array, there is always an exception during deserialization, and it cannot be parsed successfully):
[
{
"id": "header_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Host",
"args": {
"regex": "test.com:8086"
}
}
]
}
]
Method
- <font color="blue">Method</font> is very easy to understand, matching the specified method type (there can be multiple)
- Configuration file:
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://127.0.0.1:8082
predicates:
- Method=GET,POST
- The JSON format of dynamic routing, also, due to personal level issues, temporarily only practice the JSON writing of specifying a single method. If you know how to specify a method, I hope to inform, thank you:
[
{
"id": "method_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Method",
"args": {
"methods": "GET"
}
}
]
}
]
Path
- <font color="blue">Path</font> is very commonly used, matching the specified method type (there can be multiple)
- Configuration file, pay attention to <font color="blue">{segment}</font>, which means that the real value of the position can be extracted and used in the filter, which will be explained in the subsequent filter article:
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/{segment},/lbtest/{segment}
- The JSON format of dynamic routing, also, due to personal level issues, temporarily only practice the JSON writing of specifying a single method. If you know how to specify a method, I hope to inform, thank you:
[
{
"id": "path_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/hello/{segment}"
}
}
]
}
]
Query
- <font color="blue">Query</font> matches whether the request contains a specified parameter, and it can also require that the parameter is equal to the specified value (regular expression) to be matched
- The configuration file, as long as the request parameter named <font color="blue">name</font> is matched:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://127.0.0.1:8082
predicates:
- Query=name
- As shown below, you can also specify that the value of the name parameter must be <font color="blue">aaa.</font>, this decimal point means matching a character, for example, <font color="blue">name=aaa1</font> Or <font color="blue">name=aaa2</font> can be:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://127.0.0.1:8082
predicates:
- Query=name,aaa.
- The JSON format of dynamic routing. Note that the parameter name and parameter value are set with <font color="blue">param</font> and <font color="blue">regexp</font> respectively:
[
{
"id": "query_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Query",
"args": {
"param": "name",
"regexp": "aaa."
}
}
]
}
]
- The test is as follows:
RemoteAddr
- <font color="blue">RemoteAddr</font> is well understood, it matches the request from the specified source
- Configuration file:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://127.0.0.1:8082
predicates:
- RemoteAddr=192.168.50.134/24
- The JSON format of dynamic routing. Note that the parameter name and parameter value are set with <font color="blue">param</font> and <font color="blue">regexp</font> respectively:
[
{
"id": "remoteaddr_route",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "RemoteAddr",
"args": {
"sources": "192.168.50.134/24"
}
}
]
}
]
- The test is as follows. Please do not use <font color="blue">localhost</font> and <font color="blue">127.0.0.1</font> as the host address when testing. This will cause the server to determine the source. The obtained network card address is 0.0.0.0:
Weight
- <font color="blue">Weight</font> As the name implies, requests are distributed to different locations according to weight
- Configuration file:
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
- The above are the commonly used assertion types. It can be seen that the function is already very powerful. I hope to give you some reference
You are not alone, Xinchen and original are with you all the way
- Java series
- Spring series
- Docker series
- kubernetes series
- database + middleware series
- DevOps series
Welcome to pay attention to the public account: programmer Xin Chen
Search "Programmer Xin Chen" on WeChat, I am Xin Chen, and I look forward to traveling the Java world with you...
https://github.com/zq2599/blog_demos
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。