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 的個(gè)數(shù) server.undertow.threads.io=8 # 指定工作者線程個(gè)數(shù),默認(rèn)為 I/O 線程個(gè)數(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操作,有點(diǎn)類似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類
一個(gè)特別的報(bào)錯(cuò)警告
解決使用undertow容器報(bào)io.undertow.websockets.jsr -
UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
處理:
新增一個(gè)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); ? ? ? ? }); ? ? } }
驗(yàn)證成功
看到Undertow started xxx就是使用undertow容器啟動成功了。
分享感覺
網(wǎng)傳undertow比tomcat、jetty都快省資源,還是費(fèi)阻塞nio等等,實(shí)際上可能就沒有什么感覺。
我其實(shí)用postman測試了以前的一些接口,感覺接口返回秒回,就是感覺快了。
后來運(yùn)行2天(沒有配置undertow,默認(rèn)配置)有點(diǎn)小卡,然后,早上把配置改成上面的發(fā)布,再觀察幾天試試。
springboot替換默認(rèn)容器
undertow簡介
Undertow 是紅帽公司開發(fā)的一款基于 NIO 的高性能 Web 嵌入式服務(wù)器
Undertow 被設(shè)計(jì)成為完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中
性能比對
使用jmeter進(jìn)行壓測比較
tomcat壓測結(jié)果
將tomcat容器換成jetty容器進(jìn)行測試
將jetty容器修改為undertow
從吞吐量看undertow要強(qiáng)于前兩個(gè)
項(xiàng)目中使用undertow 1.引入依賴
在官網(wǎng)上可以看到undertow主要有兩個(gè)版本
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版本進(jìn)行選擇,我們這里使用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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot~nexus項(xiàng)目打包要注意的地方示例代碼詳解
這篇文章主要介紹了springboot~nexus項(xiàng)目打包要注意的地方,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java實(shí)現(xiàn)SHA1加密代碼實(shí)例
這篇文章給大家分享了Java實(shí)現(xiàn)SHA1加密的相關(guān)實(shí)例代碼,有興趣的朋友可以測試參考下。2018-07-07Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Spring類型轉(zhuǎn)換 ConversionSerivce Convertor解析
這篇文章主要介紹了Spring類型轉(zhuǎn)換 ConversionSerivce Convertor的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11java連接mongoDB并進(jìn)行增刪改查操作實(shí)例詳解
這篇文章主要介紹了java連接mongoDB并進(jìn)行增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了java環(huán)境下MongoDB擴(kuò)展包的下載、安裝及操作MongoDB連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法
這篇文章主要介紹了Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法,是一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09Spring Cloud 負(fù)載均衡器 Ribbon原理及實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 負(fù)載均衡器 Ribbon原理及實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03