SpringBoot 内嵌Tomcat的默认线程配置

https://www.cnblogs.com/silenceshining/p/14058944.html

SpringBoot中如果使用内嵌Tomcat,那么内嵌Tomcat的默认配置在ServerProperties(org.springframework.boot.autoconfigure.web)中,具体内容如下:
复制代码

/**
* Tomcat properties.
/
public static class Tomcat {/
*
* Maximum amount of worker threads.最大的工作线程数,默认为200,只能最多有200个耗时(比如查数据库)操作同时进行,一般小型应用中,达不到200个并发耗时操作。
*/
private int maxThreads = 200;

    /**
     * Minimum amount of worker threads.最小工作线程数,默认为10,即初始化时会创建10个线程用于处理请求。
     */
    private int minSpareThreads = 10;/**
     * Maximum number of connections that the server accepts and processes at any
     * given time. Once the limit has been reached, the operating system may still
     * accept connections based on the "acceptCount" property.

Tomcat在给定时间(同一时间)能接受的最大连接数。
*/
private int maxConnections = 10000;

    /**
     * Maximum queue length for incoming connection requests when all possible request
     * processing threads are in use.当前连接数超过maxConnections时,还能接受的连接的数量(排队的数量)。
     */
    private int acceptCount = 100;

}

复制代码

内嵌Tomcat使用的默认协议为NIO,配置在TomcatServletWebServerFactory类中,如下:
复制代码

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory
implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware {

private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

private static final Set<Class<?>> NO_CLASSES = Collections.emptySet();

/**
 * The class name of default protocol used.协议的默认配置
 */
public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";

private File baseDirectory;


}

复制代码

总结:
springboot 内置的Tomcat采用NIO协议,配置的参数为server.tomcat.maxThreads 200,
server.tomcat.maxConnections 1000,
server.tomcat.acceptCount 100。

可以修改为

server.tomcat.maxThreads 800,
server.tomcat.maxConnections 10000,
server.tomcat.acceptCount 100。

理论上来讲同一时刻可以处理200个请求(200个线程处理200个耗时操作),但其实可以同时接受10000个请求连接,以及大于10000个连接时,还可以再等待100个连接。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐