Spring Cloud Gateway 与 Netflix Zuul 2:2025 年 API 网关选型指南

API 网关在微服务架构中至关重要,负责路由、负载均衡、安全防护和可观测性。Java 生态中有两大主流方案:

但 2025 年哪种更胜一筹?本文将从性能、功能特性和实际适用性进行对比,助你决策。

1、性能对比

基准测试结果(延迟与吞吐量)

指标 Spring Cloud Gateway Netflix Zuul 2
平均延迟(毫秒) 12 25
最大吞吐量(请求数/秒) 15,000 8,000
CPU 使用率 更低(响应式技术栈) 更高(基于 Servlet)

🔹 为何 Spring Cloud Gateway 胜出?

  • 基于 Project Reactor(非阻塞式)。
  • 无 Servlet 容器开销(与 Zuul 2 不同)。
  • 专为 云原生负载 优化。

🔹 何时 Zuul 2 更合适?

  • 若已深度集成 Netflix OSS 生态。
  • 需更精细的过滤器(Zuul 2 内置过滤器更丰富)。

2、功能特性对比

功能特性 Spring Cloud Gateway Netflix Zuul 2
协议支持 HTTP/2、WebSockets HTTP/1.1
负载均衡 客户端(Ribbon) 动态服务端
安全性 OAuth2、JWT、CORS Basic Auth、Header
可观测性 Micrometer、Prometheus Spectator(功能有限)
配置便捷性 YAML、Java DSL Groovy 脚本

🔹 核心结论:

  • Spring Cloud Gateway 更现代化(支持 WebFlux、HTTP/2)。
  • Zuul 2 在自定义路由逻辑上更灵活
  • Spring Cloud Gateway 与 Spring Boot Actuator 监控集成更佳

3、代码示例

Spring Cloud Gateway(YAML 配置)

spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/api/users/**
          filters:
            - RewritePath=/api/users/(?<segment>.*), /$\{segment}

Zuul 2(Groovy Filter 示例)

class CustomZuulFilter extends HttpInboundSyncFilter {
    @Override
    HttpRequestMessage apply(HttpRequestMessage request) {
        if (request.path.startsWith("/api/")) {
            request.addHeader("X-Auth", "valid-user")
        }
        return request
    }
}

🔹 结论:

  • Spring Cloud Gateway 的声明式路由更简洁。
  • Zuul 2 支持更深度的定制但需更多代码。

4、当前推荐方案(2025 年)

✅ 选择 Spring Cloud Gateway 若:

  • ✔ 你使用 Spring Boot/Cloud
  • ✔ 需要 高吞吐与低延迟
  • ✔ 期望 原生 Kubernetes 支持

选择 Netflix Zuul 2 若:

  • ✔ 你处于 重度依赖 Netflix OSS 的环境。
  • ✔ 需要 复杂的动态请求处理
  • ✔ 不介意 更高的资源消耗

避免使用 Zuul 2 若:

  • 你需要 HTTP/2 或 WebSocket 支持
  • 你希望 便捷的 Prometheus 指标集成

5、迁移建议(Zuul 1/2 转 Spring Cloud Gateway)

若从 Zuul 迁移:

  1. Groovy 过滤器 替换为 WebFlux GatewayFilter
  2. 使用 Spring Cloud LoadBalancer 替代 Ribbon
  3. 指标监控 迁移至 Micrometer

示例如下:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
            .route("product-service", r -> r.path("/api/products/**")
                    .filters(f -> f.rewritePath("/api/(?<segment>.*)", "/${segment}"))
                    .uri("lb://product-service"))
            .build();
}

6、最终结论

类别 优胜方案
性能 🏆 Spring Cloud Gateway
功能 🏆 Spring Cloud Gateway
灵活性 🏆 Zuul 2
未来适应性 🏆 Spring Cloud Gateway

7、Spring Cloud Gateway 与 Netflix Zuul 2:详细基准测试配置

本节提供 Spring Cloud Gateway(SCG)与 Netflix Zuul 2 性能基准测试的完整配置指南,确保结果可靠且可复现。

测试环境配置

硬件/云环境配置

  • 机器配置: AWS EC2 c5.2xlarge(8 vCPU,16GB 内存)或同等规格
  • 操作系统: Ubuntu 22.04 LTS
  • Java 环境: OpenJDK 17(LTS)搭配 G1 垃圾回收器
  • 网络: 专用 VPC 确保服务间最低延迟

软件版本

软件 版本
Spring Cloud Gateway 4.0.0+ (Spring Boot 3.x)
Netflix Zuul 2 2.1.6
Gatling(负载测试) 3.9.0
Prometheus + Grafana 最新版本

基准测试架构

[Gatling Load Generator] → [API Gateway] → [Backend Service (Mock)]
                      [Prometheus Metrics]

后端服务: 包含 3 个端点的简易 Spring Boot 应用:

  • /api/hello(无操作响应)
  • /api/compute(CPU 密集型任务)
  • /api/db(模拟数据库调用)

网关配置:

  • 两网关配置完全相同的路由规则。
  • 禁用熔断器(Circuit Breaker)以确保纯粹性能对比。

测试场景

A:吞吐量测试(最大RPS)

目标: 测量失败前的最大请求数/秒。

方法:

// Gatling 模拟配置
setUp(
  scenario("Max Throughput")
    .exec(http("gateway_call")
      .get("/api/hello"))
    .inject(
      rampUsersPerSec(100) to (5000) during (2 minutes)
)

B:延迟测试(负载下的 P99 指标)

目标:在 50% 最大 RPS 负载下测量第 99 百分位延迟。

方法

// Gatling 模拟配置
setUp(
  scenario("Latency Test")
    .exec(http("gateway_call")
      .get("/api/db"))
    .inject(
      constantUsersPerSec(2500) during (5 minutes)
)

C:资源利用率

采集指标:

  • CPU 占用率(通过 htop)
  • 内存(常驻集大小)
  • GC 停顿
  • 线程数
配置详情

Spring Cloud Gateway:

# application.yml
spring:
  cloud:
    gateway:
      httpclient:
        pool:
          max-connections: 1000
          acquire-timeout: 2000
      metrics:
        enabled: true

Netflix Zuul 2:

// zuul.properties
zuul.server.netty.threads.worker=16
zuul.server.netty.socket.backlog=1024
zuul.server.netty.socket.runtime.optimizations=true

JVM 参数(通用配置):

-XX:+UseG1GC 
-XX:MaxRAMPercentage=75 
-XX:+AlwaysPreTouch
-Xlog:gc*
执行步骤

启动后端服务:

java -jar backend-service.jar --server.port=8081

启动 Gateway(SCG):

java -jar spring-gateway.jar --server.port=8080

启动 Gateway(Zuul 2)

java -jar zuul2.jar --zuul.server.port=8080

运行 Gatling 测试:

./gatling.sh -s gateway.PerfTest

指标采集:

# Prometheus 采集配置
- job_name: 'gateway'
  metrics_path: '/actuator/prometheus'
  static_configs:
    - targets: ['localhost:8080']
预期结果

吞吐量(请求数/秒):

并发用户数 SCG RPS Zuul 2 RPS
500 8,200 5,100
1,000 15,000 8,000
2,000 14,500 7,200

2,000 RPS 下的延迟(毫秒):

百分位 SCG Zuul 2
50th 12 25
95th 28 65
99th 45 120

资源使用情况:

指标 SCG(15K RPS 时) Zuul 2(8K RPS 时)
CPU Usage 45% 75%
Memory 1.2 GB 2.1 GB
Threads 32 48

结果分析

为何 SCG 表现更优?

  • 响应式技术栈: 非每个请求一个线程模型(thread-per-request)。
  • Netty 优化: 更佳的 HTTP/2 支持。
  • 更低开销: 无 Servlet API 限制。

观测到的 Zuul 2 瓶颈:

  • 线程池导致更高的上下文切换开销。
  • 大对象分配引发 GC 压力。
  • HTTP/1.1 解析额外开销。

结果复现建议

✅ 确保结果准确性:

  • 每次测试运行 3 次以上(首次运行作为预热丢弃)。
  • 使用独立网络(无共享带宽)。
  • 禁用测试机的节能模式。
  • 监控 TCP 重传(netstat -s)。

⚠️ 常见误区:

  • 未预热 JVM(应使用 -XX:+AlwaysPreTouch)。
  • 测试实例规格不足。
  • 后台进程占用 CPU 资源。

高阶:Kubernetes 基准测试

云原生测试:

# Kubernetes 中的 k6 负载测试任务
apiVersion: batch/v1
kind: Job
metadata:
  name: gateway-benchmark
spec:
  template:
    spec:
      containers:
      - name: k6
        image: loadimpact/k6
        command: ["k6", "run", "/scripts/test.js"]
        volumeMounts:
        - name: test-scripts
          mountPath: /scripts
      restartPolicy: Never

关键的 Kubernetes 监控指标:

  • container_cpu_usage_seconds_total
  • container_memory_working_set_bytes
  • http_request_duration_seconds_bucket

此基准测试表明:

  1. Spring Cloud Gateway 吞吐量高 1.8-2 倍
  2. Zuul 2 在同等负载下 CPU 消耗多 2 倍
  3. SCG 的延迟分布更集中

对于注重性能的生产系统,SCG 显然更优。但若需 Zuul 2 独有的过滤能力,它仍有存在价值。

8、总结

Spring Cloud Gateway 是 2025 年大多数场景的更优选择——更快、更现代且与 Spring 生态集成更佳。Zuul 2 仅适用于遗留 Netflix OSS 系统。

延伸阅读:


Ref:https://www.javacodegeeks.com/2025/06/spring-cloud-gateway-vs-netflix-zuul-2-which-api-gateway-should-you-use-in-2025.html#google_vignette