springboot集成tomcat, jetty, undertow服务器吞吐量对比
一,undertow介绍
undertow简介:
Undertow是RedHAT红帽公司开源的产品,采用java开发,是一款灵活,高性能的web服务器,提供了NIO的阻塞/非阻塞APIS,也是Wildfly的默认Web容器。
在javaweb容器的世界里,tomcat和jetty是大众熟知的,undertow目前逐步进入大众的视角,它是一款能和tomcat媲美的神器,在性能方面吊打tomcat。
目前Undertow已经成为springboot 默认集成的三大容器之一。
undertow特点
Undertow在高并发业务场景中,性能优于tomcat
1,高性能在多款同类产品的压测种,在高并发情况下表现出色。
2,Servlet4.0支持,它提供了对Servlet4.0的支持。
3,Web Socket完全支持,包含JSR-356,用以满足Web应用巨大数量的客户端。
4,内嵌式,它不需要容器,只需要通过api即可快速搭建Web服务器。
5,灵活性交由链式Handler配置和处理请求,可以最小化按需加载模块,无须加载多余功能。
6,轻量级,它是一个内嵌Web服务器,由两个核心jar包组成。
springboot集成的web服务器版本
2.2.13.RELEASE springboot版本默认支持的三种Servlet容器版本如下
2.2.13.RELEASE springboot版本(官网提供的) | |
Web服务器 | 集成版本 |
Undertow | 2.0.33.Final |
tomcat | 9.0.41 |
jetty | 4.1.17 |
二,实验过程
1,springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2,测试类的controller
@RequestMapping("/reflect")
@ResponseBody
public void reflect() {
String body = "hello in body";
User user = new User();
user.setAge(10L);
user.setId(10L);
user.setName("name");
user.setSchool("school");
user.setUid("uid");
Object[] params = {user,body};
long begin = System.currentTimeMillis();
Object object = springReflectionUtil.springInvokeMethod("reflectService", "handleUser", params);
if (object instanceof User) {
User invoke = (User)object;
System.out.println("返回结果============"+invoke.toString());
}
long end = System.currentTimeMillis();
System.out.println("game over cost="+(end-begin));
}
3,springboot默认就内嵌了tomcat
4,项目整合undertow
在pom文件中引入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
5,项目整合jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
6,jvm配置
-javaagent:D:\apache-skywalking-apm-es7-8.4.0\apache-skywalking-apm-bin-es7\agent\skywalking-agent.jar
-Dskywalking.agent.service_name=my-service
-Dskywalking.collector.backend_service=127.0.0.1:11800
-Djava.net.preferIPv4Stack=true
-server
-Xms4096m
-Xmx4096m
-XX:+UseG1GC
7,用jmeter压测
100个线程 循环1000次
1000个线程 循环100次
8,实验结果
tomcat
100线程循环1000次
1000线程循环100次
jetty
100线程循环1000次
1000线程循环100次
undertow
100线程循环1000次
1000线程循环100次
通过三张压测结果图统计如下
100线程循环1000次(吞吐量/sec) | ||||
第一次 | 第二次 | 第三次 | 平均值 | |
Tomcat | 1119.7 | 1497.5 | 1490.5 | 1369.2 |
jetty | 1363.7 | 1429.3 | 1390.5 | 1394.5 |
undertow | 1607.9 | 1467.3 | 1545.6 | 1540.3 |
1000线程循环100次(吞吐量/sec) | ||||
第一次 | 第二次 | 第三次 | 平均值 | |
Tomcat | 1212.2 | 1088.4 | 1107.5 | 1136.0 |
jetty | 1225.9 | 1222.6 | 1186.3 | 1211.6 |
undertow | 1383.7 | 1468.1 | 1434.2 | 1428.7 |
三,实验结论:
100个线程 循环1000次,都用默认配置,没有调配置参数情况下
tomcat的吞吐量平均是每秒1369.2,jetty的吞吐量是1394.5,undertow的吞吐量是每秒1540.3
1000个线程 循环100次,都用默认配置,没有调配置参数情况下
tomcat的吞吐量平均是每秒1136.0,jetty的吞吐量是1211.6,undertow的吞吐量是每秒1428.7
可以看出,默认配置情况下,undertow的吞吐量领先于jetty和tomcat.,undertow>jetty>tomcat