Tomcat性能优化
优化思考
Tomcat也是Java进程,那么优化Tomcat也是考虑吞吐量
,响应时间
,CPU
,内存
等
使用jmeter压测,观察性能指标,查看相关指令
1.查看Tomcat进程
ps -ef|grep tomcat
2.查看最占资源的线程
top -H -p pid
3.查看进程信息
cat /pro/pid/status
优化点
Tomcat的组件都在conf/server.xml
和conf/web.xml
中,那么优化也只能从这两个方面考虑:
conf/server.xml
组件优化
conf/server.xml
主要包括Server
,Service
,Connector
,Engine
,Host
,Context
等组件,参考官网,根据官网推荐进行优化,Tomcat官网优化连接
-
Server
参考官网描述:In the Tomcat world, a Server represents the whole container. Tomcat provides a default implementation of the Server interface which is rarely customized by users.可以认为Server不用考虑优化 -
Service
参考官网描述:The Service element is rarely customized by users, as the default implementation is simple and sufficient. -
Engine
参考官网描述:The Engine interface may be implemented to supply custom Engines, though this is uncommon. -
Host
参考官网描述:Users rarely create custom Hosts because the StandardHost implementation provides significant additional functionality. -
Connector
参考官网描述:Creating a customized connector is a significant effort.(可以考虑优化) -
Context
参考官网描述:The Context interface may be implemented to create custom Contexts, but this is rarely the case because the StandardContext provides significant additional functionality.**综上所述:优化Tomcat可以考虑优化Connector**
至于web.xml和server.xml中的一些其它配置(jsp,默认的servlet等),暂时不用考虑了,因为现在大多数都是使用SpringBoot了。此外,最新的Tomcat也不再使用BIO了,所以优化Tomcat就考虑优化Connector了。
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
这里可以看出,Connector中最重要的就是executor了,那么配置合适的线程池就可以优化Tomcat了。这里需要根据机器的配置,以及结合jemeter的压测,不断的去修改maxThreads,minSpareThreads等参数即可。
在SpringBoot中,Tomcat中的常用配置为SpringBoot参数配置
Tomcat官网配置
server:
tomcat:
uri-encoding: UTF-8
#最大工作线程数,默认200, 4核8g内存,线程数经验值800
#操作系统做线程之间的切换调度是有系统开销的,所以不是越多越好。
max-threads: 1000
# 等待队列长度,默认100
accept-count: 1000
max-connections: 20000
# 最小工作空闲线程数,默认10, 适当增大一些,以便应对突然增长的访问量
min-spare-threads: 100
maxThreads,acceptCount,maxConnections均可以在官网找到相关描述,
- maxThreads
官方文档的说明为:每一次HTTP请求到达Web服务,tomcat都会创建一个线程来处理该请求,那么最大线程数决定了Web服务容器可以同时处理多少个请求。maxThreads默认200,肯定建议增加。但是,增加线程是有成本的,更多的线程,不仅仅会带来更多的线程上下文切换成本,而且意味着带来更多的内存消耗。JVM中默认情况下在创建新线程时会分配大小为1M的线程栈,所以,更多的线程异味着需要更多的内存。线程数的经验值为:1核2g内存为200,线程数经验值200;4核8g内存,线程数经验值800。 - acceptCount
官方文档的说明为:当所有的请求处理线程都在使用时,所能接收的连接请求的队列的最大长度。当队列已满时,任何的连接请求都将被拒绝。accept-count的默认值为100。
详细的来说:当调用HTTP请求数达到tomcat的最大线程数时,还有新的HTTP请求到来,这时tomcat会将该请求放在等待队列中,这个acceptCount就是指能够接受的最大等待数,默认100。如果等待队列也被放满了,这个时候再来新的请求就会被tomcat拒绝(connection refused)。 - maxConnections
官方文档的说明为:这个参数是指在同一时间,tomcat能够接受的最大连接数。对于Java的阻塞式BIO,默认值是maxthreads的值;如果在BIO模式使用定制的Executor执行器,默认值将是执行器中maxthreads的值。对于Java 新的NIO模式,maxConnections 默认值是10000。
对于windows上APR/native IO模式,maxConnections默认值为8192,这是出于性能原因,如果配置的值不是1024的倍数,maxConnections 的实际值将减少到1024的最大倍数。
如果设置为-1,则禁用maxconnections功能,表示不限制tomcat容器的连接数。
maxConnections和accept-count的关系为:当连接数达到最大值maxConnections后,系统会继续接收连接,但不会超过acceptCount的值。
这三者的联系上是:在NIO的场景下,每一次请求会创建一个连接,当达到maxConnections,没有达到acceptCount时,会继续接收请求,但是当达到acceptCount时,则会得到Tomcat的Connection refused connect 的回复信息。
Tomcat处理流程图如下: