Springboot 3.x - Reactive programming (2)

news/2024/8/26 15:15:22 标签: java, WebFlux

WebFlux_0">三、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.


http://www.niftyadmin.cn/n/5558216.html

相关文章

前端学习常用技术栈

前端基础:HTML、CSS、JavaScript 前端高级:HTML5、CSS3、JavaScript 语法规范:TypeScript、ECMAScrpit、Eslint、Prettier 前端热门框架:Vue.js、React.js、Angular.js、Bootstrap、Nuxt.js、Svelte.js、Solid.js、Preact.js、Tai…

Docker缩小镜像体积与搭建LNMP架构

镜像加速地址 {"registry-mirrors": ["https://docker.m.daocloud.io","https://docker.1panel.live"] } daemon.json 配置文件里面 bip 配置项中可以配置docker 的网段 {"graph": "/data/docker", #数据目录&#xff0…

认识sm1,sm2,sm3,sm4以及如何在Node.js实现

概述 国密即国家密码局认定的国产密码算法。主要有SM1,SM2,SM3,SM4。密钥长度和分组长度均为128位。 国密算法是指国家密码管理局认定的一系列国产密码算法,包括SM1-SM9以及ZUC等。其中 SM1、SM4、SM5、SM6、SM7、SM8、ZUC等属于…

vue3小程序 中封装组件 但是想在页面中直接获取使用的话可以 通过这样的方式

我们经常会封装许多模那种 loading 或者toast 这种内容,如果在页面上大量写的话 也会感觉代码很乱 所以我们可以简单封装一个 vue2 版本 或者 vue3 版本 1. 创建一个 toast.js 文件 export default function shortToast(title, icon none) {if (typeof title !…

JDK、JRE、JVM

JDK、JVM、JRE? JDK(Java Development Kit) JDK是JRE加上额外的开发工具和资源的集合,它包含了JRE的全部内容。JDK中包括了编译器(如javac,用于将源代码编译成字节码)、调试器、文档生成工具、…

常见的排序算法,复杂度

稳定 / 非稳定排序:两个相等的数 排序前后 相对位置不变。插入排序(希尔排序): 每一趟将一个待排序记录,按其关键字的大小插入到已排好序的一组记录的适当位置上,直到所有待排序记录全部插入为止。稳定&…

持续集成03--Jenkins的安装与配置

前言 在持续集成/持续部署(CI/CD)的实践中,Jenkins作为一个开源的自动化服务器,扮演着至关重要的角色。本篇“持续集成03--Jenkins的安装配置”将带您走进Jenkins的世界,深入了解如何在Linux环境中安装并配置Jenkins。…

elasticsearch中模板的创建和使用

template是es集群内快速生成批量索引的一种快捷方式。 官方解释: 模板是一种在索引创建时配置索引的方法。 7.x之后ES支持两种模板创建方式,一种是传统的或者叫普通的创建方式,即在一次请求中指定 mappings, settings, and aliases等模板的内…