SpringBoot项目中使用Tomcat、Undertow、jetty等容器

SpringBoot项目中使用Tomcat、Undertow、jetty等容器;

1. 默认使用Tomcat容器,直接运行项目即可:Java -jar xxx.jar

# Tomcat的一些配置参数调优参考
server:
  tomcat:
    uri-encoding: UTF-8
    #最小线程数
    min-spare-threads: 500
    #最大线程数
    max-threads: 2500
    #最大链接数
    max-connections: 6500
    #最大等待队列长度
    accept-count: 1000
    #请求头最大长度kb
    max-http-header-size: 1048576
    #请请求体最大长度kb
    #max-http-post-size: 2097152
  #服务http端口
  port: 8080
  #链接建立超时时间
  connection-timeout: 12000
  servlet:
    #访问根路径
    context-path: /song

2. 使用undertow容器

2-1 引入Maven依赖,同时屏蔽内置Tomcat

<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>

2-2 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类

2-3 一个特别的报错警告及处理方法

解决使用undertow容器报io.undertow.websockets.jsr -
UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used

2-3-1 新增一个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);
        });
    }
}

2-4 启动,观察控制台即可验证

在这里插入图片描述

3. 使用jetty容器

3-1 引入Maven依赖,同时屏蔽内置Tomcat

<!--web依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--排除spring-boot-starter-tomcat依赖-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--Jetty依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

3-2 jetty容器的常用配置参考

server.port=8080
server.servlet.context-path=/home
 
# Jetty 启用处理请求的线程数,设置值小于或等于处理器的2倍,要使用的接受线程数
server.jetty.acceptors=100
# 设置Jetty post content 大小,单位为byte
server.jetty.max-http-post-size=0
# 设置Jetty selectors 线程数,要使用的选择器线程数
server.jetty.selectors=10

3-3 启动SpringBoot应用,即可发现jetty已开始运行

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_17847881/article/details/131347186