• Spring Boot配置Tomcat容器、Jetty容器、Undertow容器


    Tomcat、Jetty、Undertow

    Undertow、tomcat、jetty是Spring Boot 默认集成的三大容器。

    Tomcat概述

    Tomcat是Apache基金下的一个轻量级的Servlet容器,支持Servlet和JSP。Tomcat具有Web服务器特有的功能,包括 Tomcat管理和控制平台、安全局管理和Tomcat阀等。Tomcat本身包含了HTTP服务器,因此也可以视作单独的Web服务器。Tomcat是完全免费的,深受开发者的喜爱。

    Jetty概述

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行的Java应用提供网络和web连接。

    Undertow概述

    Undertow是Red Hat公司的开源产品, 它完全采用Java语言开发,是一款灵活的高性能Web服务器,支持阻塞IO和非阻塞IO。由于Undertow采用Java语言开发,可以直接嵌入到Java项目中使用。同时, Undertow完全支持Servlet和Web Socket,在高并发情况下表现非常出色。

    Spring Boot配置Tomcat容器

    GitHub地址:https://github.com/apache/tomcat

    Tomcat也是Spring Boot默认集成的三大容器之一。

    Tomcat是SpringBoot默认的容器技术,性能和内存使用方面都要弱于Jetty、Undertow。

    添加依赖

    Tomcat是SpringBoot默认的容器技术,故引入Web组件即可直接使用。

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4

    启动日志

    10:47:34.545  INFO 40500 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
    10:47:34.594  INFO 40500 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
    10:47:34.607  INFO 40500 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 5.125 seconds (JVM running for 6.656)
    
    • 1
    • 2
    • 3

    Spring Boot配置Undertow容器

    GitHub地址:https://github.com/undertow-io/undertow

    Undertow是Spring Boot默认集成的三大容器之一。

    Tomcat是SpringBoot默认的容器技术,同时SpringBoot也支持Undertow容器,而Undertow的性能和内存使用方面都优于Jetty、Tomcat。

    添加依赖

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-tomcatartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-undertowartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    启动日志

    10:54:25.222  INFO 31904 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
    10:54:25.253  INFO 31904 --- [  restartedMain] io.undertow                              : starting server: Undertow - 2.1.5.Final
    10:54:25.260  INFO 31904 --- [  restartedMain] org.xnio                                 : XNIO version 3.8.0.Final
    10:54:25.266  INFO 31904 --- [  restartedMain] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.0.Final
    10:54:25.311  INFO 31904 --- [  restartedMain] org.jboss.threads                        : JBoss Threads version 3.1.0.Final
    10:54:25.357  INFO 31904 --- [  restartedMain] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8888 (http)
    10:54:25.367  INFO 31904 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 4.878 seconds (JVM running for 6.32)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置

    Undertow的配置与使用Tomcat时配置类似 , 将Tomcat字段改为undertow即可

    # 日志存放目录
    server.undertow.accesslog.dir=
    
    # 是否启动日志
    server.undertow.accesslog.enabled=false 
    
    # 日志格式
    server.undertow.accesslog.pattern=common
    
    # 日志文件名前缀
    server.undertow.accesslog.prefix=access_log
    
    # 日志文件名后缀
    server.undertow.accesslog.suffix=log
    
    # HTTP POST请求最大的大小
    server.undertow.max-http-post-size=-1
    
    # 允许的最大 cookie 数量
    server.undertow.max-cookies=100
    
    # 允许的最大请求头数量
    server.undertow.max-header-size=100
    
    # 允许的最大请求行数量
    server.undertow.max-request-lines=100
    
    # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
    server.undertow.threads.io=4
    
    # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
    server.undertow.threads.worker=20
    
    # 每块buffer的空间大小,越小的空间被利用越充分,buffer会用于服务器连接的IO操作,类似netty的池化内存管理
    server.undertow.buffer-size=1024
    
    # 是否分配的直接内存
    server.undertow.direct-buffers=true
    
    # 是否启用SSL
    server.undertow.https.enabled=false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Spring Boot配置Jetty容器

    GitHub地址:https://github.com/eclipse/jetty.project

    Jetty也是Spring Boot默认集成的三大容器之一。

    Tomcat是SpringBoot默认的容器技术,同时SpringBoot也支持Jetty容器,而Jetty的性能和内存使用方面都优于Tomcat,弱于Undertow。

    添加依赖

           <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-tomcatartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jettyartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    启动日志

    注意:这里启动时间更短是由于更换了性能更高的电脑,Jetty不做过多测试。

    INFO 15196 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    INFO 15196 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
    INFO 15196 --- [  restartedMain] o.e.j.s.h.ContextHandler.application     : Initializing Spring DispatcherServlet 'dispatcherServlet'
    INFO 15196 --- [  restartedMain] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
    INFO 15196 --- [  restartedMain] o.e.jetty.server.AbstractConnector       : Started ServerConnector@60c0e4a0{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
    INFO 15196 --- [  restartedMain] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8080 (http/1.1) with context path '/'
    INFO 15196 --- [  restartedMain] com.example.demo.DemoApplication         : Started DemoApplication in 1.088 seconds (JVM running for 2.195)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Tomcat与Undertow容器性能对比

    使用JDK自带的jvisualvm.exe工具对比各项指标情况:

    Tomcat容器测试

    相同项目环境下启动,Tomcat 大约使用150M的堆内存以及大约31个线程数

    在这里插入图片描述

    闲置状态下,大概2分钟后堆内存与线程占用有所下降

    在这里插入图片描述

    当发送大量请求时,堆内存有明显增长。

    在这里插入图片描述

    Undertow容器测试

    相同项目环境下启动,Tomcat启动时大约使用150M的堆内存,然后很快降低到使用50MB的堆内存,以及大约24个线程数

    在这里插入图片描述

    当发送大量请求时,堆内存的占用增长不明显。

    在这里插入图片描述

    总结

    在并发量不高的情况下 , Tomcat与undertow的吞吐量区别不大 , 并发量高的情况下 , undertow的性能要优于Jetty与Tomcat。

  • 相关阅读:
    多种功能稻种资源 国稻种芯-何登骥:外源导入生物定向育种
    SSD上 NVIDIA Jetson Orin NANO系統如何刷
    【Python入门】Python入门必备思维导图,有方向的学习才是最实在的方法
    [小程序逆向]PC端VX小程序代码提取
    首次开通社交账号亲自招聘人才 周星驰都在关注的Web3 你知道是什么吗?
    linux三次握手、四次挥手
    【Hadoop】Apache Hadoop YARN
    SCI简介和写作顺序
    猿创征文|2022年前端之路——我的前端开发好帮手
    OCR——图像超分调研
  • 原文地址:https://blog.csdn.net/qq_38628046/article/details/125630156