目录
  • springboot切换使用undertow容器
    • maven引入jar
    • undertow的基本配置
    • 一个特别的报错警告
    • 验证成功 
    • 分享感觉
  • springboot替换默认容器
    • undertow简介
    • 性能比对
    • 项目中使用undertow 1.引入依赖

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 线程数,默认为 2 或者 CPU 的个数
server.undertow.threads.io=8
# 指定工作者线程个数,默认为 I/O 线程个数的 8 倍
server.undertow.threads.worker=256
# 设置 HTTP POST 内容的最大长度,默认不做限制
server.undertow.max-http-post-size=4MB
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理;
server.undertow.buffer-size=1024
# 是否分配的直接内存(NIO直接分配的堆外内存)
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);
        });
    }
}

验证成功 

springboot切换使用undertow容器的方式

看到Undertow started xxx就是使用undertow容器启动成功了。

分享感觉

网传undertow比tomcat、jetty都快省资源,还是费阻塞nio等等,实际上可能就没有什么感觉。

我其实用postman测试了以前的一些接口,感觉接口返回秒回,就是感觉快了。

后来运行2天(没有配置undertow,默认配置)有点小卡,然后,早上把配置改成上面的发布,再观察几天试试。

springboot替换默认容器

undertow简介

Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器

Undertow 被设计成为完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中

性能比对

使用jmeter进行压测比较

tomcat压测结果

springboot切换使用undertow容器的方式

springboot切换使用undertow容器的方式

将tomcat容器换成jetty容器进行测试

springboot切换使用undertow容器的方式

将jetty容器修改为undertow

springboot切换使用undertow容器的方式

springboot切换使用undertow容器的方式

从吞吐量看undertow要强于前两个

项目中使用undertow 1.引入依赖

在官网上可以看到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

可以根据自己的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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。