优化范围
内存优化
线程优化
配置优化
线程优化
maxConnections
tomcat8.5/webapps/docs/config/http.html
The maximum number of connections that the server will accept and process at any given time.
Tomcat能接收和处理的最大的连接数,tomcat8以后默认是NIO,多路复用,所以连接数可以很大(之前的是一个请求,一个线程)
默认值:For NIO and NIO2 the default is 10000
. For APR/native, the default is 8192
.
acceptCount
tomcat8.5/webapps/docs/config/http.html
The maximum queue length for incoming connection requests when all possible request processing threads are in use
如果请求超出了maxConnections
,可以配置一个队列,把超出的连接压倒队列里,默认100,配的太大没有意义,如果队列满了之后,新的请求就无法处理
maxThreads
tomcat8.5/webapps/docs/config/http.html
The maximum number of request processing threads to be created by this Connector
最大的工作线程数,默认200,同一个时间点上同时处理的并发的请求数,即工作线程数,有多少个工作线程,就能同时处理多少个并发任务,取决于内存和cpu相关的配置
minSpareThreads
tomcat8.5/webapps/docs/config/http.htmlThe minimum number of threads always kept running
最小空闲的线程数,不要设置太小,如果请求突然增多,会有停顿
默认值:the default of 10
is used
配置优化
autoDeploy
tomcat8.5/webapps/docs/config/host.htmlThis flag value indicates if Tomcat should check perio
当tomcat正在运行的时候,如果是true的时候,周期性的检查有新的或更新的web应用tomcat8.5\conf\ server.xml生产环境下应改为false,否则影响性能
enableLookups
tomcat8.5/webapps/docs/config/http.html
Set to true
if you want calls to request.getRemoteHost()
to perform DNS lookups in order to return the actual host name of the remote client.
如果掉调request.getRemoteHost()
能做DNS查询,设为true,会返回域名对应的IP,如果为false,跳过DNS查询,默认为false
reloadable
webapps/docs/config/context.html
Set to true
if you want Catalina to monitor classes in /WEB-INF/classes/
and /WEB-INF/lib
for changes, and automatically reload the web application if a change is detected
如果是true,如果/WEB-INF/classes/
和 /WEB-INF/lib
发现变化,然后重新载入发生变化的类,如果发生变化会重新载入发生变化的类,影响性能
默认为false
tomcat8.5\conf\ server.xml
maxConnections
、acceptCount、maxThreads相互关系
我们可以把tomcat比做一个电影院,流程是取号、买票、观影,acceptCount比作前厅(容纳取到号的人)、maxConnections比作大厅(容纳买到票的人)、maxThreads比作影厅(可以理解一个影厅只容纳一个人,因为一个线程同时只处理一个请求),以下场景是针对已达到maxConnections最大值来讨论的
1)取号:如果前厅人数已达到acceptCount,则拿号失败,会得到Connection refused connect的回复信息。反之则会进入前厅,等待买票。
2)买票:当大厅人数小于maxConnections时,前厅的人就可以进入大厅
3)观影:当影厅的人离开时,大厅的部分人能进入影厅,一般来讲大厅的容量要远大于影厅的数量。
maxConnections
、acceptCount、maxThreads实例
##tomcat server.xml配置参数
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" acceptCount="2" maxConnections="10" maxThreads="2"
connectionTimeout="20000" redirectPort="8443" />
##Servlet的代码:休眠20秒
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
InputStream is = request.getInputStream();
System.out.println(new Date()+":"+is+"开始");
Thread.sleep(20000);
System.out.println(new Date()+":"+is+"结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
一个请求或一个connection的生命周期:可以简单的理解为从doGet或doPost开始response响应结束。
使用JMeter测试,线程数20,连接超时时间10s,响应超时时间30s
测试结果:
察看结果数:
第1秒:8个请求得到响应数据:Connection refused connect
第20秒:2个请求正常响应
第30秒:剩余10个请求得到响应数据:Readtimed out
tomcat日志:
打印12组开始结束的日志,说明tomcat会处理acceptCount+maxConnections的请求,说明只要取到号,有足够的耐心,就肯定能够看到电影。