海哥 发表于 2023-11-5 13:24:42

Spring Boot 配置 Undertow 容器

https://img2023.cnblogs.com/blog/2583030/202310/2583030-20231017160846258-1379947874.png
配置之前,您需要知道的是,Tomcat, Jetty, Undertow 作为三大主流 Servelt 容器,Undertow 的性能要优于前两者。
所以,我们推荐您使用 Undertow 容器。接下来,就我们看看如何在 Spring Boot 中快捷地集成 Undertow。
一、添加 Maven 依赖

<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>二、启动项目

添加完上面的 maven 依赖后,Undertow 容器就已经集成完毕了,接下来,让我们启动项目,看看控制台输出:
Connected to the target VM, address: '127.0.0.1:50915', transport: 'socket'

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

2023-10-10 20:29:28.876INFO 21908 --- [         main] s.e.s.SpringBootUndertowApplication      : Starting SpringBootUndertowApplication on DESKTOP-RL6P6LA with PID 21908 (C:\dev\idea_workspace_personal\spring-boot-tutorial\spring-boot-undertow\target\classes started by allen in C:\dev\idea_workspace_personal\spring-boot-tutorial)
2023-10-10 20:29:28.885INFO 21908 --- [         main] s.e.s.SpringBootUndertowApplication      : No active profile set, falling back to default profiles: default
2023-10-10 20:29:34.388WARN 21908 --- [         main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2023-10-10 20:29:34.478INFO 21908 --- [         main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2023-10-10 20:29:34.478INFO 21908 --- [         main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5449 ms
2023-10-10 20:29:35.471INFO 21908 --- [         main] o.s.s.concurrent.ThreadPoolTaskExecutor: Initializing ExecutorService 'applicationTaskExecutor'
2023-10-10 20:29:36.423INFO 21908 --- [         main] org.xnio                                 : XNIO version 3.3.8.Final
2023-10-10 20:29:36.447INFO 21908 --- [         main] org.xnio.nio                           : XNIO NIO Implementation Version 3.3.8.Final
2023-10-10 20:29:36.614INFO 21908 --- [         main] o.s.b.w.e.u.UndertowServletWebServer   : Undertow started on port(s) 8080 (http) with context path ''
2023-10-10 20:29:36.621INFO 21908 --- [         main] s.e.s.SpringBootUndertowApplication      : Started SpringBootUndertowApplication in 8.912 seconds (JVM running for 10.232)
2023-10-10 20:29:48.534INFO 21908 --- io.undertow.servlet                      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-10-10 20:29:48.534INFO 21908 --- o.s.web.servlet.DispatcherServlet      : Initializing Servlet 'dispatcherServlet'
2023-10-10 20:29:48.547INFO 21908 --- o.s.web.servlet.DispatcherServlet      : Completed initialization in 12 ms启动成功,当您看到 Undertow started on port(s) 8080 (http) with context path '' 的行输出时,说明此时正在使用的是 Undertow 容器,而非 Tomcat !
https://img2023.cnblogs.com/blog/2583030/202310/2583030-20231017160855276-194362633.png
三、Undertow 相关配置

您可以针对 Undertow 容器做一些特定配置,如日志输出路径,设置工作者线程的个数等参数优化等,如下所示:
# 是否打开 undertow 日志,默认为 false
server.undertow.accesslog.enabled=false
# 设置访问日志所在目录
server.undertow.accesslog.dir=logs
# 指定工作者线程的 I/0 线程数,默认为 2 或者 CPU 的个数
server.undertow.io-threads=
# 指定工作者线程个数,默认为 I/O 线程个数的 8 倍
server.undertow.worker-threads=
# 设置 HTTP POST 内容的最大长度,默认不做限制
server.undertow.max-http-post-size=0四、Tomcat Vs Undertow 容器性能对比

在文章的开始,我们提到过 Undertow 的性能要优于 Tomcat, 但是口说无凭,需要拿出实际的证据,新建一个 Web 项目,通过 JDK 自带的工具对比一下各项指标情况:
先看看 Tomcat:
https://img2023.cnblogs.com/blog/2583030/202310/2583030-20231017161051628-1577633750.png
可以看到,Tomcat 大约使用了 110M 的堆内存以及大约 16 个线程数!
再来看看轻量级 Servlet 容器 Undertow 的指标:
https://img2023.cnblogs.com/blog/2583030/202310/2583030-20231017161100763-316171677.png
Undertow 的内存使用情况大约为 90M, 线程数大约 13 个线程的样子。这还是在应用不复杂的情况下,大型应用出入会更大。
五、环境说明

上述 Demo 是基于 Spring Boot 2.1.2.RELEASE,需要注意一下 !

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Spring Boot 配置 Undertow 容器