springboot切換使用undertow容器的方式
springboot切換使用undertow容器
maven引入jar
<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>
undertow的基本配置
#undertow容器配置開始 # 是否打開 undertow 日志,默認為 false server.undertow.accesslog.enabled=false # 設置訪問日志所在目錄 server.undertow.accesslog.dir=logs # 指定工作者線程的 I/0 線程數(shù),默認為 2 或者 CPU 的個數(shù) server.undertow.threads.io=8 # 指定工作者線程個數(shù),默認為 I/O 線程個數(shù)的 8 倍 server.undertow.threads.worker=256 # 設置 HTTP POST 內(nèi)容的最大長度,默認不做限制 server.undertow.max-http-post-size=4MB # 以下的配置會影響buffer,這些buffer會用于服務器連接的IO操作,有點類似netty的池化內(nèi)存管理; server.undertow.buffer-size=1024 # 是否分配的直接內(nèi)存(NIO直接分配的堆外內(nèi)存) server.undertow.direct-buffers=true #undertow容器配置結束
其他配置可以先看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,默認配置)有點小卡,然后,早上把配置改成上面的發(fā)布,再觀察幾天試試。
springboot替換默認容器
undertow簡介
Undertow 是紅帽公司開發(fā)的一款基于 NIO 的高性能 Web 嵌入式服務器
Undertow 被設計成為完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中
性能比對
使用jmeter進行壓測比較
tomcat壓測結果


將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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Cloud Feign接口返回流的實現(xiàn)
這篇文章主要介紹了Spring Cloud Feign接口返回流的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
必知必會的SpringBoot實現(xiàn)熱部署兩種方式
這篇文章主要為大家介紹了必知必會的SpringBoot實現(xiàn)熱部署兩種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
SpringBoot實現(xiàn)簡單的登錄注冊的項目實戰(zhàn)
本文主要介紹了SpringBoot實現(xiàn)簡單的登錄注冊的項目實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字數(shù)量的方法
這篇文章主要介紹了Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字數(shù)量的方法,涉及java針對字符串的遍歷、編碼轉換、判斷等相關操作技巧,需要的朋友可以參考下2017-06-06

