早上看到一篇关于Spring Boot虚拟线程和Webflux性能对比的文章,觉得还不错。内容较长,抓重点给大家介绍一下这篇文章的核心内容,方便大家快速阅读。
作者采用了一个尽可能贴近现实操作的场景:
这里要对比的两个核心技术点是:
不论是Webflux还是虚拟线程,这两个都是为了提供程序的高并发能力而生,那么谁更胜一筹呢?下面一起看看具体的测试。
运行环境与工具
数据准备
mysql> desc users;+--------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+--------------+------+-----+---------+-------+| email | varchar(255) | NO | PRI | NULL | || first | varchar(255) | YES | | NULL | || last | varchar(255) | YES | | NULL | || city | varchar(255) | YES | | NULL | || county | varchar(255) | YES | | NULL | || age | int | YES | | NULL | |+--------+--------------+------+-----+---------+-------+6 rows in set (0.00 sec)
application.properties配置文件:
server.port=3000spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=falsespring.datasource.username= testuserspring.datasource.password= testpwdspring.jpa.hibernate.ddl-auto= updatespring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
User实体类(为了让文章让简洁一些,这里DD省略了getter和setter):
@Entity@Table(name = "users")public class User { @Id private String email; private String first; private String last; private String city; private String county; private int age;}
应用主类:
@SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } @Bean public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() { return protocolHandler -> { protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor()); }; }}
提供CRUD操作的UserRepository:
import org.springframework.data.repository.CrudRepository;import com.example.demo.User;public interface UserRepository extends CrudRepository<User, String> {}
提供API接口的UserController类:
@RestControllerpublic class UserController { @Autowired UserRepository userRepository; private SignatureAlgorithm sa = SignatureAlgorithm.HS256; private String jwtSecret = System.getenv("JWT_SECRET"); @GetMapping("/") public User handleRequest(@RequestHeader(HttpHeaders.AUTHORIZATION) String authHdr) { String jwtString = authHdr.replace("Bearer",""); Claims claims = Jwts.parser() .setSigningKey(jwtSecret.getBytes()) .parseClaimsJws(jwtString).getBody(); Optional<User> user = userRepository.findById((String)claims.get("email")); return user.get(); }}
application.properties配置文件:
server.port=3000spring.r2dbc.url=r2dbc:mysql://localhost:3306/testdbspring.r2dbc.username=dbserspring.r2dbc.password=dbpwd
User实体(这里DD也省略了构造函数、getter和setter):
public class User { @Id private String email; private String first; private String last; private String city; private String county; private int age; // 省略了构造函数、getter、setter }
应用主类:
@EnableWebFlux@SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }}
提供CRUD操作的UserRepository:
public interface UserRepository extends R2dbcRepository<User, String> {}
提供根据id查用户的业务类UserService:
@Servicepublic class UserService { @Autowired UserRepository userRepository; public Mono<User> findById(String id) { return userRepository.findById(id); }}
提供API接口的UserController类:
@RestController@RequestMapping("/")public class UserController { @Autowired UserService userService; private SignatureAlgorithm sa = SignatureAlgorithm.HS256; private String jwtSecret = System.getenv("JWT_SECRET"); @GetMapping("/") @ResponseStatus(HttpStatus.OK) public Mono<User> getUserById(@RequestHeader(HttpHeaders.AUTHORIZATION) String authHdr) { String jwtString = authHdr.replace("Bearer",""); Claims claims = Jwts.parser() .setSigningKey(jwtSecret.getBytes()) .parseClaimsJws(jwtString).getBody(); return userService.findById((String)claims.get("email")); }}
接下来是重头戏了,作者对两个技术方案都做了500w个请求的测试,评估的不同并发连接级别包含:50、100、300。
具体结果如下三张图:
50并发连接
100并发连接
300并发连接
最后,作者得出结论:Spring Boot Webflux要更优于带虚拟线程的Spring Boot。
似乎引入了虚拟线程还不如已经在用的Webflux?不知道大家是否有做过相关调研呢?如果有的话,欢迎在留言区一起聊聊~我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。
本文链接:http://www.28at.com/showinfo-26-60396-0.htmlSpring Boot虚拟线程的性能还不如Webflux?
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com