Zuul开源的 API 网关,曾是Spring Cloud早期的默认网关

Zuul 是 Netflix 开源的 API 网关,曾是 Spring Cloud 早期的默认网关实现。

核心定位

Zuul作为微服务架构的前置入口,承担以下职责:

客户端请求 → Zuul 网关 → 路由转发 → 后端服务
              │
              ├── 鉴权认证
              ├── 限流熔断
              ├── 日志记录
              ├── 请求过滤
              └── 跨域处理

核心概念

1. 过滤器(Filter)

Zuul 的核心是请求生命周期中的过滤链,四种类型:

类型 执行时机 用途
PRE 请求转发前 鉴权、日志、参数校验
ROUTING 请求转发时 HTTP 请求构建、超时控制
POST 响应返回前 结果修改、添加头部
ERROR 出错时 统一错误处理

2. 路由(Route)

Zuul 根据规则将请求转发到后端服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
zuul:
routes:
# 简单路由
user-service: /user/**
order-service: /order/**

# 带前缀的路由
api-gateway:
path: /api/**
serviceId: user-service
stripPrefix: false # 保留 /api 前缀

3. 服务发现集成

1
2
3
4
5
# 与服务注册中心(Eureka/Nacos)集成
zuul:
service-id-to-url-path:
user-service: /user/**
order-service: /order/**

完整请求流程

客户端请求
    │
    ▼
PreFilters(预处理)
    ├── 鉴权
    ├── 日志
    ├── 限流
    └── 参数校验
    │
    ▼
RoutingFilter(路由转发)
    ├── Ribbon 负载均衡
    ├── Hystrix 熔断(Zuul 1.x 默认集成)
    ├── HTTP Client 发起请求
    └── 超时控制
    │
    ▼
PostFilters(后处理)
    ├── 响应修改
    ├── 添加 Header
    └── 聚合多服务响应
    │
    ▼
ErrorFilters(错误处理)
    └── 统一异常返回

Spring Boot 集成

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableZuulProxy // 启用代理模式(支持服务发现)
// @EnableZuulServer // 仅路由,不支持服务发现
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}

常用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
server:
port: 8080

spring:
application:
name: zuul-gateway

zuul:
prefix: /api # 全局前缀,所有路由自动加上
routes:
user-service: /user/**
order-service: /order/**

# 忽略的服务
ignored-services:
- '*' # 忽略所有服务发现

# 重试配置
retryable: true

# 超时配置
host:
connect-timeout-millis: 5000
socket-timeout-millis: 60000

# 敏感头忽略
sensitive-headers:
- Cookie
- Set-Cookie
- Authorization

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

Zuul 1.x vs Zuul 2.x

特性 Zuul 1.x Zuul 2.x
线程模型 Servlet + 阻塞 I/O Netty + 非阻塞 I/O
性能 一般(线程消耗大) 高(异步非阻塞)
连接数 有限 支持大量并发
成熟度 广泛使用 不够成熟
社区状态 维护模式 已基本停滞

Zuul 1.x 是主流,大家说的 “Zuul” 通常指 1.x。

优缺点

优点:

  • 与 Eureka 天然集成,支持服务发现
  • 过滤器链机制灵活,易于扩展
  • Ribbon 集成实现负载均衡
  • Hystrix 集成实现熔断
  • 上手简单,配置驱动

缺点:

  • 性能瓶颈:Zuul 1.x 基于 Servlet 阻塞模型,吞吐量低
  • 已停更:Netflix 宣布 Zuul 进入维护模式
  • 功能局限:缺少动态路由、灰度发布等高级功能
  • 调试困难:过滤器链复杂时排查问题麻烦

Zuul 的替代方案

网关 特点 状态
Spring Cloud Gateway 基于 Netty 非阻塞,Reactors 响应式 Spring 官方推荐
Kong 基于 Nginx + Lua,插件化 独立网关,语言无关
APISIX 动态路由、热插拔插件 国产,活跃
Sentinel Gateway 流量治理 + 网关 阿里系

Spring Cloud Gateway 对比

Zuul 1.x              Spring Cloud Gateway
─────────────          ───────────────────
Servlet 阻塞模型        Netty 异步非阻塞
性能一般                高性能,吞吐量大
过滤器:ZuulFilter      过滤器:GatewayFilter
单线程模型              响应式编程
已停更                  Spring 官方维护

总结

Zuul 1.x 是早期的 API 网关方案,已不再推荐用于新项目。Spring Cloud 官方已转向 Spring Cloud Gateway