Tomcat 7 相关参数优化说明及配置最佳案例

目录

  • 前言
  • 环境搭建
  • 目的
  • 服务器资源
  • 优化配置
  • 禁用AJP连接器

前言

基于Tomcat 7,通过调其内部参数,提高tomcat性能。

0、环境搭建

参考:在一台服务器上配置多个Tomcat 教程  https://blog.csdn.net/py_tamir/article/details/82490900

1、目的

通过优化tomcat提高网站的并发能力。

2、服务器资源

服务器所能提供CPU、内存、硬盘的性能对处理能力有决定性影响。

3、优化配置

3.1 配置tomcat管理员账户

在conf/ tomcat-users.xml下添加用户:

<role rolename="manager"/>

<role rolename="manager-gui"/>

<role rolename="admin"/>

<role rolename="admin-gui"/>

<user username="tomcat" password="tomcat" roles="admin-gui,admin,manager-gui,manager"/>

启动tomcat,登录查看信息:

http://localhost:8080/

3.2 tomcat的3种运行模式

  • bio 默认的模式,性能非常低下,没有经过任何优化处理和支持.
  • nio 利用java的异步io护理技术,no blocking IO技术.
  • apr 安装起来最困难,但是从操作系统级别来解决异步的IO问题,大幅度的提高性能.

3.2.1启动NIO模式

修改server.xml里的Connector节点,修改protocol为org.apache.coyote.http11.Http11NioProtocol

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />

3.3 执行器(线程池)

在tomcat中每一个用户请求都是一个线程,所以可以使用线程池提高性能。

3.3.1 开启并且使用

本人测试显示-1,查官方文档如下:

但为什么会显示-1呢? 查了tomcat7文档的maxThreads属性: 
这里写图片描述

最后一句话“Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.”就说到了这个情况,意思大概是: 
如果配置了一个Executor,则该属性的任何值集将被正确记录,但是它将被显示为-1

3.3.2 参数说明

maxThreads(最大线程数)

(int) The max number of active threads in this pool, default is 200

minSpareThreads(最小活跃线程数)

(int) The minimum number of threads always kept alive, default is 25

maxQueueSize(最大的等待队里数,超过则请求拒绝)

(int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is Integer.MAX_VALUE

prestartminSpareThreads

(是否在启动时就生成minSpareThreads个线程)

(boolean) Whether minSpareThreads should be started when starting the Executor or not, the default is false

3.3.3 最佳实战

3.4 连接器(connector)

Connector是Tomcat接收请求的入口,每个Connector有自己专属的监听端口,Connector有两种:

  • HTTP Connector
  • AJP Connector

3.4.1 通用属性

enableLookups

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. Set to false to skip the DNS lookup and return the IP address in String form instead (thereby improving performance). By default, DNS lookups are disabled.

若是你想request.getRemoteHost()的调用 履行,以便返回的长途客户端的实际主机名的DNS查询,则设置为true。设置为false时跳过DNS查找,并返回字符串情势的IP地址(从而

提高性能)。默认景象下,禁用DNS查找。

maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

将被容器以FORM URL参数形式处理的最大长度(以字节为单位)的POST。通过设置此属性的值小于或等于0可以禁用该限制。如果没有指定,该属性被设置为2097152(2兆字节)。

URIEncoding

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

这将指定使用的字符编码​​,来解码URI字符。如果没有指定,ISO-8859-1将被使用。

3.4.2  标准实现

标准的HTTP连接器(BIO,NIO和APR/native)都支持以下属性。

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.

当所有可能的请求处理线程都在使用时,传入连接请求的最大队列长度。当队列满时收到的任何请求将被拒绝。默认值是100。

acceptorThreadCount

The number of threads to be used to accept connections. Increase this value on a multi CPU machine, although you would never really need more than 2. Also, with a lot of non keep alive connections, you might want to increase this value as well. Default value is 1.

用于接受连接的线程的数量。在一个多CPU的机器上,增加该值,虽然你可能不会真正需要超过2个。此外,有很多非保持活动连接,您可能需要增加这个值。默认值是 1。

connectionUploadTimeout

Specifies the timeout, in milliseconds, to use while a data upload is in progress. This only takes effect if disableUploadTimeout is set to false.

上传数据过程中,指定的以毫秒为单位超时时间。只有在设置disableUploadTimeout为false有效。

disableUploadTimeout

This flag allows the servlet container to use a different, usually longer connection timeout during data upload. If not specified, this attribute is set to true which disables this longer timeout.

此标志允许servlet容器在数据上传时使用不同的连接超时,通常较长。如果没有指定,该属性被设置为true,禁用上传超时。

executor

A reference to the name in an Executor element. If this attribute is set, and the named executor exists, the connector will use the executor, and all the other thread attributes will be ignored. Note that if a shared executor is not specified for a connector then the connector will use a private, internal executor to provide the thread pool.

指向Executor元素的引用。如果这个属性被设置,并且被命名的executor存在,连接器将使用这个executor,而其他所有线程相关属性将被忽略。请注意共享的executor如果没有指定到一个连接器,则该连接器将使用一个私有的,内部的executor来提供线程池。

maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value varies by connector type. For BIO the default is the value of maxThreads unless anExecutor is used in which case the default will be the value of maxThreads from the executor. For NIO the default is 10000. For APR/native, the default is 8192.

Note that for APR/native on Windows, the configured value will be reduced to the highest multiple of 1024 that is less than or equal to maxConnections. This is done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connections are not counted.

在任何给定的时间服务器接受并处理的最大连接数。当这个数字已经达到了,服务器将不会接受任何连接,直到连接的数量降到低于此值。基于acceptCount的设置,操作系统可能仍然接受连接。默认值根据不同的连接器类型而不同。对于BIO,默认的是maxThreads的值,除非使用了Executor,在这种情况下默认值是executor的maxThreads值 。对于NIO的默认值是10000。APR /native的默认值是8192。

需要注意的是Windows系统的APR/native,所配置的值将减少到小于或等于maxConnections的1024的倍数的最大值。这样做是出于性能方面的考虑。

如果设置的值-1,maxConnections功能被禁用,而且连接数将不做计算。

 

compression

The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off".

Note: There is a tradeoff between using compression (saving your bandwidth) and using the sendfile feature (saving your CPU cycles). If the connector supports the sendfile feature, e.g. the NIO connector, using sendfile will take precedence over compression. The symptoms will be that static files greater that 48 Kb will be sent uncompressed. You can turn off sendfile by setting useSendfile attribute of the connector, as documented below, or change the sendfile usage threshold in the configuration of the DefaultServlet in the defaultconf/web.xml or in the web.xml of your web application.

为了节省服务器带宽,连接器可以使用HTTP/1.1 GZIP压缩。可接受的参数的值是“off ”(禁用压缩),“on ”(允许压缩,这会导致文本数据被压缩),“force ”(强制在所有的情况下压缩),或者一个整数值(这是相当于为“on”,但指定了输出之前被压缩的数据最小量)。如果不知道内容长度但被设置为“on”或更积极的压缩,输出的数据也将被压缩。如果没有指定,该属性被设置为“关”。

注意:这是使用压缩(节省您的带宽)和使用sendfile功能(节省你的CPU周期)之间的权衡。如果连接器支持sendfile功能,例如NIO连接,则使用sendfile将优先于压缩。症状是48 KB的静态文件将未压缩就发送。你可以如下文所述通过设置连接器的useSendfile属性来关闭sendfile,或在默认的conf/web.xml或者你的web应用的web.xml中配置DefaultServlet来改变sendfile的使用量阈值。

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.

最多同时处理的连接数,Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。如果没有指定,该属性被设置为200。如果使用了execute将忽略此连接器的该属性,连接器将使用execute,而不是一个内部线程池来处理请求。

minSpareThreads

The minimum number of threads always kept running. If not specified, the default of 10 is used.

始终保持运行最小线程数。如果没有指定,则默认为10。

SSLEnabled

Use this attribute to enable SSL traffic on a connector. To turn on SSL handshake/encryption/decryption on a connector set this value to true. The default value is false. When turning this value true you will want to set the scheme and the secureattributes as well to pass the correct request.getScheme() and request.isSecure() values to the servlets See SSL Support for more information.

在连接器上使用此属性来启用SSL加密传输。如果要打开SSL握手/加密/解密,请设置true。默认值是false。当设置这个值为true时,为了传递正确的request.getScheme()和 request.isSecure()到servlets,你需要设置scheme和secure属性。更多信息请查看SSL支持。

3.4.3 最佳配置

4、禁用AJP连接器

AJP(Apache JServer Protocol),AJPv13协议是面向包的。WEB服务器和Servlet容器通过TCP连接来交互;为了节省SOCKET创建的昂贵代价,WEB服务器会尝试维护一个永久TCP连接到servlet容器,并且在多个请求和响应周期过程会重用连接。

一般是使用Nginx+tomcat的架构,所以用不着AJP协议,故可以将AJP连接器禁用。

在管理界面中看不到ajp了:

猜你喜欢

转载自blog.csdn.net/py_tamir/article/details/82491056
今日推荐