您现在的位置是:群英 > 开发技术 > 编程语言
springboot是什么,如何切换使用undertow容器
Admin发表于 2022-10-11 17:55:51771 次浏览
在这篇文章中我们会学习到关于“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 线程数,默认为 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);
        });
    }
}

验证成功 

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

分享感觉

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

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

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

springboot替换默认容器

undertow简介

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

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

性能比对

使用jmeter进行压测比较

tomcat压测结果

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

将jetty容器修改为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>



关于“springboot是什么,如何切换使用undertow容器”的内容就介绍到这,感谢各位的阅读,相信大家对springboot是什么,如何切换使用undertow容器已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2021-10-28 18:54:18 
摘要:本文我们主要了解PHP中date()函数的内容,那么date()函数能做什么呢?date() 函数功能是用于格式化时间,返回一个字符串。date()函数的应用还是比较多的,也是一个很基础的函数,所以要掌握date()函数的用法,那么date()函数怎样用呢?
2022-05-17 11:50:53 
摘要:本篇文章给大家带来的内容是关于html5的drag和drop的用法示例(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
2022-10-08 17:52:29 
摘要:在PHP中想要完成在数组的首尾插入元素的话,需要通过array_unshift函数和array_push函数。下面我们就分别来看一下这两个函数的使用。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部