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。 理論上來講同一時刻可以處理200個請求(200個線程處理200個耗時操作),但其實可以同時接受10000個請求連接,以及大於10000個連接時,還可以再等待100個連接。