本节中, 您将学习如何在 Spring Boot 中配置 Undertow 容器. 配置之前, 您需要知道的是, Tomcat, Jetty, Undertow 作为三大主流 Servelt 容器, Undertow 的性能要优于前两者.
所以, 我们推荐您使用 Undertow 容器. 接下来, 就我们看看如何在 Spring Boot 中快捷地集成 Undertow.
一, 添加 Maven 依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <!-- 移除掉默认支持的 Tomcat -->
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- 添加 Undertow 容器 -->
- <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)
- 2019-02-22 20:29:28.876 INFO 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)
- 2019-02-22 20:29:28.885 INFO 21908 --- [ main] s.e.s.SpringBootUndertowApplication : No active profile set, falling back to default profiles: default
- 2019-02-22 20:29:34.388 WARN 21908 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
- 2019-02-22 20:29:34.478 INFO 21908 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
- 2019-02-22 20:29:34.478 INFO 21908 --- [ main] o.s.Web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5449 ms
- 2019-02-22 20:29:35.471 INFO 21908 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
- 2019-02-22 20:29:36.423 INFO 21908 --- [ main] org.xnio : XNIO version 3.3.8.Final
- 2019-02-22 20:29:36.447 INFO 21908 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
- 2019-02-22 20:29:36.614 INFO 21908 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8080 (http) with context path ''
- 2019-02-22 20:29:36.621 INFO 21908 --- [ main] s.e.s.SpringBootUndertowApplication : Started SpringBootUndertowApplication in 8.912 seconds (JVM running for 10.232)
- 2019-02-22 20:29:48.534 INFO 21908 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
- 2019-02-22 20:29:48.534 INFO 21908 --- [ XNIO-1 task-1] o.s.Web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
- 2019-02-22 20:29:48.547 INFO 21908 --- [ XNIO-1 task-1] o.s.Web.servlet.DispatcherServlet : Completed initialization in 12 ms
启动成功, 当您看到 Undertow started on port(s) 8080 (http) with context path '' 的行输出时, 说明此时正在使用的是 Undertow 容器, 而非 Tomcat !
三, 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:
可以看到, Tomcat 大约使用了 110M 的堆内存以及大约 16 个线程数!
再来看看轻量级 Servlet 容器 Undertow 的指标:
Undertow 的内存使用情况大约为 90M, 线程数大约 13 个线程的样子. 这还是在应用不复杂的情况下, 大型应用出入会更大.
五, 环境说明
上述 Demo 是基于 Spring Boot 2.1.2.RELEASE, 需要注意一下 !
六, GitHub 源码
来源: https://www.cnblogs.com/quanxiaoha/p/10433092.html