Servlet and Reactive technology stack
In the opened Spring
official document, we Reactive
can see the following architecture diagram in one column, in which it is obvious Reactive
that the technology stack and Servlet
the technology stack are completely parallel. It means that Servlet web
the type of our daily development is only half of the content, and the other half of the world is Reactive
. The corresponding dependencies between the two are as follows.
<!--Servlet web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Reactive web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
servlet-reactive
Through the architecture diagram, we can know that the commonly used ones Servlet web
are what we often say Spring MVC
, the underlying technology stack support JDBC
, etc., but Reactive web
the Spring WebFlux
underlying persistence layer support Mongo
, etc. but not supported JDBC
.
What is Spring WebFlux
From the above figure, we can see that Spring WebFlux
it is an asynchronous and non-blocking Web
framework, which can make full use of multi-core CPU
hardware resources to handle a large number of concurrent requests. Relatively Spring MVC
speaking, Spring MVC
it is built on Servlet API
top of it, using a synchronous blocking I/O
model.
Since Spring WebFlux
the underlying layer uses reactive programming and event-based asynchronous drive, it can greatly improve the throughput of the system. But be aware that this will not improve the response time of the request, just increase the throughput.
And according to the above architecture diagram, we can find that Spring WebFlux
the underlying persistence layer does not support JDBC
, that is to say, it does not support MySQL
transactional databases,
give a chestnut
Having said so much before, Spring WebFlux
how did it develop? case
Let me show you a simple example. Through the official documentation before the demonstration, we can find that Spring WebFlux
and Spring MVC
can share many components, such as @Controller
, Tomcat
etc., but there are also many differences.
Spring
The official Reactive
address is: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html
springmvc-springwebflux
Based on the code of our previous demo-reactive
project, or small partners can also starter.spring.io
download a SpringBoot
project code on it, and add spring-boot-starter-webflux
dependencies.
Define DTOs
package com.example.reactive.demoreactive.dto;
public class User {
private String name;
private int age;
//省略 getter setter
}
Define Controller
package com.example.reactive.demoreactive.controller;
import com.example.reactive.demoreactive.dto.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
@RestController
public class UserController {
@GetMapping("/hello")
public String sayHello() {
return "hello";
}
@GetMapping("/user/get")
public Mono<User> getUser() {
User user = new User();
user.setName("Java极客技术");
user.setAge(18);
return Mono.just(user);
}
@GetMapping("/user/list")
public Flux<List<User>> getAllUser() {
User user = new User();
user.setName("Java极客技术");
user.setAge(18);
List<User> list = new ArrayList<>();
list.add(user);
return Flux.just(list);
}
}
Then start our service, and then access our interface through a browser.
The access results of the above three interfaces are as follows
hello
user-get
user-list
You can see that we can get the data normally in the browser. The above code SpringMVC
is very similar to the code we usually write. The difference is that there are two classes Mono
and sums FLux
.
About Mono and Flux are the concepts of reactive programming. Mono returns 0 or 1 element, and Flux returns 0-N elements. For more details, you can refer to the official document https://projectreactor.io/docs/core/ release/api/ to view
Summarize
Although judging from the above writing method, there is no big difference between the writing method and the writing method, but the underlying mechanisms of the two are completely different, and the technology stacks are not completely the same, so you need to base your daily technology selection Spring WebFlux
on SpringMVC
Choose according to the actual situation.
The suggestion given by Ah Fen here is that if the current project is SpringMVC
the best, then don’t think about changing Spring WebFlux
to the architecture, because it is not necessary. If you want to develop a new project, you need high throughput and the bottom layer does not depend on transactional data.
According to the database, then you can try to use it Spring WebFlux
.
Source: Java Geek Technology
Author: Duck Blood Fan Tang