初识 Spring WebFlux 与 Spring Cloud Gateway

对比 Spring MVC 与 Spring WebFlux 的容器、调度模型与编程范式,解析 Spring Cloud Gateway 相对 Zuul 的架构演进。

随着基于 Netty 的非阻塞 NIO 模型以及 Project Reactor 等响应式编程生态的发展,Spring 5 推出了 Spring WebFlux 作为传统 Servlet 模型的补充,Spring Cloud 也随之推出了第二代网关 Spring Cloud Gateway 来替代基于 Servlet 模型的 Zuul。


为什么引入 WebFlux 与 Gateway

Spring WebFlux 的定位与代价

相比 Spring MVC,Spring WebFlux 的优势在于:

  • 非阻塞模型:用更少的工作线程支撑更高的并发连接数;
  • 声明式/函数式模型:基于 Reactor Mono / Flux 编排异步流。

但响应式编程并非“让单次请求变快”,它的收益是高并发与高延迟场景下的线程资源利用率,同时伴随着明显代价:

  • 开发、断点调试与问题排查门槛明显上升;
  • 一旦链路中混入阻塞式 API,吞吐量反而可能低于 Spring MVC。

因此,99% 的常规 CRUD 业务依然更适合简洁易维护的 Spring MVC;而 Gateway 这类天然 I/O 密集的代理层,正是 WebFlux 最能发挥价值的场景。

Spring Cloud Gateway vs Zuul

两者在抽象上都借助 HandlerMapping 接入流量并经由 FilterChain 处理后转发:

  • Zuul 1.x:基于 Servlet + Hystrix 线程池,实测当单接口延迟 10ms 左右时,达到 3000 QPS 后靠堆线程数已很难继续提升吞吐;
  • Gateway:直接运行在 Netty + Project Reactor 之上,并发承载力不再受限于“一请求一线程”,同等资源下吞吐通常明显优于传统 Servlet 网关。

不过 Gateway 定位更接近 Nginx 体系中的 OpenResty(提供核心引擎),距离企业级网关(类似 Kong)仍需自行补齐动态配置、权限与监控中心。


Spring WebFlux 与 Spring MVC 核心对比

Spring MVC 与 WebFlux 架构对比

对比维度Spring MVCSpring WebFlux
底层容器与抽象Servlet 容器(HttpServletRequest / DispatcherServlet)Netty / Servlet 3.1+(ServerWebExchange / DispatcherHandler)
处理模型HandlerMapping → HandlerAdapter → FilterChain保留注解驱动的同时新增函数式路由,基于适配器统一调度
编程范式命令式同步阻塞基于 Project Reactor 的异步非阻塞响应式流

Spring MVC 调度主线(DispatcherServlet#doDispatch)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// org.springframework.web.servlet.DispatcherServlet#doDispatch
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
    noHandlerFound(processedRequest, response);
    return;
}
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    return;
}
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);

Spring WebFlux 调度主线(DispatcherHandler#handle)

1
2
3
4
5
6
7
// org.springframework.web.reactive.DispatcherHandler#handle
return Flux.fromIterable(this.handlerMappings)
        .concatMap(mapping -> mapping.getHandler(exchange))
        .next()
        .switchIfEmpty(createNotFoundError())
        .flatMap(handler -> invokeHandler(exchange, handler))
        .flatMap(result -> handleResult(exchange, result));

可以看到 WebFlux 运行时拆成了两个阶段:先声明式组装整条执行链,再在订阅触发时流式执行。这也是传统断点调试难以同时观察“链路结构”与“运行时数据”的根本原因。


Spring Cloud Gateway Core 结构

从核心包结构即可直观看出 Gateway 与 Spring 生态的集成方式:

1
2
3
4
5
6
7
8
9
org.springframework.cloud.gateway
├── actuate     // Actuator 端点与控制命令
├── config      // Spring Boot 自动装配
├── discovery   // Spring Cloud 服务发现集成
├── event       // 动态路由刷新等事件机制
├── filter      // 统一过滤器链(不再像 Zuul 拆 pre/route/post 三段)
├── handler     // 与 WebFlux DispatcherHandler 衔接
├── route       // 路由断言与匹配规则
└── support     // 辅助工具类

其中转发层的 WebClient 路由分为两条路径:

  • 直连 URL 转发:WebClientHttpRoutingFilter
  • 基于服务发现的转发:LoadBalancerClientFilter 配合 NettyRoutingFilter 直接底层 Netty 转发。
使用 Hugo 构建
主题 Stack 由 Jimmy 设计