Springboot 3.x - Reactive programming (2)

三、WebFlux

Blocking Web vs. Reactive Web

Blocking Web (Servlet) and Reactive Web (WebFlux) have significant differences in several aspects.

1. Front Controller
  • Servlet-Blocking Web: Uses DispatcherServlet as the front controller to handle all HTTP requests.
  • WebFlux-Reactive Web: Uses DispatcherHandler as the front controller to handle all HTTP requests.
2. Handler
  • Servlet-Blocking Web: Uses Controller as the handler.
  • WebFlux-Reactive Web: Uses WebHandler or Controller as the handler.
3. Request and Response
  • Servlet-Blocking Web: Uses ServletRequest and ServletResponse.
  • WebFlux-Reactive Web: Uses ServerWebExchange, along with ServerRequest and ServerResponse.
4. Filters
  • Servlet-Blocking Web: Uses Filter (e.g., HttpFilter).
  • WebFlux-Reactive Web: Uses WebFilter.
5. Exception Handlers
  • Servlet-Blocking Web: Uses HandlerExceptionResolver to handle exceptions.
  • WebFlux-Reactive Web: Uses DispatchExceptionHandler to handle exceptions.
6. Web Configuration
  • Servlet-Blocking Web: Configured via @EnableWebMvc.
  • WebFlux-Reactive Web: Configured via @EnableWebFlux.
7. Custom Configuration
  • Servlet-Blocking Web: Uses WebMvcConfigurer.
  • WebFlux-Reactive Web: Uses WebFluxConfigurer.
8. Return Types
  • Servlet-Blocking Web: The return type can be any object.
  • WebFlux-Reactive Web: The return type can be a Mono, a Flux, or any object.
9. Sending REST Requests
  • Servlet-Blocking Web: Uses RestTemplate to send REST requests.
  • WebFlux-Reactive Web: Uses WebClient to send REST requests.
Core Difference Between Blocking and Reactive Models

Blocking Model (Servlet): Each request is handled by a dedicated thread, which waits for operations to complete (such as database queries or IO operations). This model can lead to thread exhaustion under high concurrency, affecting system performance.

Reactive Model (WebFlux): Uses a non-blocking IO model with a small number of threads handling many requests. It leverages callback mechanisms, event-driven architecture, and asynchronous non-blocking IO for efficient resource utilization and high concurrency handling. Key features of the reactive programming model include:

  • Non-blocking Operations: Operations do not block the current thread, allowing it to continue processing other tasks.
  • Callback Mechanism: Handles subsequent steps through callback mechanisms once an operation completes.
  • Event-driven Architecture: Processes requests based on an event-driven approach.

This model is more efficient in resource usage and is suitable for scenarios requiring high concurrency and large traffic volumes.

Summary

The choice between a blocking or reactive web framework depends on specific application scenarios and requirements. If the application is primarily I/O-intensive with high concurrency needs, then WebFlux is a more suitable choice; if it involves CPU-intensive tasks, the traditional Servlet model might be more appropriate.

相关推荐

  1. Springboot 3.x - Reactive programming (2)

    2024-07-17 06:06:02       26 阅读
  2. jdk8与jdk17的区别。springboot2.xspringboot3.x的区别

    2024-07-17 06:06:02       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 06:06:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 06:06:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 06:06:02       58 阅读
  4. Python语言-面向对象

    2024-07-17 06:06:02       69 阅读

热门阅读

  1. C++基础语法:STL之容器(1)--容器概述和序列概述

    2024-07-17 06:06:02       32 阅读
  2. 【前端】原生实现图片的放大与缩放

    2024-07-17 06:06:02       22 阅读
  3. Meta Llama - Model Cards & Prompt formats

    2024-07-17 06:06:02       22 阅读
  4. 后端开发面试题

    2024-07-17 06:06:02       22 阅读
  5. 自动化回滚的艺术:Conda包依赖的智能管理策略

    2024-07-17 06:06:02       26 阅读
  6. 探索Dubbo的服务引用:XML配置方式

    2024-07-17 06:06:02       26 阅读
  7. 单例模式 饿汉式和懒汉式的区别

    2024-07-17 06:06:02       22 阅读
  8. 【云原生CI/CD工具GitOps】GitOps工作流程和工具链

    2024-07-17 06:06:02       27 阅读