springboot切換使用undertow容器的方式
springboot切換使用undertow容器
maven引入jar
<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>
undertow的基本配置
#undertow容器配置開始 # 是否打開 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.threads.io=8 # 指定工作者線程個數(shù),默認(rèn)為 I/O 線程個數(shù)的 8 倍 server.undertow.threads.worker=256 # 設(shè)置 HTTP POST 內(nèi)容的最大長度,默認(rèn)不做限制 server.undertow.max-http-post-size=4MB # 以下的配置會影響buffer,這些buffer會用于服務(wù)器連接的IO操作,有點類似netty的池化內(nèi)存管理; server.undertow.buffer-size=1024 # 是否分配的直接內(nèi)存(NIO直接分配的堆外內(nèi)存) server.undertow.direct-buffers=true #undertow容器配置結(jié)束
其他配置可以先看springboot的autoconfig配置類這塊的配置:
org.springframework.boot.autoconfigure.web包下的ServerProperties、servlet、embedded的undertowxxx類
一個特別的報錯警告
解決使用undertow容器報io.undertow.websockets.jsr -
UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
處理:
新增一個component注解的類,具體如下:
@Component
public class UndertowPoolCustomizer implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
? ? @Override
? ? public void customize(UndertowServletWebServerFactory factory) {
? ? ? ? factory.addDeploymentInfoCustomizers(deploymentInfo -> {
? ? ? ? ? ? WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
? ? ? ? ? ? webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024));
? ? ? ? ? ? deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
? ? ? ? });
? ? }
}驗證成功

看到Undertow started xxx就是使用undertow容器啟動成功了。
分享感覺
網(wǎng)傳undertow比tomcat、jetty都快省資源,還是費阻塞nio等等,實際上可能就沒有什么感覺。
我其實用postman測試了以前的一些接口,感覺接口返回秒回,就是感覺快了。
后來運行2天(沒有配置undertow,默認(rèn)配置)有點小卡,然后,早上把配置改成上面的發(fā)布,再觀察幾天試試。
springboot替換默認(rèn)容器
undertow簡介
Undertow 是紅帽公司開發(fā)的一款基于 NIO 的高性能 Web 嵌入式服務(wù)器
Undertow 被設(shè)計成為完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中
性能比對
使用jmeter進行壓測比較
tomcat壓測結(jié)果


將tomcat容器換成jetty容器進行測試

將jetty容器修改為undertow


從吞吐量看undertow要強于前兩個
項目中使用undertow 1.引入依賴
在官網(wǎng)上可以看到undertow主要有兩個版本
2.1
The current stable Servlet 4.0 branch, requires JDK8 or above
1.4
The current stable Servlet 3.1 branch, supports JDK7
可以根據(jù)自己的servlet和jdk版本進行選擇,我們這里使用2.1版本
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>2.1.0.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>2.1.0.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-websockets-jsr</artifactId>
<version>2.1.0.Final</version>
</dependency>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot~nexus項目打包要注意的地方示例代碼詳解
這篇文章主要介紹了springboot~nexus項目打包要注意的地方,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Java實現(xiàn)學(xué)生管理系統(tǒng)詳解流程
這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
Spring類型轉(zhuǎn)換 ConversionSerivce Convertor解析
這篇文章主要介紹了Spring類型轉(zhuǎn)換 ConversionSerivce Convertor的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法
這篇文章主要介紹了Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法,是一個非常實用的技巧,需要的朋友可以參考下2014-09-09
Spring Cloud 負載均衡器 Ribbon原理及實現(xiàn)
這篇文章主要介紹了Spring Cloud 負載均衡器 Ribbon原理及實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

