1. What is WebFlux

WebFlux is a sub-module of responsive programming, which is level with SpringMVC and made a responsive framework of MVC based on Reactor, including various routing, asynchronous processing and support of various asynchronous containers. Reactor is a Reactive Streams FRAMEWORK based on the Reactive Streams API specification and runs on Java8.

2, Reactive Streams

2.1 Interface Definition

Reactive Streams is “a specification that provides a standard for asynchronous stream processing based on non-blocking backpressure”. 9 in the Java version, the reactive Flow specification is included in the JDK, the corresponding API interface is Java. The util.. Concurrent Flow. It is a set of specifications containing the TCK tool suite and four simple interfaces (Publisher, Subscriber, Subscription, and Processor)

  1. Publisher: It publishes (or sends) elements to current subscribers based on the request it receives.

  2. Subscriber subscribers subscribe to and receive elements from publishers.

  3. Subscription Preserves the Subscription relationship between a publisher and a subscriber.

  4. The Processor

    Processor acts as the processing phase for subscribers and publishers.
    ,r>

2.2 back pressure

It means that subscribers can interact with publishers, adjust the rate of publishing data, and solve the problem that subscribers are overwhelmed; The submit method of Publisher SubmissionPublisher is a block method. Subscribers will have a buffer pool, which defaults to flow.defaultBufferSize () = 256. When the subscriber’s buffer pool is full, the publisher calls the Submit method to publish data, which is blocked and the publisher stops (slows). Once the subscriber has consumed the data (call the Subscription. Request method) and the buffer pool is available, the Submit method continues to execute. Of course it will slow down.

3, Reactor

Reactor is a Reactive Streams API based framework running on Java8.

3.1 Flux and Mono

Flux and Mono are two basic concepts in Reactor. Both Flux and Mono are Publisher implementations of Reactive Streams. Flux represents an asynchronous sequence of 0 to N elements. There are three different types of message notifications that can be contained in this sequence: normal messages containing elements, end-of-sequence messages, and sequence error messages. The corresponding subscriber methods onNext(), onComplete(), and onError() are called when message notifications are generated. Mono represents an asynchronous sequence of 0 or 1 elements. This sequence can also contain the same three types of message notifications as Flux. There can be transitions between Flux and Mono. The result of counting a Flux sequence is a Mono object. When you combine two Mono sequences together, you get a Flux object.

1. Use of WebFlux

The key to WebFlux is to write your own code to return streams (Flux/Mono), and the Spring framework handles the subscriptions. The Spring framework provides two development patterns for writing responsive code, using the pre-MVC annotation pattern and using the Router Function pattern, both requiring our code to return a stream

Only the Web server supports react. The database must support react from the front to the back, and the client must also support react. The entire link from the request initiation to the return must be a REACTOR.