来源:程序猿dd
早上看到一篇关于spring boot虚拟线程和webflux性能对比的文章,觉得还不错。内容较长,抓重点给大家介绍一下这篇文章的核心内容,方便大家快速阅读。
测试场景
作者采用了一个尽可能贴近现实操作的场景:
从授权头信息中提取jwt
验证jwt并从中提取用户的email
使用用户的email去mysql里执行查询
返回用户记录
测试技术
这里要对比的两个核心技术点是:
带有虚拟线程的spring boot:这不是一个跑在传统物理线程上的spring boot应用,而是跑在虚拟线程上的。这些轻量级线程简化了开发、维护和调试高吞吐量并发应用程序的复杂任务。虽然虚拟线程仍然在底层操作系统线程上运行,但它们带来了显着的效率改进。当虚拟线程遇到阻塞 i/o 操作时,java 运行时会暂时挂起它,从而释放关联的操作系统线程来为其他虚拟线程提供服务。这个优雅的解决方案优化了资源分配并增强了整体应用程序响应能力。
spring boot webflux:spring boot webflux是spring生态系统中的反应式编程框架,它利用project reactor库来实现非阻塞、事件驱动的编程。所以,它特别适合需要高并发和低延迟的应用程序。依靠反应式方法,它允许开发人员有效地处理大量并发请求,同时仍然提供与各种数据源和通信协议集成的灵活性。
不论是webflux还是虚拟线程,这两个都是为了提供程序的高并发能力而生,那么谁更胜一筹呢?下面一起看看具体的测试。
测试环境
运行环境与工具
一台16g内存的macbook pro m1
java 20
spring boot 3.1.3
启用预览模式,以获得虚拟线程的强大能力
依赖的第三方库:jjwt、mysql-connector-java
测试工具:bombardier
数据库:mysql
数据准备
在bombardier中准备100000个jwt列表,用来从中随机选取jwt,并将其放入http请求的授权信息中。
在mysql中创建一个users表,表结构如下:
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)
为users表准备100000条用户数据
测试代码
带虚拟线程的spring boot程序
application.properties配置文件:
server.port=3000spring.datasource.url= jdbc//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 {}
提供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 = userrepository.findbyid((string)claims.get(email)); return user.get(); }}
spring boot webflux程序
application.properties配置文件:
server.port=3000spring.r2dbc.url=r2dbc//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 {}
提供根据id查用户的业务类userservice:
@servicepublic class userservice { @autowired userrepository userrepository; public mono 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 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?不知道大家是否有做过相关调研呢?如果有的话,欢迎在留言区一起聊聊~
利用非常简单的工艺步骤和廉价的材料,设计出了一种薄电容式传感器
目前全球最先进的类人机器人已经诞生
瑞为技术为企业高质量发展保驾护航
集成灶厨房智能化语音芯片方案选型推荐 WT588F02B-8S工业级语音芯片
FPC失效分析,PCB应力应变测试标准!
Spring Boot虚拟线程和Webflux性能对比
怎样探索区块链的价值
LED灯具做CB认证有哪些要求和标准
印刷线路板及其加工
英特尔:智能感知计算与场景设计的跨界创新
纳芯微推出全新高性能、低成本、集成隔离电源的数字隔离器NIRSP31
光网络将成为数字经济的基石,成为运营商和千行百业的增长引擎
如何减少航空业的碳排放
芯驰G9芯片获2022中国汽车供应链优秀创新成果
CES 2019关注的8K电视新品大盘点 8K一定是未来
2020年全球智能手机产量同比下降11%
工控机安装时出现的故障及注意事项
雷蛇笔记本水银版,除了更薄更快更时尚,还有什么?
再见了!微软宣布停服Windows Phone 8.1系统
限幅器原理是什么?