配置之前,您需要知道的是,Tomcat, Jetty, Undertow 作為三大主流 Servelt 容器,Undertow 的性能要優(yōu)于前兩者。
所以,我們推薦您使用 Undertow 容器。接下來,就我們看看如何在 Spring Boot 中快捷地集成 Undertow。
一、添加 Maven 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除掉默認(rèn)支持的 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>
二、啟動項(xiàng)目
添加完上面的 maven 依賴后,Undertow 容器就已經(jīng)集成完畢了,接下來,讓我們啟動項(xiàng)目,看看控制臺輸出:
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.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)
2023-10-10 20:29:28.885 INFO 21908 --- [ main] s.e.s.SpringBootUndertowApplication : No active profile set, falling back to default profiles: default
2023-10-10 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
2023-10-10 20:29:34.478 INFO 21908 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
2023-10-10 20:29:34.478 INFO 21908 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5449 ms
2023-10-10 20:29:35.471 INFO 21908 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-10-10 20:29:36.423 INFO 21908 --- [ main] org.xnio : XNIO version 3.3.8.Final
2023-10-10 20:29:36.447 INFO 21908 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
2023-10-10 20:29:36.614 INFO 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.621 INFO 21908 --- [ main] s.e.s.SpringBootUndertowApplication : Started SpringBootUndertowApplication in 8.912 seconds (JVM running for 10.232)
2023-10-10 20:29:48.534 INFO 21908 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-10-10 20:29:48.534 INFO 21908 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-10-10 20:29:48.547 INFO 21908 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 12 ms
啟動成功,當(dāng)您看到?Undertow started on port(s) 8080 (http) with context path ''
?的行輸出時,說明此時正在使用的是 Undertow 容器,而非 Tomcat !
三、Undertow 相關(guān)配置
您可以針對 Undertow 容器做一些特定配置,如日志輸出路徑,設(shè)置工作者線程的個數(shù)等參數(shù)優(yōu)化等,如下所示:
# 是否打開 undertow 日志,默認(rèn)為 false
server.undertow.accesslog.enabled=false
# 設(shè)置訪問日志所在目錄
server.undertow.accesslog.dir=logs
# 指定工作者線程的 I/0 線程數(shù),默認(rèn)為 2 或者 CPU 的個數(shù)
server.undertow.io-threads=
# 指定工作者線程個數(shù),默認(rèn)為 I/O 線程個數(shù)的 8 倍
server.undertow.worker-threads=
# 設(shè)置 HTTP POST 內(nèi)容的最大長度,默認(rèn)不做限制
server.undertow.max-http-post-size=0
四、Tomcat Vs Undertow 容器性能對比
在文章的開始,我們提到過 Undertow 的性能要優(yōu)于 Tomcat, 但是口說無憑,需要拿出實(shí)際的證據(jù),新建一個 Web 項(xiàng)目,通過 JDK 自帶的工具對比一下各項(xiàng)指標(biāo)情況:
先看看 Tomcat:
可以看到,Tomcat 大約使用了 110M 的堆內(nèi)存以及大約 16 個線程數(shù)!
再來看看輕量級 Servlet 容器 Undertow 的指標(biāo):
Undertow 的內(nèi)存使用情況大約為 90M, 線程數(shù)大約 13 個線程的樣子。這還是在應(yīng)用不復(fù)雜的情況下,大型應(yīng)用出入會更大。文章來源:http://www.zghlxwxcb.cn/news/detail-711561.html
五、環(huán)境說明
上述 Demo 是基于?Spring Boot 2.1.2.RELEASE,需要注意一下 !文章來源地址http://www.zghlxwxcb.cn/news/detail-711561.html
到了這里,關(guān)于Spring Boot 配置 Undertow 容器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!