1. Prepare
Microservice project
Create a new springBoot project as a microservice. A simple demo is as follows:
@RestController public class MemberController { @RequestMapping("/query-demo") public String queryDemo(String name) { return "https://blog.csdn.net/"+ name + "/article/details/120475609"; } }
- yml
server: port: 8083 spring: application: name: member-server
Gateway gateway service
- Introduce dependency
<!-- gateway rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- springcloud Dependence, dependencies Sibling, non child --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- yml configuration
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: https://blog.csdn.net / # target URI, address of the route to the microservice predicates: #Assertion (judgment condition) - Path=/xuan_lu/** #Match the corresponding URL request and append the matching request after the target URI
The following routing rules change the contents of predicates assertion items;
2,Path
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: https://blog.csdn.net / # target URI, address of the route to the microservice predicates: #Assertion (judgment condition) - Path=/xuan_lu/** #Match the corresponding URL request and append the matching request after the target URI
Request address http://localhost:8082/xuan_lu/article/details/120475609
Jump directly to: https://blog.csdn.net/xuan_lu/article/details/120475609
ID: our customized routing ID, which remains unique
uri: destination service address
Predictions: routing conditions. Predictions accept an input parameter and return a boolean result.
3,Query
- Request parameter assertion:
The configuration is as follows:
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - Query=name #Match the request with name in the request parameter
Indicates: the parameter name in the request address must contain name before it can be passed;
http://localhost:8082/query-demo?name=xl
- Request parameter and request value assertions
The above parameter values may not be obvious. If the routing request is successful, the combination of parameter values will be obvious:
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - Query=name,xuanlu. #Match the request with name in the request parameter
Request route forwarding failed: http://localhost:8082/query-demo?name=xuan
Request route forwarding succeeded: http://localhost:8082/query-demo?name=xuanlu1
4,Method
Route through different request methods such as POST, GET, PUT and DELETE.
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - Method=GET #The matching request type is GET
- Not accessed through the gateway: in order to verify the request method, first change the code request to the @ RequestMapping annotation ID, indicating that both GET requests and POST requests are supported. Verify them with the browser and Postman tools respectively, and directly request the prepared microservice items. The port is 8083;
- Access through the gateway: the microservice project request still uses the @ RequestMapping annotation. The gateway route sets the assertion to the GET request mode. Access using PostMan is as follows: obviously, the route does not pass;
5,DateTime
keyword | format | explain |
---|---|---|
Before | - Before=2021-09-27T17:42:47.000+08:00[Asia/Shanghai] | The request will not be forwarded until a certain time http://localhost:8083 On the server |
After | - After=2021-09-27T17:42:47.000+08:00[Asia/Shanghai] | The request will not be forwarded until after a certain time |
Between | - Between=2021-09-27T17:42:47.000+08:00Asia/Shanghai],2021-09-27T17:42:47.000+08:00[Asia/Shanghai] | In a certain period of time will be forwarded |
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - After=2021-09-27T17:42:47.000+08:00[Asia/Shanghai] #Matching request time: after 17:42 Shanghai time
Verification passed:
On the contrary, because the time for the small series test is 20 o'clock, it is changed to 21 o'clock to test whether the verification is passed: the result is obviously failed;
6,RemoteAddr
Requests can only be routed by setting an IP interval number segment. RemoteAddr Route Predicate accepts a list of cidr symbols (IPv4 or IPv6) strings (the minimum size is 1), such as 192.168.0.1/16 (where 192.168.0.1 is the IP address and 16 is the subnet mask).
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - RemoteAddr=192.168.31.49/0
7,Header
Header Route Predicate: it can receive 2 parameters, including the attribute name and a regular expression in a header. If the attribute value matches the regular expression, it will be executed.
server: port: 8082 spring: application: name: gateway-demo cloud: gateway: # Routing rules routes: - id: csdn-service #Route id, unique uri: http://localhost:8083 / # destination URI, the address of the route to the microservice predicates: #Assertion (judgment condition) - Header=X-Request-Id, \d+