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操作,有點(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類
一個特別的報錯警告
解決使用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);
? ? ? ? });
? ? }
}驗(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è)計成為完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中
性能比對
使用jmeter進(jìn)行壓測比較
tomcat壓測結(jié)果


將tomcat容器換成jetty容器進(jìn)行測試

將jetty容器修改為undertow


從吞吐量看undertow要強(qiáng)于前兩個
項(xiàng)目中使用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版本進(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>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java詳解HashMap實(shí)現(xiàn)原理和源碼分析
這篇文章主要介紹了Java關(guān)于HashMap的實(shí)現(xiàn)原理并進(jìn)行源碼分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
Spring Cloud Feign接口返回流的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Feign接口返回流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
必知必會的SpringBoot實(shí)現(xiàn)熱部署兩種方式
這篇文章主要為大家介紹了必知必會的SpringBoot實(shí)現(xiàn)熱部署兩種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
SpringBoot實(shí)現(xiàn)簡單的登錄注冊的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了SpringBoot實(shí)現(xiàn)簡單的登錄注冊的項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字?jǐn)?shù)量的方法
這篇文章主要介紹了Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字?jǐn)?shù)量的方法,涉及java針對字符串的遍歷、編碼轉(zhuǎn)換、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Java切面(Aspect)的多種實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于Java切面(Aspect)的多種實(shí)現(xiàn)方式,在Java開發(fā)中切面(Aspect)是一種常用的編程方式,用于實(shí)現(xiàn)橫切關(guān)注點(diǎn)(cross-cutting concern),需要的朋友可以參考下2023-08-08

