In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Webflux in Spring Boot". In daily operation, I believe many people have doubts about how to use Webflux in Spring Boot. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Webflux in Spring Boot"! Next, please follow the editor to study!
To make it easier for you to understand, let's first understand a few concepts.
Responsive programming
In computers, responsive programming or reactive programming (English: Reactive programming) is a programming paradigm oriented to data flow and change propagation. This means that static or dynamic data streams can be easily expressed in programming languages, and the relevant computing models automatically propagate the changed values through the data streams.
For example, in an imperative programming environment, a=b+c means that the result of an expression is assigned to a, and then changing the value of b or c does not affect a. In responsive programming, however, the value of an is updated as b or c is updated.
Responsive programming is based on asynchronous and event-driven non-blocking programs that only need to start a small number of thread extensions within the program rather than horizontally through clusters.
In vernacular, most of the programs we wrote were blocking programs, and when a request came, the task would be blocked until the task was completed, and then returned to the front end; responsive programming only submitted a request to the back end, and the back end would arrange another thread to execute the task, and then notify the front end asynchronously when the task was completed.
Reactor
Among the responsive programming libraries in the field of Java, the most famous is Reactor. Reactor is also the foundation of reactive programming in Spring 5, and Webflux is built on Reactor.
Reactor is a basic library of asynchronous applications based on JVM. Abstract libraries for building event-and data-driven applications are provided for Java, Groovy, and other JVM languages. Reactor performance is quite high, and on the latest hardware platforms, non-blocking dispensers can handle 1500 events per second.
To put it simply, Reactor is a lightweight JVM base library that helps your service or application deliver messages efficiently and asynchronously. There are two very important concepts in Reactor Flux and Mono.
Flux and Mono
Flux and Mono are two basic concepts in Reactor. Flux represents an asynchronous sequence of 0 to N elements. There can be three different types of message notifications in this sequence: normal messages containing elements, messages at the end of the sequence, and messages with sequence errors. When a message notification is generated, the corresponding methods onNext (), onComplete (), and onError () in the subscriber are called.
Mono represents an asynchronous sequence that contains 0 or 1 elements. This sequence can also contain the same three types of message notifications as Flux. Conversion can be done between Flux and Mono. Count a Flux sequence and the result is a Mono
What is WebFlux?
The name of the WebFlux module is spring-webflux, and the Flux in the name comes from the class Flux in Reactor. Spring webflux has a new non-blocking functional Reactive Web framework that can be used to build asynchronous, non-blocking, event-driven services that perform very well in terms of scalability.
The key expected benefit of non-blocking is the ability to scale with a small fixed number of threads and less memory. WebFlux supports two different programming models on the server side:
Annotation-based @ Controller and other annotations also support Spring MVC
Functional, Java 8 lambda style routing and processing
As shown in the figure, the WebFlux module is composed of three new components: Router Functions, WebFlux, and Reactive Streams from top to bottom.
Router Functions
Spring MVC annotations for standard @ Controller,@RequestMapping, etc., provide a functional style of API for creating Router, Handler, and Filter.
WebFlux
Core components, coordinate upstream and downstream components to provide responsive programming support.
Reactive Streams
An asynchronous data flow processing standard that supports back pressure (Backpressure). The mainstream implementation of RxJava and Reactor,Spring WebFlux integration is Reactor.
By default, Spring Boot 2 uses Netty WebFlux because Netty is widely used in asynchronous non-blocking space, and asynchronous non-blocking connections can save more resources and provide higher responsiveness. By comparing Servlet 3.1 non-blocking I / O, there is not much use because it is more expensive to use, and Spring WebFlux opens a practical path.
It is worth noting that the only databases that support reactive programming are MongoDB, redis, Cassandra, Couchbase
Spring Webflux
Spring Boot 2.0 includes a new spring-webflux module. The module includes support for responsive HTTP and WebSocket clients, as well as support for programs such as REST,HTML and WebSocket interaction. Generally speaking, Spring MVC is used for synchronous processing and Spring Webflux for asynchronous processing.
Spring Boot Webflux has two programming models, one is similar to Spring MVC annotations, and the other is responsive based on Reactor.
Get started quickly
Add webflux dependency
Org.springframework.boot
Spring-boot-starter-webflux
Through the dependency graph of IEDA, we can realize that spring-boot-starter-webflux depends on spring-webflux, Reactor, and Netty related dependency packages.
Create Controller
@ RestController
Public class HelloController {
@ GetMapping ("/ hello")
Public Mono hello () {
Return Mono.just ("Welcome to reactive world ~")
}
}
From the above example, you can see that the development pattern is not very different from the previous Spring Mvc pattern, except in the return value of the method.
The just () method specifies all the elements contained in the sequence.
The return value of responsive programming must be Flux or Mono, which can be converted to each other.
Test class
@ RunWith (SpringRunner.class)
@ WebFluxTest (controllers = HelloController.class)
Public class HelloTests {
@ Autowired
WebTestClient client
@ Test
Public void getHello () {
Client.get (). Uri ("/ hello"). Exchange (). ExpectStatus (). IsOk ()
}
}
Run the test class, and the test case indicates that the service is normal. After starting the project, visit the address: http://localhost:8080/hello, and return information on the page:
Welcome to reactive world ~
It is proved that the Webflux integration is successful.
At this point, the study on "how to use Webflux in Spring Boot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.