首页后端开发其他后端知识springboot是什么,如何切换使用undertow容器

springboot是什么,如何切换使用undertow容器

时间2024-03-24 16:58:03发布访客分类其他后端知识浏览1473
导读:在这篇文章中我们会学习到关于“springboot是什么,如何切换使用undertow容器”的知识,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望对大家学习或工作能有帮助。下面就请大家跟着小编的思路一起来学习一下吧。...
在这篇文章中我们会学习到关于“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 WebServerFactoryCustomizerUndertowServletWebServerFactory>
 {

  @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容器已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注网络,小编将为大家输出更多高质量的实用文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: springboot是什么,如何切换使用undertow容器
本文地址: https://pptw.com/jishu/652194.html
MySQL中十个常用的字符串函数你了解多少? SpringMVC初始化流程是怎样的,包括哪些内容?

游客 回复需填写必要信息