Spring Boot嵌入式Tomcat默认HTTP线程池大小及查询方法
嵌入式Tomcat HTTP线程池默认值及查询方法
默认线程池大小
Spring Boot 1.x、2.x、3.x官方原生版本中,嵌入式Tomcat的HTTP请求处理线程池默认最大线程数为200。如果未在application.properties或application.yml中手动配置server.tomcat.max-threads参数,应用将默认采用该值运行。
注:如果使用的是厂商二次定制的Spring Boot框架,默认值可能被修改,最终值以实际运行时生效的配置为准。
常用查询默认值的途径
- 查看Spring Boot配置类源码
直接查阅org.springframework.boot.autoconfigure.web.ServerProperties类的源码,其内部静态类Tomcat中明确定义了默认参数:
该值就是Spring Boot官方预置的默认最大线程数。private int maxThreads = 200; - 运行时读取生效配置
可以在应用代码中注入ServerProperties对象,直接调用对应get方法获取当前实际生效的线程池配置,示例代码如下:
若应用开启了Actuator组件的env端点,也可以直接访问import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class TomcatConfigReader { @Autowired private ServerProperties serverProperties; public void printTomcatMaxThreads() { int maxThreads = serverProperties.getTomcat().getMaxThreads(); System.out.println("当前Tomcat最大线程数:" + maxThreads); } }/actuator/env接口,搜索server.tomcat.max-threads字段查看生效值。 - 查看配置元数据文件
解压Spring Boot的autoconfigure依赖jar包,找到META-INF/spring-configuration-metadata.json文件,搜索server.tomcat.max-threads配置项,其中defaultValue字段标注的数值即为官方默认值。
内容的提问来源于stack exchange,提问作者manjosh
相关产品推荐
相关产品推荐

