Tomcat的优化处理

站在巨人的肩膀上

本次记录一下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 pages
    • manager-script - allows access to the text interface and the status pages
    • manager-jmx - allows access to the JMX proxy and the status pages
    • manager-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 the manager-script or manager-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"  
connectionTimeout = "20000"  
redirectPort="8443" />  
改为 
< Connector   port = "8080"   protocol = "org.apache.coyote.http11.Http11NioProtocol"  
connectionTimeout = "20000"  
redirectPort = "8443"  />

成功后在Tomcat Manager 中显示:



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。如果用的还是低版本的用户,强烈建议升级

<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 HTTP/1.1 which uses an auto-switching mechanism to select either a blocking Java based connector or an APR/native based connector. If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. If the native library cannot be found, the blocking Java based connector will be used. Note that the APR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanism described above, the following values may be used:
org.apache.coyote.http11.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector.
Custom implementations may also be used.
Take a look at our Connector Comparison chart. The configuration for both Java connectors is identical, for http and https.
For more information on the APR connector and APR specific SSL settings please visit the APR documentation


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 mod_jk 1.2.x connector for Apache 1.3), please refer to the AJP Connector documentation.

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 maxThreads attribute). If still more simultaneous requests are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCount attribute). Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them.

一些博客记录的还有

minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75

这些博主大神写的时候应该是比较以前的。因为现在的tomcat,高版本点都没有这两个参数存在了,所以最好别设置,不然就秀智商了

所以一般只需设置

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
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 false, this timeout will also be used when reading the request body (if any).

可以设置为 20000ms  即20秒,这样总体应该会好点

如NIO 形式

    
    
  1. <Connector port="8080"   
  2.            protocol="org.apache.coyote.http11.Http11NioProtocol"  
  3.            connectionTimeout="20000"  
  4.            redirectPort="8443"   
  5.            maxThreads="500"   
  6.            minSpareThreads="20"  
  7.            acceptCount="100" 
  8.            disableUploadTimeout="true" 
  9.            enableLookups="false"   
  10.            URIEncoding="UTF-8" /> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值