本次记录一下tomcat的优化处理 Tomcat 是一个小型的轻量级的应用服务器,深受JavaEE 开发者的喜爱吗,不过,许多开发人员都是是用Tomcat 默认的配置方式(bio)。 Tomcat 有三种运行模式,BIO、NIO、 APR 三种模式都有区别
在这里,大家要了解下 Tomcat Manager 的相关配置,因为通过它可以查看自己tomcat 的许多信息。 Tomcat Manager 的配置很简单: 在Tomcat 安装目录/conf/tomcat-users.xml 中配置: 如:
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<user username="jaky" password="123456" roles="manager-gui,admin-gui"/>
</tomcat-users>
在这里,rolename 不是随便定义的
For example, to add the
manager-gui
role to a user named tomcat
with a password of s3cret
, add the following to the config file listed above.
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single
manager
role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.
manager-gui
- allows access to the HTML GUI and the status pagesmanager-script
- allows access to the text interface and the status pagesmanager-jmx
- allows access to the JMX proxy and the status pagesmanager-status
- allows access to the status pages only
The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:
- Users with the
manager-gui
role should not be granted either themanager-script
ormanager-jmx
roles. - If the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session.
For more information - please see the Manager App HOW-TO.
BIO
bio(blocking I/O) ,即阻塞型IO,是传统Java I/O 操作。Tomcat 默认就是使用这种模式,但是,BIO 却是性能最差的一种模式。
NIO
nio(New I/O),新型IO,是非阻塞上式的。配置也很简单,只需在server.xml 中修改一下即可
<Connector port="8080" protocol="HTTP/1.1"APR apr(Apache Portable Runtime/Apache可移植运行时),是Apache HTTP 服务器的支持库。Tomcat 以JNI的形式调用Apache HTTP 的服务器的核心动态库来处理文件读取和网络传输操作,从而大大的提高Tomcat对静态文件的处理性能,Tomcat APR 是运行高并发应用的首选,Tomcat 6.x的许多版本需要手动设置APR,而且听说还有点难,但幸运的是,Tomcat 6.x版本从6.0.32开始就默认支持apr。Tomcat 7.x版本从7.0.30 开始就默认支持apr。如果用的还是低版本的用户,强烈建议升级
connectionTimeout="20000"
redirectPort="8443" />
改为 <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />成功后在Tomcat Manager 中显示:
<Connector port="80" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
在这里你会发现就是改变了protocol(window 是这样,linux 还要进行配置)
这里就是API,说的很明确
protocol |
Sets the protocol to handle incoming traffic. The default value is |
7. 让测试说话
优化系统最忌讳的就是只调优不测试,有时不适当的优化反而会让性能更低。以上所有的优化方法都要在本地进行性能测试过后再不断调整参数,这样最终才能达到最佳的优化效果。
补充Bio、Nio、Apr模式的测试结果:
对于这几种模式,我用ab命令模拟1000并发测试10000次,测试结果比较意外,为了确认结果,我每种方式反复测试了10多次,并且在两个服务器上都测试了一遍。结果发现Bio和Nio性能差别非常微弱,难怪默认居然还是Bio。但是采用apr,连接建立的速度会有50%~100%的提升。直接调用操作系统层果然神速啊,这里强烈推荐apr方式!
再优化一点:
我们还可以调整一下Connector的相关参数:
这里是Tomcat 7.0.64 的介绍
roduction |
The HTTP Connector element represents a Connector component that supports the HTTP/1.1 protocol. It enables Catalina to function as a stand-alone web server, in addition to its ability to execute servlets and JSP pages. A particular instance of this component listens for connections on a specific TCP port number on the server. One or more suchConnectors can be configured as part of a single Service, each forwarding to the associated Engine to perform request processing and create the response. If you wish to configure the Connector that is used for connections to web servers using the AJP protocol (such as the Each incoming request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the |
minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75
所以一般只需设置
maxThreads和acceptCount这两个值
maxThreads |
The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. |
acceptCount |
The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100. |
connectionTimeout |
The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to |
如NIO 形式
- <Connector port="8080"
- protocol="org.apache.coyote.http11.Http11NioProtocol"
- connectionTimeout="20000"
- redirectPort="8443"
- maxThreads="500"
- minSpareThreads="20"
- acceptCount="100"
- disableUploadTimeout="true"
- enableLookups="false"
- URIEncoding="UTF-8" />
这里带上API http://tomcat.apache.org/tomcat-7.0-doc/config/http.html