WebFlux基于Netty,其异步复用线程模型与非阻塞IO带来的优势相比SpringMVC/Tomcat技术栈,可以带来更小的线程切换开销,更重要的是可以避免应用间的级联故障。对于Tomcat模型经常需要考虑的问题是线程池的参数调优,以及线程池隔离来做故障模块的隔离熔断。而异步非阻塞技术栈天然避免了这些问题。
以往由于JDBC是同步阻塞协议的原因,WebFlux技术栈一般应用于纯请求转发类的场景,在涉及到关系型数据的场景受限。有了R2DBC之后则补上了这块拼图。
使用的demo应用代码
搭建一个WebFlux应用,包含/user/getAllUser/
和/user/slow/
两个接口,应用使用r2dbc连接MySQL数据库,实现全链路异步非阻塞。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wangan</groupId>
<artifactId>springbootone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootone</name>
<description>springbootone</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.3</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.8.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.17.RELEASE</version>
<configuration>
<mainClass>com.wangan.springbootone.SpringbootoneApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件:
spring.application.name=springbootone
server.port=8080
spring.r2dbc.driver-class-name=com.mysql.jdbc.Driver
spring.r2dbc.username=root
spring.r2dbc.password=root
spring.r2dbc.url=r2dbc:pool:mysql://xxx.xx.xxx.187:3306/db_core?useSSL=false&useUnicode=true&characterEncoding=UTF8&autoReconnect=true
repository
public interface UserRepository extends ReactiveCrudRepository<User, Integer> {
@Query(value = "select sleep(10) ")
public Mono<User> slowQuery();
}
entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("webflux_user")
public class User {
@Id
private int id;
private String username;
private String password;
}
service
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public Mono<User> addUser(User user) {
return userRepository.save(user);
}
public Mono<ResponseEntity<Void>> delUser(int id) {
return userRepository.findById(id)
.flatMap(user -> userRepository.delete(user).then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))))
.defaultIfEmpty(new ResponseEntity<Void>(HttpStatus.NOT_FOUND));
}
public Mono<ResponseEntity<User>> updateUser(User user) {
return userRepository.findById(user.getId())
.flatMap(user0 -> userRepository.save(user))
.map(user0 -> new ResponseEntity<User>(user0, HttpStatus.OK))
.defaultIfEmpty(new ResponseEntity<User>(HttpStatus.NOT_FOUND));
}
public Flux<User> getAllUser() {
return userRepository.findAll();
}
public Mono<ResponseEntity<User>> slowFunction(){
return userRepository.slowQuery()
//.flatMap(user0 -> userRepository.save(user))
.map(user0 -> new ResponseEntity<User>(user0, HttpStatus.OK))
.defaultIfEmpty(new ResponseEntity<User>(HttpStatus.NOT_FOUND));
}
}
controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "getAllUser", method = RequestMethod.GET)
public Flux<User> getAllUser() {
return userService.getAllUser();
}
@RequestMapping(value = "slow", method = RequestMethod.GET)
public Mono<ResponseEntity<User>> slow() {
return userService.slowFunction();
}
}
异步非阻塞能力测试
为了实验方便,先把webflux的工作线程设置为1:
@Configuration
public class ReactNettyConfiguration {
@Bean
public ReactorResourceFactory reactorClientResourceFactory() {
System.setProperty("reactor.netty.ioSelectCount","1");
// 这里工作线程数为2-4倍都可以。看具体情况
//int ioWorkerCount = Math.max(Runtime.getRuntime().availableProcessors()*3, 4));
System.setProperty("reactor.netty.ioWorkerCount",String.valueOf(1));
return new ReactorResourceFactory();
}
}
此时应用内的线程情况,只有一个工作线程来处理请求:
两个接口
localhost:8080/user/getAllUser/
先调用slow接口(用select sleep(10)模拟的由慢SQL导致的慢接口),再调用getAllUser,后者可以马上返回。同样的测试案例和配置,如果使用SpringMVC + JDBC的组合就会发生slow接口10秒后返回,此时getAllUser才可以返回。
后续
- 关于远程调用的异步非阻塞:上述应用中使用r2dbc调用MySQL,如果该应用需要调用远程的Http服务,则需要使用
WebClient
替代RestTemplate
、Apache HttpClient
之类的传统Http客户端组件,实现全链路异步非阻塞。如果是该应用需要调用的后端服务是Dubbo服务,理论上只要将Dubbo客户端调用结果转为Reactive Streams返回仍然可以实现上述非阻塞调用效果。 - (未完成)上述例子中,我们利用新的连接驱动r2dbc代替了jdbc,实现了异步非阻塞的与数据库网络通信,然后这里其实也可以使用个连接池,见:Data Access with R2DBC :: Spring Framework “When you use Spring’s R2DBC layer, you can configure your own with a connection pool implementation provided by a third party. A popular implementation is R2DBC Pool (). Implementations in the Spring distribution are meant only for testing purposes and do not provide pooling.”
参考
https://docs.spring.io/spring-framework/reference/data-access/r2dbc.html
SpringBoot之Webflux&R2DBC操作MySQL
Spring Cloud Gateway 雪崩了,我 TM 人傻了 - 知乎 (zhihu.com)
https://blog.csdn.net/weixin_42182797/article/details/117216371