摘要:概念響應(yīng)式編程,異步非阻塞就是響應(yīng)式編程,與之相對(duì)應(yīng)的是命令式編程。的另外一種實(shí)現(xiàn)方式就是消息隊(duì)列。非阻塞設(shè)計(jì)利用規(guī)范中的實(shí)現(xiàn)實(shí)現(xiàn)代碼鏈接
注: 本文是由讀者觀看小馬哥公開課視頻過程中的筆記整理而成。0. 編程模型與并發(fā)模型
更多Spring Framework文章可參看筆者個(gè)人github: spring-framework-lesson 。
Spring 5實(shí)現(xiàn)了一部分Reactive
Spring WebFlux: Reactive Web(non-blocking servers in general)
Spring Web MVC:傳統(tǒng)Servlet Web(servlet applications in general)
0.1 編程模型編程模型:阻塞、非阻塞
NIO:同步+非阻塞,基于事件
非阻塞
基本上采用Callback方式
當(dāng)時(shí)不阻塞,后續(xù)再輸出(再回調(diào))
0.2 并發(fā)模型
并發(fā)模型:
同步(Sync)
異步(Async)
0.3 比較同步+非阻塞:線程不會(huì)改變,不會(huì)切換
[線程:main] Observable 添加觀察者! [線程:main] 通知所有觀察者! [線程:main] 3. 收到數(shù)據(jù)更新:Hello World [線程:main] 2. 收到數(shù)據(jù)更新:Hello World [線程:main] 1. 收到數(shù)據(jù)更新:Hello World
異步+非阻塞:線程會(huì)被切換
[線程:main] 啟動(dòng)一個(gè)JFrame窗口! [線程:AWT-EventQueue-0] 銷毀當(dāng)前窗口! [線程:AWT-EventQueue-0] 窗口被關(guān)閉,退出程序!
使用Jconsole查看改異步非阻塞程序
等待總數(shù)一直在增加,說明異步程序一直在等待。NIO就是無限地在處理,無限地在等待。
1. Reactive概念Reactive Programming:響應(yīng)式編程,異步非阻塞就是響應(yīng)式編程(Reactive Programming),與之相對(duì)應(yīng)的是命令式編程。
Reactive并不是一種新的技術(shù),不用Reactive照樣可以實(shí)現(xiàn)非阻塞(同步、異步均可,推拉模式的結(jié)合),比如利用觀察者模式實(shí)現(xiàn)(比如Java Swing GUI技術(shù))。
Reactive的另外一種實(shí)現(xiàn)方式就是消息隊(duì)列。
1.1 標(biāo)準(zhǔn)概念 1.1.1 維基百科講法https://en.wikipedia.org/wiki...
In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow
關(guān)鍵點(diǎn):
聲明式的編程范式
數(shù)據(jù)流
傳播改變
使用靜態(tài)數(shù)組或者動(dòng)態(tài)事件
1.1.2 Reactive-Streams講法Reactive標(biāo)準(zhǔn):http://www.reactive-streams.org/
Reactive Streams JVM實(shí)現(xiàn):https://github.com/reactive-s...
Handling streams of data—especially “l(fā)ive” data whose volume is not predetermined—requires special care in an asynchronous system. The most prominent issue is that resource consumption needs to be carefully controlled such that a fast data source does not overwhelm the stream destination. Asynchrony is needed in order to enable the parallel use of computing resources, on collaborating network hosts or multiple CPU cores within a single machine.
關(guān)鍵點(diǎn):
處理的是數(shù)據(jù)流(活躍數(shù)據(jù),動(dòng)態(tài)數(shù)據(jù))
數(shù)據(jù)容量無法預(yù)判
異步系統(tǒng)
資源消費(fèi)需要精細(xì)控制
data source(數(shù)據(jù)上游),stream destination(數(shù)據(jù)下游)
1.2 實(shí)現(xiàn)框架 1.2.1 ReactiveXIn summary, Reactive Streams is a standard and specification for Stream-oriented libraries for the JVM that
process a potentially unbounded number of elements
in sequence,
asynchronously passing elements between components,
with mandatory non-blocking backpressure.
ReactiveX:http://reactivex.io/intro.html
ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.
是一個(gè)類庫
異步
基于事件
可觀察的數(shù)據(jù)/事件序列(sequences of data and/or events)
有順序的
可增長的
設(shè)計(jì)模式上說:繼承了觀察者模式
數(shù)據(jù)結(jié)構(gòu)上說:支持?jǐn)?shù)據(jù)序列(sequences of data),其實(shí)就是數(shù)據(jù)流(streams of data),并且需要屏蔽高并發(fā)相關(guān)(線程,同步,線程安全,并發(fā)數(shù)據(jù)結(jié)構(gòu),非阻塞I/O等)
You can think of the Observable class as a “push” equivalent to Iterable, which is a “pull.” With an Iterable, the consumer pulls values from the producer and the thread blocks until those values arrive.
Observable觀察者模式是“push”模式
Iterable是“pull”模式
數(shù)據(jù)已經(jīng)準(zhǔn)備好了,自己直接去“拉”即可
Iterable:For Each語句的提升
Iterator:迭代器(Java 5)
1.2.2 Reactorhttps://projectreactor.io/doc...
Reactor is a fully non-blocking reactive programming foundation for the JVM, with efficient demand management (in the form of managing "backpressure"). It integrates directly with the Java 8 functional APIs, notably CompletableFuture, Stream, and Duration. It offers composable asynchronous sequence APIs Flux (for [N] elements) and Mono (for [0|1] elements), extensively implementing the Reactive Streams specification.1.3 Spring WebFluxReactor also supports non-blocking inter-process communication with the reactor-netty project. Suited for Microservices Architecture, Reactor Netty offers backpressure-ready network engines for HTTP (including Websockets), TCP, and UDP. Reactive Encoding and Decoding are fully supported.
https://docs.spring.io/spring...
The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+ containers.
可以運(yùn)行在Netty,Undertow,Servlet 3.1+等容器中
Servlet 3.1也有異步處理
1.3.1 Why WebFlux?Part of the answer is the need for a non-blocking web stack to handle concurrency with a small number of threads and scale with fewer hardware resources. Servlet 3.1 did provide an API for non-blocking I/O. However, using it leads away from the rest of the Servlet API, where contracts are synchronous (Filter, Servlet) or blocking (getParameter, getPart). This was the motivation for a new common API to serve as a foundation across any non-blocking runtime. That is important because of servers (such as Netty) that are well-established in the async, non-blocking space.The other part of the answer is functional programming. Much as the addition of annotations in Java 5 created opportunities (such as annotated REST controllers or unit tests), the addition of lambda expressions in Java 8 created opportunities for functional APIs in Java. This is a boon for non-blocking applications and continuation-style APIs (as popularized by CompletableFuture and ReactiveX) that allow declarative composition of asynchronous logic. At the programming-model level, Java 8 enabled Spring WebFlux to offer functional web endpoints alongside annotated controllers.
異步非阻塞
異步非阻塞(non-blocking web stack)
少量線程數(shù)處理并發(fā)性(handle concurrency with a small number of threads)
少量硬件資源提高伸縮性(scale with fewer hardware resources.)
Servlet 3.1 提供一個(gè)非阻塞API
函數(shù)式編程(functional programming)
1.3.2 Define “Reactive”The term, “reactive,” refers to programming models that are built around reacting to change?—?network components reacting to I/O events, UI controllers reacting to mouse events, and others. In that sense, non-blocking is reactive, because, instead of being blocked, we are now in the mode of reacting to notifications as operations complete or data becomes available.There is also another important mechanism that we on the Spring team associate with “reactive” and that is non-blocking back pressure. In synchronous, imperative code, blocking calls serve as a natural form of back pressure that forces the caller to wait. In non-blocking code, it becomes important to control the rate of events so that a fast producer does not overwhelm its destination.
Reactive Streams is a small spec (also adopted in Java 9) that defines the interaction between asynchronous components with back pressure. For example a data repository (acting as Publisher) can produce data that an HTTP server (acting as Subscriber) can then write to the response. The main purpose of Reactive Streams is to let the subscriber to control how quickly or how slowly the publisher produces data.
指的是圍繞對(duì)變化作出反應(yīng)而構(gòu)建的編程模型
對(duì)I / O事件做出反應(yīng)的網(wǎng)絡(luò)組件
對(duì)鼠標(biāo)事件做出反應(yīng)的UI控制器等。
非阻塞在某種程度上說就是Reactive(In that sense, non-blocking is reactive)
非阻塞背壓
小的規(guī)范
1.3.2 PerformancePerformance has many characteristics and meanings. Reactive and non-blocking generally do not make applications run faster. They can, in some cases, (for example, if using the WebClient to execute remote calls in parallel). On the whole, it requires more work to do things the non-blocking way and that can increase slightly the required processing time.The key expected benefit of reactive and non-blocking is the ability to scale with a small, fixed number of threads and less memory. That makes applications more resilient under load, because they scale in a more predictable way. In order to observe those benefits, however, you need to have some latency (including a mix of slow and unpredictable network I/O). That is where the reactive stack begins to show its strengths, and the differences can be dramatic.
并不是為了變得更快
一般而言,并不是為了使應(yīng)用程序運(yùn)行更快(Reactive and non-blocking generally do not make applications run faster)
為了非阻塞需要額外的工作可能會(huì)增加處理時(shí)間(it requires more work to do things the non-blocking way and that can increase slightly the required processing time)
關(guān)鍵的一個(gè)好處(The key expected benefit)
利用固定、少量的線程,小的內(nèi)存提供伸縮性的能力(reactive and non-blocking is the ability to scale with a small, fixed number of threads and less memory)
某種程度上,WebFlux并不適合Web請(qǐng)求
1.4 特性異步
非阻塞
事件驅(qū)動(dòng)
可能有背壓(back pressure)
多路復(fù)用
這點(diǎn)是NIO中的特點(diǎn),Reactive是整個(gè)編程范式的概念
高吞吐
防止回調(diào)地獄(Callback Hell)
Callbacks are hard to compose together, quickly leading to code that is difficult to read and maintain (known as "Callback Hell").
關(guān)于回調(diào)地域相關(guān)說明參見以下鏈接
回調(diào)地域
2. Reactive使用場景The second approach (mentioned earlier), seeking more efficiency, can be a solution to the resource wasting problem. By writing asynchronous, non-blocking code, you let the execution switch to another active task using the same underlying resources and later come back to the current process when the asynchronous processing has finished.
But how can you produce asynchronous code on the JVM? Java offers two models of asynchronous programming:
Callbacks: Asynchronous methods do not have a return value but take an extra callback parameter (a lambda or anonymous class) that gets called when the result is available. A well known example is Swing’s EventListenerhierarchy.
Futures: Asynchronous methods return a Future
immediately. The asynchronous process computes a T value, but the Future object wraps access to it. The value is not immediately available, and the object can be polled until the value is available. For instance, ExecutorService running Callable tasks use Future objects.
Long Live模式:Netty I/O連接(RPC)
Short Live模式:HTTP(需要超時(shí)時(shí)間)
短平快的應(yīng)用并不適合Reactive,需要做合理的timeout時(shí)間規(guī)劃
Reactive 性能測(cè)試: https://blog.ippon.tech/sprin...
3. Reactive理解誤區(qū)Web:快速響應(yīng)(Socket Connection Timeout)
Tomcat Connector Thread Pool(200個(gè)線程) --> Reactive Thread Pool(50個(gè)線程)
I/O連接從Tomcat轉(zhuǎn)移到了Reactive
Tomcat Thread 負(fù)責(zé)處理連接(可以由200個(gè)連接擴(kuò)充到2000個(gè)連接)
Reactive Thread負(fù)責(zé)處理任務(wù)(有可能會(huì)處理不過來)
繼續(xù)等待(Timeout無限)
Timeout
4. 設(shè)計(jì)Reactive Web 4.1 異步設(shè)計(jì)利用Servlet 3.1規(guī)范中的異步處理(Asynchronous processing),異步上下文(AsyncContext)實(shí)現(xiàn)異步Servlet。
4.2 非阻塞設(shè)計(jì)Some times a filter and/or servlet is unableto complete the processing of a request
without waiting for a resource or event before generating a response. For example, a
servlet may need to wait for an available JDBC connection, for a response from a
remote web service, for a JMS message, or for an application event, before
proceeding to generate a response. Waiting within the servlet is an inefficient
operation as it is a blocking operation that consumes a thread and other limited
resources. Frequently a slow resource such as a database may have many threads
blocked waiting for access and can cause thread starvation and poor quality of
service for an entire web container.
The asynchronous processing of requests is introduced to allow the thread may
return to the container and perform other tasks. When asynchronous processing
begins on the request, another thread or callback may either generate the response
and call completeor dispatch the request so that it may run in the context of the
container using the AsyncContext.dispatchmethod. A typical sequence of events
for asynchronous processing is:The request is received and passed via normal filters for authentication etc. to the
servlet.The servlet processes the request parameters and/or content to determine the
nature of the request.The servlet issues requests for resources or data, for example, sends a remote web
service request or joins a queue waiting for a JDBC connection.The servlet returns without generating a response.
After some time, the requested resourcebecomes available, the thread handling
that event continues processing either in the same thread or by dispatching to a
resource in the container using the AsyncContext.
利用Servlet 3.1規(guī)范中的Non Blocking IO
Non-blocking request processing in the Web Container helps improve the ever5. 實(shí)現(xiàn)Reactive Web
increasing demand for improved Web Container scalability, increase the number of
connections that can simultaneously be handled by the Web Container. Nonblocking IO in the Servlet container allowsdevelopers to readdata as it becomes
available or write data when possible to do so. Non-blocking IO only works with
async request processing in Servlets and Filters (as defined in Section 2.3.3.3,
“Asynchronous processing” on page 2-10), and upgrade processing (as defined in
Section 2.3.3.5, “Upgrade Processing” on page 2-20). Otherwise, an
IllegalStateExceptionmust be thrown when
ServletInputStream.setReadListeneror
ServletOutputStream.setWriteListeneris invoked.
實(shí)現(xiàn)代碼鏈接:github
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/74655.html
摘要:原文鏈接編程方法論響應(yīng)式與代碼設(shè)計(jì)實(shí)戰(zhàn)序,來自于微信公眾號(hào)次靈均閣正文內(nèi)容在一月的架構(gòu)和設(shè)計(jì)趨勢(shì)報(bào)告中,響應(yīng)式編程和函數(shù)式仍舊編列在第一季度的早期采納者中。 原文鏈接:《Java編程方法論:響應(yīng)式RxJava與代碼設(shè)計(jì)實(shí)戰(zhàn)》序,來自于微信公眾號(hào):次靈均閣 正文內(nèi)容 在《2019 一月的InfoQ 架構(gòu)和設(shè)計(jì)趨勢(shì)報(bào)告》1中,響應(yīng)式編程(Reactive Programming)和函數(shù)式...
摘要:響應(yīng)式編程是基于異步和事件驅(qū)動(dòng)的非阻塞程序,只是垂直通過在內(nèi)啟動(dòng)少量線程擴(kuò)展,而不是水平通過集群擴(kuò)展。三特性常用的生產(chǎn)的特性如下響應(yīng)式編程模型適用性內(nèi)嵌容器組件還有對(duì)日志消息測(cè)試及擴(kuò)展等支持。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號(hào):泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 02:WebFlux 快速入門實(shí)踐 文章工程: JDK...
摘要:是一個(gè)倡議,它提倡提供一種帶有非阻塞背壓的異步流處理的標(biāo)準(zhǔn)。是標(biāo)準(zhǔn)的實(shí)現(xiàn)之一。的實(shí)現(xiàn)細(xì)節(jié)請(qǐng)求響應(yīng)的與請(qǐng)求響應(yīng)的暴露為是請(qǐng)求的的消費(fèi)者是響應(yīng)的的生產(chǎn)者內(nèi)部的內(nèi)部 北京時(shí)間 9 月 26 日,Oracle 官方宣布 Java 11 正式發(fā)布 一、JDK HTTP Client介紹 JDK11中的17個(gè)新特性 showImg(https://segmentfault.com/img/remo...
摘要:中的全局異常處理不能直接用來處理,通過跟蹤異常信息的拋出,找到對(duì)應(yīng)的源碼,自定義一些處理邏輯來符合業(yè)務(wù)的需求。如果不做處理,當(dāng)發(fā)生異常時(shí),默認(rèn)給出的錯(cuò)誤信息是頁面,不方便前端進(jìn)行異常處理。需要對(duì)異常信息進(jìn)行處理,返回格式的數(shù)據(jù)給客戶端。 Spring Cloud Gateway中的全局異常處理不能直接用@ControllerAdvice來處理,通過跟蹤異常信息的拋出,找到對(duì)應(yīng)的源碼,自...
摘要:構(gòu)建于四個(gè)指導(dǎo)性的原則。在這個(gè)文章的其余部分我們將繼續(xù)深入異步邊界的概念。電商領(lǐng)域的一致性的重要性并不是碰巧出現(xiàn)的。性能持久性安全都是的方面。來見識(shí)下騎士資本集團(tuán)在年發(fā)生軟件故障的經(jīng)歷。接下來的分鐘發(fā)生的事情是一場惡夢(mèng)。 What is Reactive Programming?為了了解Reactive——從編程范式至其背后的動(dòng)機(jī),有必要了解現(xiàn)在的開發(fā)者和公司在十年前不曾面對(duì)的挑戰(zhàn)。 ...
閱讀 2779·2021-09-24 10:34
閱讀 1882·2021-09-22 10:02
閱讀 2272·2021-09-09 09:33
閱讀 1472·2021-08-13 15:02
閱讀 3283·2020-12-03 17:10
閱讀 1199·2019-08-30 15:44
閱讀 2157·2019-08-30 12:58
閱讀 3241·2019-08-26 13:40