SpringCloud combat service in response to the decline tutorial series (Chapter III)

Then before:

SpringCloud combat service in response to the decline tutorial series (the first chapter)

SpringCloud combat service in response to the decline series of tutorials (Chapter II)

1.1.3Reactor framework

Reactive programming is a programming model This section describes the specific implementation of this programming model Project Reactor tool framework. Reactor framework is implemented in a default frame Spring5 reactive programming employed.

Project Reactor: https://projectreactor.io/

1.1.4 responsive programming Technical Overview

Reactive programming is the use of asynchronous data streams are programmed, is a form of observers (Observer) mode in nature. This section first discuss the implementation of some common asynchronous operation mode, and then drawn mainstream reactive programming implementation technology.

1. A common way to implement asynchronous operation

In Java, in order to achieve non-blocking asynchronous, typically using a callback (Callback) and Future These two mechanisms, but both mechanisms there are some limitations.

(1) callback

Method A callback shown in FIG meaning, i.e., class A () method call class B Method B () method, then the class methodB B () method performs callback () method of class A is completed before the active call. Callback is embodied in a two-way invocation.

SpringCloud combat service in response to the decline tutorial series (Chapter III)

It can be seen pullback in the task execution process will not cause any obstruction, the result of a task and ready, the callback will be executed. But we should also see when using the callback mechanism, the code jumps to a method in another class from a class a method, resulting in discontinuities process. For asynchronous execution of a single layer, the callback is easy to use.

But for nested asynchronous multi-layered combination of terms, it is very awkward. So the callback is difficult on a large scale in combination, because soon it will lead to code difficult to understand and maintain that form the so-called "hell callback (Callback Hell)" problem.

(2) Future

Future model can be understood as a simple scenario: There is a task you want to process, submit the task to the Future, Future will complete this task within a certain time, and during this time we can do other things.

Future as an implementation mode, Java Future interface contains only the following five methods

public interface Puture<v>{
   boolean cancel (boolean mayinterrupt Ifrunning):
   boolean iscancelled
   boolean isdone ():
   V get() throws Interruptedexception, Executionexception;
   V get (long timeout, Timeunit unit)?
      throws Interruptedexception, Executionexception, Timeoutexception
}

Future cancel method to cancel interface for performing tasks; iscancelledo method used to determine whether the task has been canceled; two get () method will wait for the task execution ends and get the result, the difference is whether you can set the timeout, the last isDone () methods to determine whether the task has been completed.

While Future demand can be implemented asynchronously get the results, but it does not provide notification mechanism, we do not know when Future completed. To get results, we either return using blocking both get () method waits Future results, then equivalent to perform synchronization operations;

Either method using isDone Future determine whether the polling is completed, so that consumes CPU resources. So, Future calls for simple single layer, for nested asynchronous calls is also very heavy, not suitable for complex service link building.

In view of the defects of the Future mechanism, introduced in Java8 Completablefuture mechanism. Completablefuture to some extent make up for the shortcomings of common Future. After the asynchronous task is completed, we do not need to wait for the results of using the task, you can directly through thenaccept (), thenApply (), thencompose () methods will result in front of asynchronous processing to another thread to handle asynchronous event handling.

Completablefuture provides a very powerful Future extensions, we can help simplify the complexity of asynchronous programming, and provides the ability to functional programming, the results can be processed by way of correction, conversion also supports a variety of methods and compositions provided Completablefuture .

For the daily development work, most of the time we are dealing with simple tasks, then use Completablefuture indeed meet the demand. However, when the system is more complex, or we need to handle the task itself is very complicated, Completablefuture for a combination of multiple processes is still not easy. Arrange multiple use Completablefuture Future is possible, but not easy. We will worry about whether to write out the code really is no problem, but over time, these codes will become increasingly complex and difficult to maintain. To this end, we need to introduce relevant technologies and frameworks reactive programming of these technologies and frameworks can support more easily maintain asynchronous processing code in the future.

2. mainstream reactive programming techniques to achieve

At present, the mainstream reactive programming implementation techniques include Rxjava, Akka Streams, Vert.x and Project Reactor and so on.

(1)Rxjava

Reactive Extensions (Rx) is a class library that incorporates event-based asynchronous observable (Observable) sequence-driven programming, first used in Microsoft's NET platform. The Rxjava is Reactive Extensions for the Java implementation is constructed by using the Observable / Flowable serial asynchronous and event-based library, there are two versions 2.x and version 1x achieved.

Before Rxjava1.x born responsive flow specification, although conversion and interfaces responsive stream, but because of the underlying implementation, use is not very intuitive. Rxjava2 consideration in the design and implementation of the integration with existing specifications, in accordance with the flow specification on the interface responsive been rewritten, and the function of the back pressure version 1.x separate out. But in order to maintain compatibility with the Rxjava1x, Rxjava2 used in many places it is not intuitive. More details about Rxjava, refer to the official website (http://reactivex.io/), we do not make too much introduction.

(2) Akka Streams

Akka runs on the JVM, a tool member is constructed of high concurrency and high elastic distributed message-driven application. Actor is the core concept Akka, it is an object that encapsulates the state and behavior, can communicate through the exchange of messages between the Actor. By Actor simplifies lock and thread management, you can very easily develop a proper system of parallel and concurrent programs.

Akka is also a member of the initial flow specification responsive, and Akka Streams are Aka-based responsive achieve flow, provides an abstraction on top of the higher level of Akka existing role models, support the back pressure responsive mechanism.

(3) Vertx

Vert. X is an open source Java tool under the Eclipse Foundation, is an asynchronous web application development framework for building highly concurrent, asynchronous, scalable, Web applications, multi-language support. Vert.x response is to build systems designed, based on event-driven architecture, Vert x processing task to achieve a non-blocking mechanism.

verx contains Vert.x Reactive Streams tool magazine, the tool magazine provides an implementation of the Vver.x responsive flow specification. We can provide a stream by Vert x readable and writable stream processing flow responsive specification publishers and subscribers.

(4) Project Reactor

Introducing a reactive programming mechanism Spring S, and the default Spring5 Project Reactor as integrated implementation framework of the mechanism. Reactor birth of late, can be considered a second generation responsive development framework.

So, it is a fully responsive flow specification based on the design and implementation of the tool library, not that kind of history Rxjava package, more intuitive in use, easy to understand. However, the API design and form of expression, Reactor and Rxjava compare like with like, we can say Reactor based on responsive flow specification, but in terms of API and as far as possible to move closer to Rxjava.

Mono and Flux Reactor core components are the two representative Flux asynchronous sequence comprising 0 to n elements, and Mono 0 or a sequence of said element. Reactor framework so that the focus of our discussion, we introduced the next section will explain the specific Flux Reactor framework and Mono.

Guess you like

Origin www.cnblogs.com/javazhiyin/p/11410837.html