Article directory
-
- rely
- Three components
- The first use of the small Demo Gateway
- working principle
- Configure route assertions and filters
- Routing Assertion Rules
-
- 1. Time as a matching routing rule After Before Between
- 2.Cook as a cookie for matching routing rules
- 3. The request header is used as the matching routing rule Header
- 4.Host as the matching routing rule Host
- 5. The request method is used as the matching routing rule Method
- 6. Path as the matching routing rule Path
- 7. Query parameters are used as matching routing rules Query
- 8. Weight as matching routing rule Weight
rely
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
Three components
-
routing
-
Affirmation
-
filter
routing
The basic composition of a gateway, it consists of an ID, a target URL, an assertion and a filter. If the assertion is true, the route will be matched
Affirmation
Only the asserted successful request will match the route
filter
Can handle requests or responses
The first use of the small Demo Gateway
Create a new service module, providing @GetMapping("/say") @GetMapping("/say/one") two request mappings
@RestController
public class HelloController {
@GetMapping("/say")
public String say()
{
return "HelloWord";
}
@GetMapping("/say/one")
public String sayOne()
{
return "HelloWord one";
}
}
Create a new gateway module
Set the following configuration
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Path=/say/**
start two services
We first test whether it can be accessed normally without going through the gateway access service
Then test routing the service through the gateway
So far, a demo of the gateway routing service is completed.
working principle
The following diagram provides a high-level overview of how Spring Gateway works:
The client sends a request to the gateway. If the mapping request processed by the gateway matches the route, the mapping request is handed over to the GateWay Web Handler for processing. The Handler then passes the request to the actual service for execution through the relevant filter chain, and finally returns
The filter can be executed before the proxy request is sent, or it can be executed after the proxy request
Configure route assertions and filters
There are two ways to configure the gateway
1. Shortcut configuration gateway
Shortcut configurations are identified by the filter name followed by an equal sign =, and parameter values separated by commas (,).
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
2. Expand all parameters
Fully expanded parameters look more like standard yaml configuration with name/value pairs. Typically, there will be a name key and an args key. The args key is a map of key-value pairs that configure the assertion or filter
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
Routing Assertion Rules
1. Time as a matching routing rule After Before Between
1.1 The After Route Predicate Factory
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- After=2022-03-20T21:02:47.789-07:00[Asia/Shanghai]
When the requested time is after the assertion time, the route will be matched
The time we configured is 2022-03-20T21:02:47.789 before the current time 2022-03-20 21:07
That is, the current request time can match the route after the time we assert
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- After=2022-03-21T21:02:47.789-07:00[Asia/Shanghai]
The current time is 03-20 and the time we asserted is 03-21, the request cannot match the current route
1.2 The Before Route Predicate Factory
Looking at the example after the time above, you must immediately understand what happened before the time.
The exact opposite of After
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Before=2022-03-20T21:02:47.789-07:00[Asia/Shanghai]
The time of the request is after the time set by the assertion, the current request cannot be matched, and all routes cannot be matched
Set the assertion time to the current time and test it again
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Before=2022-03-22T21:02:47.789-07:00[Asia/Shanghai]
1.3. Comparison between After and Before
1.4 The Between Route Predicate Factory
Specify two times, separated by commas, if the request time is between these two times, the route will be matched
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Between=2022-03-19T21:02:47.789-07:00[Asia/Shanghai],2022-03-22T21:02:47.789-07:00[Asia/Shanghai]
At this time, the request time is
Can be routed normally if the request time is not within the set time range
does not match the route
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Between=2022-03-22T21:02:47.789-07:00[Asia/Shanghai],2022-03-23T21:02:47.789-07:00[Asia/Shanghai]
2.Cook as a cookie for matching routing rules
2.1 The Cookie Route Predicate Factory
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Cookie=token,123
If the name in the request cookie is token, and the value is 123, it will match the current route
If there is a difference between name and value, it cannot be routed successfully
If the name and value are the same, the route can be successfully routed
3. The request header is used as the matching routing rule Header
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Header=token,123
If there is a name token in the request header of the request object and the value is 123, it will match the current route
Use postman tests instead
As shown in the test request header, if there is no request header information with a name of token value of 123, the route cannot be matched.
When there is request header information with name as token and value of 123 in the request header, the current route can be matched
4.Host as the matching routing rule Host
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Host=**.haha.com:81
Modify the local host file first
127.0.0.1 qq.haha.com
127.0.0.1 qq.haha.cn
Visit http://qq.haha.com:81/say to match the route
Change to cn will not match the route
5. The request method is used as the matching routing rule Method
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Method=GET,POST
Both GET and PUT requests can match the route
If we change to PUT request, it can't match the route
6. Path as the matching routing rule Path
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Path=/say
Access /say can match the route
Adding one more level path will not match the route
Can be changed to /say/**
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Path=/say/**
At this point, no matter accessing /say or /say/one, the route can be matched
7. Query parameters are used as matching routing rules Query
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Query=skuID
If only one parameter is written, it means that the query parameter has skuID and matches the current route
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8080 # 匹配后提供服务的路由地址
predicates:
- Query=skuID,11
If two parameters are separated by commas, it means that the query parameter is skuID and the value is 11 to match the current route
Note that both conditions must be met and the request method is consistent with the way the service request is mapped
If the skuID is not 11, it cannot match the current route
8. Weight as matching routing rule Weight
server:
port: 81
spring:
cloud:
gateway:
routes: # 配置路由,是一个集合
- id: apptest1 # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:80 # 匹配后提供服务的路由地址
predicates:
- Path=/say/**
- Weight=group,5
- id: apptest2 # 路由的ID, 没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8081 # 匹配后提供服务的路由地址
predicates:
- Path=/say/**
- Weight=group,5
There are two routing IDs. Both routes are in a weighted group and the weight is 5. Fifty percent of requests matching /say/** are sent to port 80, and the rest are sent to port 8081.
Copy a service and print the port number of the current service
package gateway.controller;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sz
* @DATE 2022/3/20 20:15
*/
@Data
@RestController
public class HelloController {
@Value("${server.port}")
public String serverPort;
@GetMapping("/say")
public String say()
{
return "HelloWord "+serverPort;
}
@GetMapping("/say/one")
public String sayOne()
{
return "HelloWord one";
}
}