spring boot内置容器性能比较(Jetty、Tomcat、Undertow)

本文对比了SpringBoot中Jetty、Tomcat和Undertow三种内置容器的性能,通过使用siege和ab工具进行压力测试,详细记录了不同条件下的吞吐量、平均响应时间和线程使用情况。

spring boot内置容器性能比较(Jetty、Tomcat、Undertow)

一、准备工作

1.1 服务器环境

名称配置
服务器操作系统Ubuntu 18.04.2 LTS
内存15.4 GiB
处理器Intel® Core™ i7-7560U CPU @ 2.40GHz × 4
磁盘SSD 177.2 GB

1.2 创建服务

新建spring boot项目

  1. jetty
  2. tomcat
  3. undertow

1.3 安装相关性能测试工具

安装 visualvm

sudo apt-get install visualvm

安装visualgc(visualvm的插件)安装visualgc
可在VisualVM的Tools->Plugins->AvailablePlugins里边找到Visual GC插件进行安装
重启后可见到相关tab

安装 ab(ApacheBench)测试工具

sudo apt-get install apache2-utils

安装 siege 高性能压测工具

sudo apt-get install siege 

二、服务设置

这边每个服务具体初始化配置如下

2.1 tomcat

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.2 jetty

<dependencies>
    <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>
</dependencies>

2.3 undertow

<dependencies>
    <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-undertow</artifactId>
    </dependency>
</dependencies>

2.4 每个服务rest服务配置如下

package com.songyaxu.container.jetty.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;

/**
 * @Author: yaxuSong
 * @Description:
 * @Date: 19-8-8 上午11:41
 * @MOdified by:
 **/
@RestController
@RequestMapping("test")
public class TestController {


    @Value("${server.name}")
    private String SERVER_NAME;

    /**
     * 未使用HTTP异步的接口
     */
    @GetMapping("/block")
    public String block() {
        return SERVER_NAME + " block!!!";
    }

    /**
     * 使用HTTP异步的接口
     */
    @GetMapping("/async")
    public WebAsyncTask<String> async() {
        return new WebAsyncTask(() -> SERVER_NAME + " async!!!");
    }
}


三、进行测试

3.1 第一轮简单的测试

使用seige进行的http同步测试

访问情况统计

服务器条件命中成功率吞吐量平均耗时
tomcat-c 50 -t 2197626100%1656.41 trans/sec0.03
tomcat-c 100 -t 2201692100%1690.35 trans/sec0.06
tomcat-c 200 -t 2240596100%2010.66 trans/sec0.10
jetty-c 50 -t 2282701100%2364.51 trans/sec0.03
jetty-c 100 -t 2319180100%2674.10 trans/sec0.04
jetty-c 200 -t 2224428100%1882.79 trans/sec0.11
undertow-c 50 -t 2260263100%2176.11 trans/sec0.02
undertow-c 100 -t 2349105100%2925.54 trans/sec0.03
undertow-c 200 -t 2358812100%3006.38 trans/sec0.07

开启线程统计

服务器条件存活线程数守护线程数累计开启线程数
tomcat初始292542
tomcat-c 50 -t 29389111
tomcat-c 100 -t 2149145365
tomcat-c 200 -t 2196192532
jetty初始221235
jetty-c 50 -t 213211147
jetty-c 100 -t 217311195
jetty-c 200 -t 221512239
undertow初始181232
undertow-c 50 -t 2501265
undertow-c 100 -t 2501265
undertow-c 200 -t 2501267

cpu和内存统计

服务器条件CPU占用率内存占用率最高cpu占用率
tomcat初始1.0%1.8%2.3%
tomcat-c 50 -t 256.1%3.0%250.3%
tomcat-c 100 -t 253.1%3.7%211.3%
tomcat-c 200 -t 253.6%2.8%222.7%
jetty初始0.3%1.5%1.5%
jetty-c 50 -t 267.2%5.7%204.0%
jetty-c 100 -t 255.9%5.2%211.7%
jetty-c 200 -t 254.7%4.2%210.3%
undertow初始0.3%1.5%1.5%
undertow-c 50 -t 264.3%4.7%190.1%
undertow-c 100 -t 250.9%4.4%197.3%
undertow-c 200 -t 251.5%3.9%198.3%
使用seige进行的http异步测试

访问情况统计

服务器条件命中成功率吞吐量平均耗时
tomcat-c 50 -t 2128260100%1076.28 trans/sec0.05
tomcat-c 100 -t 2145973100%1218.47 trans/sec0.08
tomcat-c 200 -t 2161967100%1356.62 trans/sec0.15
jetty-c 50 -t 2173843100%1459.88 trans/sec0.03
jetty-c 100 -t 2189112100%1586.51 trans/sec0.06
jetty-c 200 -t 2202334100%1699.14 trans/sec0.12
undertow-c 50 -t 2201798100%1684.60 trans/sec0.03
undertow-c 100 -t 2217834100%1819.53 trans/sec0.05
undertow-c 200 -t 2248644100%2081.40 trans/sec0.10

开启线程统计

服务器条件存活线程数守护线程数累计开启线程数
tomcat初始332943
tomcat-c 50 -t 210290116
tomcat-c 100 -t 2160148251
tomcat-c 200 -t 2226214450
jetty初始271737
jetty-c 50 -t 212211148
jetty-c 100 -t 218212208
jetty-c 200 -t 222312260
undertow初始201431
undertow-c 50 -t 2491271
undertow-c 100 -t 2571180
undertow-c 200 -t 2491288

cpu和内存统计

服务器条件CPU占用率内存占用率最高cpu占用率
tomcat初始0.3%1.5%1.5%
tomcat-c 50 -t 276.1%6.9%231.0%
tomcat-c 100 -t 260.3%8.3%231.3%
tomcat-c 200 -t 258.1%9.0%230.2%
jetty初始1.0%1.2%1.3%
jetty-c 50 -t 275.1%3.7%227.3%
jetty-c 100 -t 259.9%3.1%228.7%
jetty-c 200 -t 260.3%3.1%226.1%
undertow初始0.7%1.5%1.5%
undertow-c 50 -t 266.4%3.5%213.7%
undertow-c 100 -t 256.9%2.9%219.3%
undertow-c 200 -t 255.0%2.5%215.7%

一些对比的图片

-tomcat-async-moitor
-tomcat-async-gc
-jetty-async-monitor
-jetty-async-gc
-undertow-async-monitor
-undertow-async-gc

3.2 第二轮简单的测试

使用ab进行的http同步测试

访问情况统计

服务器条件成功率吞吐量平均耗时总消耗时间
tomcat-n 20000 -c 50100%10739.60 [#/sec]0.0931.862
tomcat-n 20000 -c 100100%11437.54 [#/sec]0.0871.749
tomcat-n 20000 -c 200100%12582.45 [#/sec]0.0791.590
jetty-n 20000 -c 50100%8546.79 [#/sec]0.1172.340
jetty-n 20000 -c 100100%10445.64 [#/sec]0.0961.915
jetty-n 20000 -c 200100%10777.76 [#/sec]0.0931.856
undertow-n 20000 -c 50100%8801.86 [#/sec]0.1142.272
undertow-n 20000 -c 100100%11803.92 [#/sec]0.0851.694
undertow-n 20000 -c 200100%12604.54 [#/sec]0.0791.587

开启线程统计

服务器条件存活线程数守护线程数累计开启线程数
tomcat-n 20000 -c 50858195
tomcat-n 20000 -c 100122118189
tomcat-n 20000 -c 200139135300
jetty-n 20000 -c 5012912145
jetty-n 20000 -c 10014012158
jetty-n 20000 -c 20021411233
undertow-n 20000 -c 50521463
undertow-n 20000 -c 100501263
undertow-n 20000 -c 200501263

cpu和内存统计

服务器条件CPU占用率内存占用率
tomcat-n 20000 -c 5059.8%2.7%
tomcat-n 20000 -c 10058.2%3.5%
tomcat-n 20000 -c 20053.2%3.7%
jetty-n 20000 -c 5058.7%1.7%
jetty-n 20000 -c 10058.6%2.2%
jetty-n 20000 -c 20064.1%2.3%
undertow-n 20000 -c 5059.2%2.3%
undertow-n 20000 -c 10064.3%2.7%
undertow-n 20000 -c 20057.9%3.6%
使用ab进行的http异步测试

访问情况统计

服务器条件成功率吞吐量平均耗时总消耗时间
tomcat-n 20000 -c 50100%5870.13 [#/sec]0.1703.407
tomcat-n 20000 -c 100100%8311.58 [#/sec]0.1201.749
tomcat-n 20000 -c 200100%8774.37 [#/sec]0.1142.279
jetty-n 20000 -c 50100%6519.73 [#/sec]0.1533.068
jetty-n 20000 -c 100100%8270.94 [#/sec]0.1212.418
jetty-n 20000 -c 200100%8530.12 [#/sec]0.1172.345
undertow-n 20000 -c 50100%6659.26 [#/sec]0.1503.003
undertow-n 20000 -c 100100%9329.74 [#/sec]0.1072.144
undertow-n 20000 -c 200100%9856.14 [#/sec]0.1012.029

开启线程统计

服务器条件存活线程数守护线程数累计开启线程数
tomcat-n 20000 -c 50897799
tomcat-n 20000 -c 100140128210
tomcat-n 20000 -c 200183195376
jetty-n 20000 -c 5013116142
jetty-n 20000 -c 10015312177
jetty-n 20000 -c 20018412226
undertow-n 20000 -c 50611773
undertow-n 20000 -c 100581281
undertow-n 20000 -c 200581289

cpu和内存统计

服务器条件CPU占用率内存占用率
tomcat-n 20000 -c 5071.8%2.9%
tomcat-n 20000 -c 10065.2%4.3%
tomcat-n 20000 -c 20062.1%5.5%
jetty-n 20000 -c 5067.8%2.5%
jetty-n 20000 -c 10064.2%3.2%
jetty-n 20000 -c 20065.9%3.6%
undertow-n 20000 -c 5067.5%2.4%
undertow-n 20000 -c 10063.3%2.7%
undertow-n 20000 -c 20055.2%3.1%
<think> 我们之前讨论的是Spring Boot的配置文件和多环境配置,现在用户转向了Spring Boot内置Web服务器的主题。根据用户的问题,他想要了解Spring Boot内置TomcatJetty等服务器的相关信息。同时,用户提供了三个引用,但这些引用主要涉及部署到外部服务器和内嵌式服务器的使用场景对比,与内置服务器本身的相关性不是直接的,但我们可以参考其中的一些信息。 用户的问题:我想了解Spring Boot可以内置TomcatJetty等服务器的相关信息。 回答需要包括: 1. 内置服务器的概念:Spring Boot默认使用嵌入式服务器(如TomcatJettyUndertow),这意味着这些服务器被打包在应用中,应用可以独立运行(无需外部Web容器)。 2. 默认服务器:Spring Boot的starter-web默认使用Tomcat,但可以通过排除Tomcat依赖并引入其他服务器(如JettyUndertow)来切换。 3. 如何切换服务器:给出具体的依赖配置示例。 4. 各服务器的特点:根据引用[3]提到的Jetty的优势(轻量级、启动速度快、适合微服务和WebSocket),可以对比TomcatJettyUndertow的优缺点。 5. 嵌入式服务器的应用场景:适合微服务、独立部署的应用,而传统的WAR包部署到外部服务器(如引用[1])则适合需要集中管理服务器环境的场景。 回答结构: - 首先介绍内置服务器的概念和优势。 - 然后说明默认配置以及如何切换。 - 接着对比不同嵌入式服务器的特点和适用场景。 - 最后总结如何选择。 注意:用户提供的引用中,引用[2]提到了Spring Boot使用内嵌式TomcatJetty,并提到启动嵌入式容器就是启动这些组件。我们可以据此解释内置服务器的工作原理。 同时,回答末尾按照要求生成相关问题。 由于用户的问题集中在“介绍”,因此不需要涉及部署到外部服务器的详细步骤(引用[1]),但可以简要对比内置服务器和外部部署的适用场景(如引用[3]中提到的Jetty的优势)。 具体回答如下: </think> ### Spring Boot内置TomcatJetty服务器详解 Spring Boot通过**嵌入式Servlet容器**技术,将TomcatJetty等主流Web服务器直接集成到应用中,实现开箱即用的独立部署能力。以下是核心工作机制: #### 一、嵌入式容器原理 1. **组件化设计**: Tomcat/Jetty采用模块化架构(如Connector、Engine、Host等组件)[^2]。 Spring Boot在启动时自动初始化这些组件: ```java @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); // 隐含启动Tomcat/Jetty } } ``` 2. **依赖控制**: - 默认嵌入Tomcat: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 默认包含Tomcat --> </dependency> ``` - 切换为Jetty: ```xml <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> ``` #### 二、三大嵌入式容器对比 | 特性 | Tomcat | Jetty | Undertow | |---------------------|----------------------------|----------------------------|----------------------------| | **启动速度** | 中等 | ️ **最快** (轻量级设计) [^3] | 快 | | **内存占用** | 较高 | ✅ **最低** | 低 | | **长连接优化** | 常规 | ✅ **优秀** (WebSocket场景) [^3] | 优秀 | | **适用场景** | 传统Web应用 | 微服务/实时通信 | 高并发API服务 | #### 三、容器配置方式 通过`application.yml`自定义容器参数: ```yaml server: port: 8080 # 监听端口 tomcat: # Tomcat专属配置 max-threads: 200 # 最大线程数 connection-timeout: 5000 jetty: # Jetty专属配置 max-http-post-size: 2MB thread-pool: max: 300 ``` #### 四、应用场景建议 1. **选择Tomcat**: - 需要Servlet/JSP支持 - 传统企业级应用 2. **选择Jetty**: - 微服务架构(快速启动/低内存)[^3] - WebSocket/长连接场景(如实时推送) 3. **选择Undertow**: - 高并发API服务(非阻塞IO性能强) > **最佳实践**:生产环境推荐使用外部容器部署(如独立Tomcat集群),但嵌入式容器在开发/测试/微服务场景更具效率优势[^1][^3]。 --- ### 总结 Spring Boot的嵌入式容器设计实现了**应用与服务器的解耦**: 1. 开发阶段:使用内置容器快速迭代 2. 生产环境:可通过`mvn package`生成WAR包部署到外部Tomcat/Jetty[^1] 3. 容器切换:仅需修改依赖,无需代码调整
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风华正茂少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值