一. Spring Cloud Gateway 簡介
Spring Cloud Gateway 是在 Spring MVC 基礎之上用於構建 API 網關的項目,其目標是提供一個簡單、有效的方式實現 API 的路由和一些其他跨領域的功能,比如安全、監控/指標、彈性服務等。關於網關設計的要點,可以參考耗子叔專欄中的文章管理設計篇之“網關模式”。
1. 主要概念
- Route(路由):每條 Route 代表一條 API 的路由規則,主要由四部分組成:路由 ID,目的地址,一組匹配器(Predicate)和一組過濾器(Filter)。
- Predicate(匹配器):可以基於時間、權重、請求信息等進行請求匹配。一條路由的所有 Predicate 匹配都通過時該條路由纔算匹配成功。
- Filter(過濾器):用於對請求和響應做響應的處理。
2. 工作流程
下面兩幅圖展示了 Spring Cloud Gateway 的工作流程:
可以看到,當一個請求過來時主要有下面幾步:
- 接收請求
- 遍歷路由進行匹配器匹配,匹配成功則開始執行 filter
- PreFilter 處理請求
- 轉發請求到 Service(即匹配成功的 Route 所配置的 uri)
- PostFilter 處理響應
- 返回響應
二. Hello World 示例
上面是基本的概念介紹,下面我們創建一個簡單的示例項目。
假設我們有一個教務系統,有教師服務 teacher-service和學生服務 student-service 兩個後臺服務,現在我們需要用 Spring Cloud Gateway 作爲網關進行請求代理和轉發。
1. 創建後臺服務
教師後臺服務
- Controller
@SpringBootApplication
public class TeacherServiceApplication {
public static void main(String[] args) {
SpringApplication.run(TeacherServiceApplication.class, args);
}
}
@RestController
public class TeacherController {
@GetMapping("/teacher/message")
public String message() {
return "This is Teacher Service";
}
}
- application.yml 配置文件
server:
port: 8081
- pom 文件
<?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>
<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.school.teacher</groupId>
<artifactId>teacher-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>teacher-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
學生後臺服務
- Controller
@SpringBootApplication
public class StudentServiceApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServiceApplication.class, args);
}
}
@RestController
public class StudentService {
@GetMapping("/student/message")
public String message() {
return "This is Student Service";
}
}
- application.yml 配置文件
server:
port: 8082
- pom 文件
<?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>
<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.school.student</groupId>
<artifactId>student-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
創建完成並啓動服務後,可以分別訪問對應服務的 Controller 進行測試,都整對的話訪問情況如下:
2. 創建網關服務
後端服務創建好之後,再來創建網關服務並配置路由其
- Java 代碼
@SpringBootApplication
public class SchoolGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolGatewayApplication.class, args);
}
}
- pom 文件
<?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>
<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.school.gateway</groupId>
<artifactId>school-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>school-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>
- 路由配置
Spring Cloud Gateway 的路由可以在代碼、配置文件中進行配置。代碼如下:
- application.yml 文件配置
server:
port: 8080
spring:
cloud:
gateway:
routes:
# 按路徑匹配
# 以 /teacher 開頭的請求將會被路由到 http://localhost:8081
- id: teacher-route
uri: http://localhost:8081
predicates:
- Path=/teacher/**
# 以 /student 開頭的請求將會被路由到 http://localhost:8082
- id: student-route
uri: http://localhost:8082
predicates:
- Path=/student/**
Java 代碼配置
@Configuration
public class RouteConfig {
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/teacher/**")
.uri("http://localhost:8081/")
.id("teacher-route"))
.route(r -> r.path("/student/**")
.uri("http://localhost:8082/")
.id("student-route"))
.build();
}
}
配置完成後啓動網關服務,然後通過網關訪問後端服務,訪問情況如下:
可以看到通過網關可以正常訪問後端服務了,網關會根據請求路由將請求路由到對應的後端服務。有了直觀的使用之後,就可以對 Spring Cloud Gateway 做更詳細的探索了。下一篇主要介紹下 Predicate 匹配器的主要匹配規則使用。