4. Spring Cloud Gateway 入门:核心概念与项目搭建
Spring Cloud Gateway 是 Spring 官方基于 Spring WebFlux 构建的 API 网关,旨在为微服务架构提供一种简单、有效且统一的 API 路由管理方式。本章将深入剖析其三大核心概念:Route(路由)、Predicate(断言)、Filter(过滤器),并通过一个完整的 Hello World 项目演示从零搭建到成功路由的全过程。
4.1 三大核心概念
Spring Cloud Gateway 的处理流程可以概括为:客户端请求进入网关,由 Predicate 匹配规则决定是否由某个 Route 处理,在请求前后经过一系列 Filter 进行增强或拦截,最终转发到目标微服务。
核心公式: Route = Route ID + Predicate(匹配条件)+ Filter(处理逻辑)+ URI(目标地址)
| 概念 | 说明 | 类比 |
|---|---|---|
| Route(路由) | 网关的基本构建块,包含 ID、目标 URI、断言集合和过滤器集合。一个请求匹配成功后会按路由配置转发。 | 快递单:收件地址(URI)+ 配送规则(Predicate)+ 增值服务(Filter) |
| Predicate(断言) | Java 8 Predicate 的实现,用于匹配 HTTP 请求的任意属性(路径、Header、参数、时间等)。匹配成功则进入该路由。 | 安检门:只有符合条件(机票+身份证)的人才能通过 |
| Filter(过滤器) | GatewayFilter 实例,可在请求转发前或响应返回后修改请求/响应内容。分为 Pre 和 Post 两种类型。 | 加工流水线:进入前贴标签(Pre),出去前打包(Post) |
💡 关键区别: Predicate 决定“是否处理”,Filter 决定“如何处理”。两者组合使用才能构建完整的路由逻辑。
4.2 路由处理流程(SVG 图)
下图展示了 Spring Cloud Gateway 内部对一个 HTTP 请求的完整处理链路:
4.3 项目搭建与 Hello World
下面通过一个完整的 Spring Boot 项目演示 Spring Cloud Gateway 的搭建与基本路由配置。项目使用 Maven 构建,JDK 17+。
4.3.1 创建 Maven 项目并引入依赖
在 pom.xml 中添加以下核心依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<dependencies>
<!-- Spring Cloud Gateway 核心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
⚠️ 版本兼容性: Spring Cloud Gateway 基于 WebFlux(Reactive),因此 不能 引入
spring-boot-starter-web,否则会导致启动冲突。确保只引入 spring-boot-starter-gateway。
4.3.2 编写启动类
创建一个标准的 Spring Boot 启动类:
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
4.3.3 配置路由(YAML 方式)
在 application.yml 中配置一个简单的路由,将 /hello/** 的请求转发到 http://httpbin.org:
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: hello_route
uri: http://httpbin.org
predicates:
- Path=/hello/**
filters:
- StripPrefix=1
| 配置项 | 说明 |
|---|---|
id |
路由的唯一标识,用于日志和监控 |
uri |
目标服务的地址,支持 http、lb(负载均衡)等协议 |
predicates |
断言规则列表,此处使用 Path 断言匹配请求路径 |
filters |
过滤器列表,StripPrefix=1 表示去除路径中的第一段(即 /hello) |
4.3.4 验证 Hello World
启动项目后,使用 curl 或浏览器访问:
# 请求网关
curl http://localhost:8080/hello/get
# 实际转发到 http://httpbin.org/get
# 返回 httpbin.org 的标准 JSON 响应
如果看到 httpbin.org 返回的 JSON 数据,说明路由配置成功。网关将 /hello/get 的 /hello 前缀剥离后,转发到 http://httpbin.org/get。
4.4 核心概念关系图(SVG 图)
下图展示了 Route、Predicate、Filter 三者之间的组合关系:
4.5 常见问题与避坑指南
- 依赖冲突: 不要同时引入
spring-boot-starter-web和spring-boot-starter-gateway,否则启动报错。 - 路径匹配优先级: 多个路由的 Predicate 匹配时,按配置顺序生效。精确路径应放在通配路径之前。
- Filter 顺序: 同一个路由中多个 Filter 按配置顺序执行。Pre Filter 顺序影响请求处理,Post Filter 顺序影响响应处理。
- 调试技巧: 开启 Gateway 的日志级别可查看路由匹配详情:
logging.level.org.springframework.cloud.gateway=DEBUG
✅ 最佳实践: 建议在开发阶段为每个路由配置
id 时使用有业务含义的名称(如 user_service_route),便于后期维护和问题排查。