SpringBoot-服务器Tomcat

目的

Java Web 目前最常用的三款服务器:

  • Undettow
  • Jetty
  • Tomcat

Tomcat 是Apache下的一款重量级的服务器,不用多说历史悠久,经得起实践的考验。
然而,现在当下微服务兴起,Spring boot、Spring cloud 越来越热的情况下,选择一款轻量级而性能优越的服务器是必要的选择。

Jetty 和Undertow 都是基于NIO 实现的高并发轻量级的服务器,支持servlet3.1 和websocket 。

压测对比

服务器 命中 成功率 吞吐量 平均耗时
Jetty 11488 100% 96.25 trans/sec 0.00sec
18393 100% 153.92 trans/sec 0.01sec
21484 99.99% 179.51 trans/sec 0.01sec
Undertow 11280 100% 94.02 trans/sec 0.00sec
19442 100% 163.35 trans/sec 0.01sec
23277 100% 195.54 tran/sec 0.01sec
Tomcat 10845 100% 90.95 trans/sec 0.02sec
21673 99.98% 181 trans/sec 0.01sec
25084 99.98% 209.10 trans/sec 0.01sec

从中可以看出在高负载下Undertow 的吞吐量高于Jetty 而且随着压力增大Jett 和Undertow 成功率差距会拉大。
而在负载不是太大情况下服务器处理能力差不多,Jetty 还略微高于Undertow ,而Tomcat 的负载能力似乎和Undertow 很接近。


Undertow

将Spring Boot 的内置服务器更换为Ungertow 。

  1. pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <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>
  2. application.properties

    1
    2
    3
    4
    5
    6
    # undertow
    server.port=8080
    server.undertow.buffer-size=1024
    server.undertow.io-threads=4
    server.undertow.worker-threads=20
    server.undertow.direct-buffers=true
  3. 启动日志

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    . ____ _ __ _ _
    /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    ' |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot :: (v2.1.7.RELEASE)

    2019-08-15 10:53:17.220 INFO 10336 --- [ main] v.s.SpringbootstarterdemotestApplication : Starting SpringbootstarterdemotestApplication on VgbhComputer with PID 10336 (F:\IDEWorkSpace\springbootstarterdemotest\target\classes started by Vgbh in F:\IDEWorkSpace\springbootstarterdemotest)
    2019-08-15 10:53:17.225 INFO 10336 --- [ main] v.s.SpringbootstarterdemotestApplication : No active profile set, falling back to default profiles: default
    2019-08-15 10:53:19.053 WARN 10336 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
    2019-08-15 10:53:19.105 INFO 10336 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
    2019-08-15 10:53:19.105 INFO 10336 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1721 ms
    2019-08-15 10:53:19.433 INFO 10336 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
    2019-08-15 10:53:19.659 INFO 10336 --- [ main] org.xnio : XNIO version 3.3.8.Final
    2019-08-15 10:53:19.687 INFO 10336 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
    2019-08-15 10:53:19.792 INFO 10336 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8080 (http) with context path ''
    2019-08-15 10:53:19.799 INFO 10336 --- [ main] v.s.SpringbootstarterdemotestApplication : Started SpringbootstarterdemotestApplication in 3.446 seconds (JVM running for 5.561)
    2019-08-15 10:53:37.319 INFO 10336 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2019-08-15 10:53:37.319 INFO 10336 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
    2019-08-15 10:53:37.326 INFO 10336 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms

    通过启动日志就可以看得出来,Undertow 比Tomcat 更加轻量,性能更好。


Jetty

将Spring Boot 的内置服务器更换为Ungertow 。

  1. pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <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>
  2. 启动日志

    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
      .   ____          _            __ _ _
    /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    ' |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot :: (v2.1.7.RELEASE)

    2019-08-15 13:19:03.516 INFO 13360 --- [ main] v.s.SpringbootstarterdemotestApplication : Starting SpringbootstarterdemotestApplication on VgbhComputer with PID 13360 (F:\IDEWorkSpace\springbootstarterdemotest\target\classes started by Vgbh in F:\IDEWorkSpace\springbootstarterdemotest)
    2019-08-15 13:19:03.521 INFO 13360 --- [ main] v.s.SpringbootstarterdemotestApplication : No active profile set, falling back to default profiles: default
    2019-08-15 13:19:04.758 INFO 13360 --- [ main] org.eclipse.jetty.util.log : Logging initialized @3184ms to org.eclipse.jetty.util.log.Slf4jLog
    2019-08-15 13:19:04.872 INFO 13360 --- [ main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8080
    2019-08-15 13:19:04.874 INFO 13360 --- [ main] org.eclipse.jetty.server.Server : jetty-9.4.19.v20190610; built: 2019-06-10T16:30:51.723Z; git: afcf563148970e98786327af5e07c261fda175d3; jvm 1.8.0_192-b12
    2019-08-15 13:19:04.903 INFO 13360 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
    2019-08-15 13:19:04.903 INFO 13360 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1296 ms
    2019-08-15 13:19:05.031 INFO 13360 --- [ main] org.eclipse.jetty.server.session : DefaultSessionIdManager workerName=node0
    2019-08-15 13:19:05.031 INFO 13360 --- [ main] org.eclipse.jetty.server.session : No SessionScavenger set, using defaults
    2019-08-15 13:19:05.032 INFO 13360 --- [ main] org.eclipse.jetty.server.session : node0 Scavenging every 660000ms
    2019-08-15 13:19:05.041 INFO 13360 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@517bd097{application,/,[file:///C:/Users/Vgbh/AppData/Local/Temp/jetty-docbase.2121540245403559071.8080/],AVAILABLE}
    2019-08-15 13:19:05.041 INFO 13360 --- [ main] org.eclipse.jetty.server.Server : Started @3468ms
    2019-08-15 13:19:05.221 INFO 13360 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
    2019-08-15 13:19:05.423 INFO 13360 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2019-08-15 13:19:05.423 INFO 13360 --- [ main] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
    2019-08-15 13:19:05.428 INFO 13360 --- [ main] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
    2019-08-15 13:19:05.450 INFO 13360 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@5b11a194{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
    2019-08-15 13:19:05.454 INFO 13360 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 8080 (http/1.1) with context path '/'
    2019-08-15 13:19:05.458 INFO 13360 --- [ main] v.s.SpringbootstarterdemotestApplication : Started SpringbootstarterdemotestApplication in 2.602 seconds (JVM running for 3.886)

    查看启动日志就可以看见服务器已经更换了。


参考资料


个人备注

此内容均为作者学习所做笔记!
转做其他用途必经作者同意,假冒转载必究!