2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Spring Cloud Gateway provides an API gateway built on top of the Spring ecosystem, including: Spring 5, Spring Boot 2, and Project Reactor. Spring Cloud Gateway aims to provide a simple and effective way of routing and provide them with some basic gateway features such as: security, monitoring/indicators, and resilience.
Let's take two examples to illustrate:
Spring Boot version: 2.2.5.RELEASE
Spring Cloud version: Hoxton.SR3
Unless otherwise specified, all Spring Cloud examples will use the above version.
Suggestion: Before starting this course, if you do not understand the detailed steps of creating Eureka, it is recommended to read [Learn how to use Spring Cloud's registration center Eureka through an example.It doesn’t matter if you haven’t seen it before, just follow the steps below to get started:
If you don't have a parent project (if you have completed the instance of the registry, you must have a parent project), use simple Maven to create a parent project:
After creation, open the pom.xml file and add the following code:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.cherry</groupId>
- <artifactId>springcloudproject</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>14</java.version>
- <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
- <springboot.version>2.2.5.RELEASE</springboot.version>
- </properties>
- <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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${springboot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
Delete the src folder
Under the parent project, use Spring Initializr to create a new gateway subproject gateway, select the dependency Gateway, and the simplified map is as follows:
After building, modify the pom.xml file. The modified file is as follows:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>springcloudproject</artifactId>
- <groupId>com.cherry</groupId>
- <version>1.0-SNAPSHOT</version>
- <!-- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.6.RELEASE</version>
- <relativePath/> <!– lookup parent from repository –>-->
- </parent>
- <groupId>com.cherry</groupId>
- <artifactId>gateway</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>gateway</name>
- <description>Demo project for Spring Boot</description>
-
- <!--<properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
- </properties>-->
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
-
- <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>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
Modify the suffix of the configuration file application.properties to yml (that is, the file name is changed to application.yml) and configure the gateway
Here we use csdn blog as the service provider
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
Execute run, springboot built-in Tomcat starts, port 9001
Enter the address in the browserhttp://localhost:9001/huanzi833
If you want the gateway to be unavailable, add the following settings to application.yml:
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- enabled: false
-
Browser access, the results are as follows:
Note: After modifying the property file, please restart the application by yourself. I will not repeat it every time later.
The route takes effect after the time set in After, for example: all requests after January 1, 2020 are forwarded to my blog, and those before this time cannot be forwarded
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
Routes before the time set in Before take effect, for example, all requests before January 1, 2021 are forwarded to my blog, and those after this time cannot be forwarded
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
- - Before=Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
The routing between the times set in Between takes effect. For example, all requests between January 1, 2020 and October 1, 2020 are forwarded to my blog. Requests outside this time cannot be forwarded. Usually Between is not used together with After and Before to avoid duplicate settings.
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
For Cookie, predicates can receive two parameters, one is Cookie name, the other is regular expression. The routing rule will match the corresponding Cookie name value and regular expression. If there is a match, the route will be executed, otherwise it will not be executed.
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- enabled: true
-
Using Cookie route matching, we can enter cmd for testing and enter the following statement in cmd:
Similar to Cookie route matching, it also takes two parameters, a parameter name and a regular expression. If a match is found, the route will be executed. If no match is found, the route will not be executed.
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- enabled: true
-
Enter cmd for testing and enter the following statement in cmd:
The following configuration matches by host address, such as www.csdn.net, or www.baidu.com, or blog.csdn.net, etc.
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- enabled: true
-
Enter cmd for testing and enter the following statement in cmd:
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- - Method=GET, POST #Method路由规则
- enabled: true
-
Enter cmd to perform a Get test and enter the following statement in cmd:
Enter cmd to perform a POST test and enter the following statement in cmd: (Note: If the path in the service provider controller uses GetMapping, using -X POST here when calling will result in a 404 error)
Predicate also supports routing requests by setting a certain IP interval, such as 192.168.1.1/24 (where 192.168.1.1 is the IP address and 24 is the subnet mask, where 24 means the subnet mask is 255.255.255.0). You can set this address to the local IP address for testing.
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- - Method=GET, POST #Method路由规则
- - RemoteAddr=192.168.1.1/24
- enabled: true
-
Path routing matching/parameter matching/weight matching will be completed in the integration with the registration center below.
1. First create the service center Eureka, the code is as follows: