欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot切換使用undertow容器的方式

 更新時(shí)間:2022年07月08日 08:37:01   作者:肥仔哥哥1930  
最近稍微有點(diǎn)空閑,回頭再來優(yōu)化下基礎(chǔ)框架,也是一種重新學(xué)習(xí)。今天主要寫寫跟大家分享下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)文章

最新評論