当前位置: 首页 > news >正文

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.

四、Integration with Springboot

Spring WebFlux is the new reactive web framework introduced in Spring Framework 5.0. Unlike Spring MVC, it does not require the servlet API, is fully asynchronous and non-blocking, and implements the Reactive Streams specification through the Reactor project.

Spring WebFlux comes in two flavors: functional and annotation-based. The annotation-based one is quite close to the Spring MVC model, as shown in the following example:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/users")
public class MyRestController {private final UserRepository userRepository;private final CustomerRepository customerRepository;public MyRestController(UserRepository userRepository, CustomerRepository customerRepository) {this.userRepository = userRepository;this.customerRepository = customerRepository;}@GetMapping("/{userId}")public Mono<User> getUser(@PathVariable Long userId) {return this.userRepository.findById(userId);}@GetMapping("/{userId}/customers")public Flux<Customer> getUserCustomers(@PathVariable Long userId) {return this.userRepository.findById(userId).flatMapMany(this.customerRepository::findByUser);}@DeleteMapping("/{userId}")public Mono<Void> deleteUser(@PathVariable Long userId) {return this.userRepository.deleteById(userId);}}

“WebFlux.fn”, the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;/*** <B>Main class name:</B>MyRoutingConfiguration<BR>* <B>Summary description:</B>WebFlux router configuration class<BR>* @author Chris.Gou* @since 2024/07/19 14:53:40*/
@SpringBootConfiguration(proxyBeanMethods = false)
public class MyRoutingConfiguration {/*** RequestPredicate is an interface that defines a request predicate, that is, a condition for testing HTTP requests. <BR/>* Here, ACCEPT_JSON defines a predicate that checks whether the Accept header of the request is application/json.*/private static final RequestPredicate ACCEPT_JSON = accept(MediaType.APPLICATION_JSON);/*** RouterFunction<ServerResponse> is an interface that represents a routing function that routes a request to a handler and returns a ServerResponse.* @param userHandler* @return*/@Beanpublic RouterFunction<ServerResponse> monoRouterFunction(MyUserHandler userHandler) {return route() // route() is a static method that returns a RouterFunctions.Builder for building routing functions..GET("/{user}", ACCEPT_JSON, userHandler::getUser).GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers).DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser).build();}}
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;/*** <B>Main class name:</B>MyUserHandler<BR>* <B>Summary description:</B>MyUserHandler<BR>* @author Chris.Gou* @since 2024/07/19 15:02:47*/
@Component
public class MyUserHandler {public Mono<ServerResponse> getUser(ServerRequest request) {// Business processingreturn null;}public Mono<ServerResponse> getUserCustomers(ServerRequest request) {// Business processingreturn null;}public Mono<ServerResponse> deleteUser(ServerRequest request) {// Business processingreturn null;}
}

These route definitions enable applications to implement specific business logic by routing requests to corresponding handler methods based on different URL patterns and HTTP methods.

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 钡铼Profinet、EtherCAT、Modbus、MQTT、Ethernet/IP、OPC UA分布式IO系统BL20X系列耦合器
  • GOLLIE : ANNOTATION GUIDELINES IMPROVE ZERO-SHOT INFORMATION-EXTRACTION
  • vue基于Cookies实现记住密码自动登录功能
  • Spring Boot外部配置加载顺序
  • Github报错:Kex_exchange_identification: Connection closed by remote host
  • Linux云计算 |【第一阶段】ENGINEER-DAY3
  • centos(或openEuler系统)安装kafka集群
  • 中文科技核心论文发表
  • java多线程等待唤醒机制详细介绍
  • [React]利用Webcomponent封装React组件
  • 【Python将字符串连接在一起】
  • Cadence23导入板框时的疑难杂症
  • SpringBoot3整合Druid报错Cannot load driver class: org.h2.Driver
  • 【芯片设计- RTL 数字逻辑设计入门 番外篇 12 -- SoC 设计中的 ECO】
  • 初识Docker及管理Docker
  • 「译」Node.js Streams 基础
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Android单元测试 - 几个重要问题
  • gitlab-ci配置详解(一)
  • GraphQL学习过程应该是这样的
  • React 快速上手 - 07 前端路由 react-router
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Vue.js-Day01
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 离散点最小(凸)包围边界查找
  • 前端相关框架总和
  • 入口文件开始,分析Vue源码实现
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 原生js练习题---第五课
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​VRRP 虚拟路由冗余协议(华为)
  • # Redis 入门到精通(七)-- redis 删除策略
  • #if 1...#endif
  • #NOIP 2014# day.1 T2 联合权值
  • (02)vite环境变量配置
  • (2024,LoRA,全量微调,低秩,强正则化,缓解遗忘,多样性)LoRA 学习更少,遗忘更少
  • (Java企业 / 公司项目)点赞业务系统设计-批量查询点赞状态(二)
  • (k8s中)docker netty OOM问题记录
  • (LeetCode C++)盛最多水的容器
  • (Ruby)Ubuntu12.04安装Rails环境
  • (zhuan) 一些RL的文献(及笔记)
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (接口封装)
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (三)Kafka 监控之 Streams 监控(Streams Monitoring)和其他
  • (图文详解)小程序AppID申请以及在Hbuilderx中运行
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)Linux下编译安装log4cxx
  • .Mobi域名介绍
  • .NET/C# 使用 ConditionalWeakTable 附加字段(CLR 版本的附加属性,也可用用来当作弱引用字典 WeakDictionary)
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)
  • .Net8 Blazor 尝鲜