The difference between spring-cloud-starter-gateway-server-webflux
and spring-cloud-starter-gateway-server-webmvc
lies in their underlying technology stack, concurrency model, and official support in Spring Cloud Gateway. Here's a breakdown:
1. spring-cloud-starter-gateway-server-webflux
(Reactive/WebFlux)
Technology Stack:
Built on Spring WebFlux (reactive stack) and Project Reactor.Concurrency Model:
Uses non-blocking I/O (event-loop) for high scalability and efficiency with concurrent requests.Dependencies:
Includesspring-boot-starter-webflux
(reactive Netty, Undertow, etc.).Performance:
Optimized for high-throughput, low-latency scenarios (e.g., microservices, streaming).Official Support:
This is the standard/officially supported starter for Spring Cloud Gateway.Use Case:
Reactive applications, cloud-native microservices requiring scalability.
2. spring-cloud-starter-gateway-server-webmvc
(Blocking/WebMVC)
Technology Stack:
Built on Spring WebMVC (servlet stack) with blocking I/O.Concurrency Model:
Uses thread-per-request (blocking) model, limiting scalability under heavy load.Dependencies:
Includesspring-boot-starter-web
(Tomcat, Jetty, etc.).Performance:
May bottleneck under high concurrency due to thread overhead.Official Support:
Not officially supported or maintained by Spring Cloud.
Likely a community experiment or deprecated prototype.Use Case:
Only for legacy servlet applications (not recommended for new projects).
Key Technical Differences:
Aspect | webflux (Reactive) | webmvc (Blocking) |
---|---|---|
HTTP Server | Netty, Reactor-based servers | Tomcat, Jetty (Servlet containers) |
Request Handling | Asynchronous (event-loop) | Synchronous (thread-per-request) |
Scalability | High (efficient resource usage) | Limited (thread exhaustion risk) |
Backpressure Support | Yes (via Project Reactor) | No |
Reactive Streams | Full support (Mono/Flux) | Not applicable |
Why Spring Officially Recommends WebFlux:
Gateway Design:
Spring Cloud Gateway relies on reactive APIs (e.g.,ServerWebExchange
,Reactor
).Compatibility:
Integrates seamlessly with reactive Spring projects (Spring Data R2DBC, RSocket).Performance:
Non-blocking I/O handles 10K+ concurrent connections with minimal threads.Future-Proof:
Aligns with cloud-native/async trends (gRPC, WebSocket, streaming).
Should You Use webmvc
?
Avoid it.
Thewebmvc
variant is unsupported, may have bugs, and negates the core benefits of Spring Cloud Gateway (scalability, non-blocking).If you need a blocking API gateway, consider alternatives:
Spring Cloud Netflix Zuul (deprecated but servlet-based).
Traditional reverse proxies (Nginx, HAProxy).
Correct Starter for New Projects:
Use the standard (and only officially maintained) dependency:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <!-- Implicitly uses WebFlux --> </dependency>
This automatically pulls spring-cloud-starter-gateway-server-webflux
under the hood.
Summary:
webflux
: Production-ready, reactive, high-performance.webmvc
: Experimental/legacy, blocking, unsupported.
Always choosespring-cloud-starter-gateway
(WebFlux) for new work.
No comments:
Post a Comment