90-Webflux响应式编程怎么去理解?

开发 前端
现在网关都采用spring-cloud-gateway,我们看使用过程中发现编码已经采用响应式编程,直接集成了spring-boot-starter-webflux依赖,这就捎带着把响应式编程带火了一把。
本文转载自微信公众号「Java时间屋」,作者 Jack佳。转载本文请联系一个Java时间屋公众号。
  • 前言
    • 1.什么是响应式编程:
    • 2.使用spring-boot-starter-webflux:
    • 3.Jetty、tomcat、undertow、netty怎么区分:
  • 总结

前言

现在网关都采用spring-cloud-gateway,我们看使用过程中发现编码已经采用响应式编程,直接集成了spring-boot-starter-webflux依赖,这就捎带着把响应式编程带火了一把。本文结合我的理解对响应式编程做一个总结性的介绍,希望能帮助到大家。

1.什么是响应式编程:

提到响应式编程,跟传统的编程区别可能刚开始不太好区分,其中最重要的区别就是传统的是阻塞的,响应式编程是非阻塞异步。官网介绍响应式编程:

  1. In computing, reactive programming is an asynchronous programming paradigm  
  2. concerned with data streams and the propagation of change.  
  3. This means that it becomes possible to express static (e.g. arrays) or  
  4. dynamic (e.g. event emitters) data streams with ease via the employed  
  5. programming language(s), and that an inferred dependency within the  
  6. associated execution model exists, which facilitates the automatic propagation of  
  7. the change involved with data flow. 
  8.  
  9. 在计算机领域,响应式编程是一个专注于数据流和变化传递的异步编程范式。 
  10. 这意味着可以使用编程语言很容易地表示静态(例如数组)或动态(例如事件发射器)数据流, 
  11. 并且在关联的执行模型中,存在着可推断的依赖关系,这个关系的存在有利于自动传播与数据流有关的更改。 

在计算机领域,响应式编程是一个专注于数据流和变化传递的异步编程范式。

这意味着可以使用编程语言很容易地表示静态(例如数组)或动态(例如事件发射器)数据流,

并且在关联的执行模型中,存在着可推断的依赖关系,这个关系的存在有利于自动传播与数据流有关的更改。

可能这段话还是不好理解,但是可以着重看下数据变化,响应式编程就是基于数据变化的新的编程模式,实现异步非阻塞,就是当请求来了之后进行订阅数据的变化,后续业务处理发布变化,然后进行监听到变化,进行响应。而传统的springmvc则是创建新线程等待阻塞,知道请求完毕,释放线程的过程。

2.使用spring-boot-starter-webflux:

比较经典的图示:

从图中我们可以看到基于spring-webmvc和spring-webflux的路线和区别。其中webflux默认是使用netty的通信框架作为web容器,相比较tomcat,netty的优势不再赘述了,并发高、传输快、封装好,其中netty的零拷贝等等。我们在使用webflux的时候注意两个需要经常使用的对象Mono和Flux:

Mono Flux
实现发布者,并返回 0 或 1 个元素,即单对象 实现发布者,并返回 N 个元素,即 List 列表对象

3.Jetty、tomcat、undertow、netty怎么区分:

tomcat:市场占有率仍然非常高,虽然性能上跟其他web服务器比较会有欠缺,但是因为其成熟,实践度很高。undertow和Jetty都是基于NIO实现高并发的轻量级服务器,支持servlet3.1和websocket springboot2以后增加了webflux的web容器,而webflux是基于netty的,netty是nio的,加上其零拷贝的实现,保证其性能上占据优势。

3.1 springboot中使用jetty:

  1. <!-- web剔除tomcat容器= --> 
  2. <parent> 
  3.  <groupId>org.springframework.boot</groupId> 
  4.  <artifactId>spring-boot-starter-parent</artifactId> 
  5.  <version>1.5.10.RELEASE</version> 
  6.  <relativePath/> <!-- lookup parent from repository --> 
  7. </parent> 
  8. <dependency> 
  9.     <groupId>org.springframework.boot</groupId> 
  10.     <artifactId>spring-boot-starter-web</artifactId> 
  11.     <exclusions> 
  12.         <exclusion> 
  13.             <artifactId>spring-boot-starter-tomcat</artifactId> 
  14.             <groupId>org.springframework.boot</groupId> 
  15.         </exclusion> 
  16.     </exclusions> 
  17. </dependency> 
  18. <!-- 引入Jetty容器--> 
  19. <dependency> 
  20.     <groupId>org.springframework.boot</groupId> 
  21.     <artifactId>spring-boot-starter-jetty</artifactId> 
  22. </dependency> 

 

3.2 springboot中使用Webflux/Netty:

  1. <!-- 添加spring-boot-starter-web,默认使用tomcat作为web容器 --> 
  2.         <dependency> 
  3.             <groupId>org.springframework.boot</groupId> 
  4.             <artifactId>spring-boot-starter-web</artifactId> 
  5.             <exclusions> 
  6.                 <exclusion> 
  7.                     <groupId>org.springframework.boot</groupId> 
  8.                     <artifactId>spring-boot-starter-logging</artifactId> 
  9.                 </exclusion> 
  10.                 <exclusion> 
  11.                     <groupId>org.springframework.boot</groupId> 
  12.                     <artifactId>spring-boot-starter-tomcat</artifactId> 
  13.                 </exclusion> 
  14.             </exclusions> 
  15.         </dependency> 
  16.         <!-- 去除tomcat,将undertow作为容器 --> 
  17.          <dependency> 
  18.             <groupId>org.springframework.boot</groupId> 
  19.             <artifactId>spring-boot-starter-undertow</artifactId> 
  20.         </dependency> 

 

总结

 

其实Spring提供的webflux框架简化了我们操作Netty使用的复杂性,提供了Reactor Netty库,因为网关性能的要求,所有spring-cloud-gateway直接集成了webflux,使用Netty的nio的特性极大的满足了网关高并发,高性能要求的场景,个人觉得不见得响应式编程未来会遍地开发,但是网关这种特殊的场景确实比较适合响应式编程的应用。

 

责任编辑:武晓燕 来源: Java时间屋
相关推荐

2022-09-22 08:19:26

WebFlux函数式编程

2022-03-09 23:02:30

Java编程处理模型

2023-11-27 07:42:27

Reactor响应式

2022-09-26 08:54:39

Spring函数式编程

2020-08-31 07:19:57

MonoFlux Reactor

2019-07-01 13:34:22

vue系统数据

2021-07-14 13:12:51

2022-06-16 13:08:30

Combine响应式编程订阅

2022-07-15 08:16:56

Stream函数式编程

2023-01-28 08:04:08

AOPSpring框架

2024-03-06 07:52:21

Spring框架响应式编程微服务架构

2016-11-03 13:19:38

vue.jsjavascript前端

2022-08-25 11:00:19

编程系统

2022-10-25 08:05:12

Kotlin响应式编程

2023-07-12 08:16:54

JVM工具包Vert.x

2022-03-29 07:32:38

R2DBC数据库反应式

2022-09-01 08:00:00

响应式编程集成

2020-10-27 10:26:03

编程开发Java

2022-04-13 09:34:52

软件开发嵌入式软件

2013-02-21 09:54:12

响应式重构Web
点赞
收藏

51CTO技术栈公众号