004-tomcat优化-Catalina中JVM优化、Connector优化、NIO化

一、服务端web层

  涉及内容Nginx、Varnish、JVM、Web服务器【Tomcat、Web应用开发(Filter、spring mvc、css、js、jsp)】

1.1、基本优化思路

1、尽量缩短单个请求的处理时间

2、尽可能多的并发处理请求

3、应用能够横向扩展

1.2、tomcat的JVM基本优化

  tomcat默认的配置已经是经过优化的,个人可优化空间较小,主要调整的是,跟具体使用场景相关的设置。如下

1、合理分配Tomcat需要的内存

  主要是启动tomcat的时候设置catalina.sh中的JAVA_OPTS,常见设置如下:

  -server:启用JDK的Server版本

  -Xms:虚拟机初始化时的最小内存

  -Xmx:虚拟机可使用的最大内存【建议到物理内存的80%】

  -XX:PermSize:持久代初始值

  -XX:MaxPermSize:持久代最大内存(默认32M)

  -XX:MaxNewSize:新生代内存的最大内存(默认16M)

说明:

  1》一般设置-Xms、-Xmx相对以避免在每次GC后调整堆的大小。

    因为默认空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制;

    空余堆内存大于70%时,JVM会减少堆直到-Xms的最小限制。

  2》查看配置是否生效:jmap -heap tomcat的进程号

示例一、默认配置:启动,查看现有配置,可以看到默认配置如下:

jmap -heap 11220

Attaching to process ID 11220, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.162-b12

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 960495616 (916.0MB)
   NewSize                  = 19922944 (19.0MB)
   MaxNewSize               = 319815680 (305.0MB)
   OldSize                  = 40894464 (39.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 15728640 (15.0MB)
   used     = 2111592 (2.0137710571289062MB)
   free     = 13617048 (12.986228942871094MB)
   13.425140380859375% used
From Space:
   capacity = 2097152 (2.0MB)
   used     = 2064432 (1.9687957763671875MB)
   free     = 32720 (0.0312042236328125MB)
   98.43978881835938% used
To Space:
   capacity = 5767168 (5.5MB)
   used     = 0 (0.0MB)
   free     = 5767168 (5.5MB)
   0.0% used
PS Old Generation
   capacity = 40894464 (39.0MB)
   used     = 13474480 (12.850265502929688MB)
   free     = 27419984 (26.149734497070312MB)
   32.94939872546074% used

10581 interned Strings occupying 940368 bytes.

 

示例二、配置JAVA_OPTS

在catalina的开始部分即可,注意jdk8 持久代已废除

window配置

set JAVA_OPTS=-server -Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxPermSize=512M -XX:MaxNewSize=1024M

linux配置

JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxPermSize=512M -XX:MaxNewSize=1024M"

 

  配置说明:JAVA_OPTS后面是字符串,参数间用空格,没有“:”的参数直接跟踪具体值,有的化用等号。

jmap -heap 7836
Attaching to process ID 7836, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.162-b12

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 1073741824 (1024.0MB)
   NewSize                  = 1073217536 (1023.5MB)
   MaxNewSize               = 1073217536 (1023.5MB)
   OldSize                  = 524288 (0.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 805830656 (768.5MB)
   used     = 225768040 (215.3091812133789MB)
   free     = 580062616 (553.1908187866211MB)
   28.016809526789707% used
From Space:
   capacity = 133693440 (127.5MB)
   used     = 0 (0.0MB)
   free     = 133693440 (127.5MB)
   0.0% used
To Space:
   capacity = 133693440 (127.5MB)
   used     = 0 (0.0MB)
   free     = 133693440 (127.5MB)
   0.0% used
PS Old Generation
   capacity = 524288 (0.5MB)
   used     = 0 (0.0MB)
   free     = 524288 (0.5MB)
   0.0% used

10638 interned Strings occupying 944104 bytes.

1.3、tomcat本身优化

Tomcat配置文件server.xml中<Connector .../>

常用参数说明:操作系统最大连接数、cpu、内存等

maxConnections:最大连接数,对BIO模式,默认等于maxThreads;对NIO模式默认10000;对APR/native默认8192
compression:设置是否开启Gzip压缩
compressableMimeType:哪些类型需要压缩,默认是text/html,text/xml,text/plain
compressionMinSize:启用压缩的输出内容大小,默认2048字节=2k 大于2k开始压缩
minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为 10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为 75
acceptCount:当处理任务的线程数达到最大时,接受排队的请求个数;允许的最大连接数,应大于等于 maxProcessors ,默认值为 100  ;监听端口队列最大数,满了之后客户请求会被拒绝(不能小于maxSpareThreads  )
enableLookups:是否反查域名,取值为: truefalse 。为了提高处理能力,应设置为 false
connectionTimeout:网络连接超时,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。 
maxThreads  客户请求最大线程数,同时处理的任务个数,默认值200
minSpareThreads    Tomcat初始化时创建的 socket 线程数,默认10
maxSpareThreads   Tomcat连接器的最大空闲 socket 线程数
redirectPort        在需要基于安全通道的场合,把客户请求转发到基于SSL 的 redirectPort 端口
URIEncoding    URL统一编码

说明:

  1》如果要加大并发连接数,应同时加大maxThreads和acceptCount,可以设置成一样

    其中和最大连接数相关的参数为maxProcessors 和 acceptCount 。如果要加大并发连接数,应同时加大这两个参数。

  2》web server允许的最大连接数还受制于操作系统的内核参数设置,通常 Windows 是 2000 个左右, Linux 是 1000 个左右。

    可以通过 :ulimit -a查看,主要查看IO的open file 默认1024,可以设置大一点。

  3》如果配置了<Executor>,在<Connector>中通过executor属性指定参照<Executor>,那么<Connector>中关于线程的配置失效,以<Executor>中配置为准

1.4、关于BIO/NIO/APR

(1)BIO是最稳定最老的一个连接器,是采用阻塞的方式,意味着每个连接线程绑定到每个Http请求,直到获得Http响应返回,如果Http客户端请求的是keep-Alive连接,那么这些连接也许一直保持着直至达到timeout时间,这期间不能用于其它请求。
(2)NIO是使用Java的异步IO技术,不做阻塞,要使用的话,直接修改server.xml里的Connector节点,修改protocol为 :protocol="org.apache.coyote.http11.Http11NioProtocol"

(3)APR是使用原生C语言编写的非堵塞I/O,但是需要安装apr和native,直接启动就支持apr,能大幅度提升性能。使用时指定protocol为protocol=“org.apache.coyote.http11.Http11AprProtocol” 。

  可以到http://apr.apache.org/download.cgi去下载,大致的安装步骤如下:

  A:安装apr
    ./configure --prefix=/usr/local/apr
    make make install
  B:安装apr-iconv
    ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
    make make install
  C:安装apr-util
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-
    iconv=/usr/local/apr-iconv/bin/apriconv
    make make install
  D:安装tomcat-native ,就在Tomcat的bin下自带
    tar zxvf tomcat-native.tar.gz
    cd tomcat-native-1.1.29-src/jni/native
    ./configure --with-apr=/usr/local/apr
    make make install

  E:设置 apr 的环境变量
    进入Tomcat的bin路径下,打开catalina.sh,在文件的#!/bin/sh下添加如下内容:
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib export LD_LIBRARY_PATH
    这样就只是给这个TOMCAT添加了APR,不破坏其它TOMCAT的配置
  G:重新启动Tomcat,查看日志信息,应该有类似如下的信息:
    org.apache.catalina.core.AprLifecycleListener.init Loaded APR based Apache Tomcat Native library 1.1.29 using APR version 1.5.0.

  参考配置如下:

<Connector port="8080" protocol=" org.apache.coyote.http11.Http11AprProtocol "
  URIEncoding="UTF-8"
  maxConnections="10000"
  maxThreads="2000"
  acceptCount="2000"
  minSpareThreads="100"
  compression="on"
  compressionMinSize="2048"
  compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
  enableLookups="false"
  disableUploadTimeout="true"
  connectionTimeout="20000"
  redirectPort="8443" />

 

 

 

• -n1000 –c100
• bio+apr nio apr
• Tomcat 500 400 600
• MyApp 450 300 800

附录:所有的Connector 支持的参数

AttributeDescription描述
allowTrace A boolean value which can be used to enable or disable the TRACE HTTP method. If not specified, this attribute is set to false 是否禁用http trace方法,默认值fales
asyncTimeout The default timeout for asynchronous requests in milliseconds. If not specified, this attribute is set to the Servlet specification default of 30000 (30 seconds) 异步请求的默认超时时间(以毫秒为单位)。 如果未指定,则此属性设置为默认值为30000(30秒)
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查找,并以String形式返回IP地址(从而提高性能)。 默认情况下,DNS查找被禁用。
maxHeaderCount The maximum number of headers in a request that are allowed by the container. A request that contains more headers than the specified limit will be rejected. A value of less than 0 means no limit. If not specified, a default of 100 is used. 容器允许的请求中的最大header数。 包含比指定限制更多的header的请求将被拒绝。 小于0表示无限制,默认值100。
maxParameterCount 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 zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit. POST的最大值(以字节为单位),将由容器FORM URL参数解析来处理。若小于0则不限制,默认为2097152(2MB)。 请注意,FailedRequestFilter可用于拒绝超出此限制的请求。
maxPostSize The maximum size in bytes of the POST which will be saved/buffered by the container during FORM or CLIENT-CERT authentication. For both types of authentication, the POST will be saved/buffered before the user is authenticated. For CLIENT-CERT authentication, the POST is buffered for the duration of the SSL handshake and the buffer emptied when the request is processed. For FORM authentication the POST is saved whilst the user is re-directed to the login form and is retained until the user successfully authenticates or the session associated with the authentication request expires. The limit can be disabled by setting this attribute to -1. Setting the attribute to zero will disable the saving of POST data during authentication. If not specified, this attribute is set to 4096 (4 kilobytes) 在FORM或CLIENT-CERT身份验证期间,将由容器保存/缓冲的POST的最大大小(以字节为单位)。 对于这两种类型的身份验证,POST将在用户进行身份验证之前进行保存/缓冲。 对于CLIENT-CERT认证,POST在SSL握手期间进行缓冲,缓冲区在处理请求时清空。 对于FORM身份验证,POST将被保存,同时用户被重定向到登录表单,并保留,直到用户成功认证或与身份验证请求相关联的会话过期。 可以通过将此属性设置为-1来禁用该限制。 将属性设置为零将在身份验证期间禁用POST数据的保存。 如果未指定,此属性设置为4096(4kb)
parseBodyMethods A comma-separated list of HTTP methods for which request bodies will be parsed for request parameters identically to POST. This is useful in RESTful applications that want to support POST-style semantics for PUT requests. Note that any setting other than POST causes Tomcat to behave in a way that goes against the intent of the servlet specification. The HTTP method TRACE is specifically forbidden here in accordance with the HTTP specification. The default is POST 以逗号分隔的HTTP方法列表,请求主体将被解析为与POST相同的请求参数。 这对于希望支持PUT请求的POST样式语义的RESTful应用程序很有用。 请注意,POST之外的任何设置都会导致Tomcat的行为违反了servlet规范。 这里根据HTTP规范特别禁止HTTP方法TRACE。 默认为POST
port The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. This is typically only useful in embedded and testing applications. 您的操作系统将只允许一个服务器应用程序侦听特定IP地址上的特定端口号。 如果使用特殊值0,则Tomcat将随机选择一个空闲端口用于此连接器。
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 non blocking Java NIO 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 non 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 connectororg.apache.coyote.http11.Http11NioProtocol - non blocking Java NIO connectororg.apache.coyote.http11.Http11Nio2Protocol - non blocking Java NIO2 connectororg.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  
proxyName If this Connector is being used in a proxy configuration, configure this attribute to specify the server name to be returned for calls to request.getServerName(). See Proxy Support for more information.  
proxyPort If this Connector is being used in a proxy configuration, configure this attribute to specify the server port to be returned for calls to request.getServerPort(). See Proxy Support for more information.  
redirectPort If this Connector is supporting non-SSL requests, and a request is received for which a matching requires SSL transport, Catalina will automatically redirect the request to the port number specified here.  
scheme Set this attribute to the name of the protocol you wish to have returned by calls to request.getScheme(). For example, you would set this attribute to “https” for an SSL Connector. The default value is “http”.  
secure Set this attribute to true if you wish to have calls to request.isSecure() to return true for requests received by this Connector. You would want this on an SSL Connector or a non SSL connector that is receiving data from a SSL accelerator, like a crypto card, a SSL appliance or even a webserver. The default value is false.  
URIEncoding This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.  
useBodyEncodingForURI This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.Notes: 1) This setting is applied only to the query string of a request. Unlike URIEncoding it does not affect the path portion of a request URI. 2) If request character encoding is not known (is not provided by a browser and is not set by SetCharacterEncodingFilter or a similar filter using Request.setCharacterEncoding method), the default encoding is always “ISO-8859-1”. The URIEncoding setting has no effect on this default.  
useIPVHosts Set this attribute to true to cause Tomcat to use the IP address that the request was received on to determine the Host to send the request to. The default value is false  
xpoweredBy Set this attribute to true to cause Tomcat to advertise support for the Servlet specification using the header recommended in the specification. The default value is false.  
The standard HTTP connectors (BIO, NIO, NIO2 and APR/native) all support  the following attributes in addition to the common Connector attributes listed above.
AttributeDescription描述
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。
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). 连接器在接受连接后等待的请求URI行的毫秒数。 使用值-1表示无(即无穷大)超时。 默认值为60000(即60秒),但请注意Tomcat附带的标准server.xml将其设置为20000(即20秒)。 除非disableUploadTimeout设置为false,否则读取请求主体(如果有的话)也将使用此超时。
keepAliveTimeout The number of milliseconds this Connector will wait for another HTTP request before closing the connection. The default value is to use the value that has been set for the connectionTimeout attribute. Use a value of -1 to indicate no (i.e. infinite) timeout. 连接器在关闭连接之前等待另一个HTTP请求的毫秒数。 默认值是使用为connectionTimeout属性设置的值。 使用值-1表示无(即无穷大)超时。
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 an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO and NIO2 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. 服务器在任何给定时间接受和处理的最大连接数。当达到这个数字时,服务器将接受一个进一步的连接,但不会处理。这个附加连接将被阻塞,直到正在处理的连接数降到maxConnections以下,服务器再次开始接受并重新处理新的连接。请注意,一旦达到限制,操作系统仍然可以接受基于acceptCount设置的连接。默认值因连接器类型而异。对于BIO,默认值是maxThreads的值,除非使用Executor,在这种情况下,默认值将是执行器的maxThreads值。对于NIO和NIO2,默认值为10000.对于APR / native,默认值为8192。请注意,对于Windows上的APR / native,配置的值将减少到小于或等于maxConnections的1024的最高倍数。这是出于性能原因。如果设置为-1,则禁用maxConnections功能,并且不计算连接。maxKeepAliveRequests The maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100. 在服务器关闭连接之前,可以流水线的最大HTTP请求数。 将此属性设置为1将禁用HTTP / 1.0保持活动,以及HTTP / 1.1保持活动和流水线。 将其设置为-1将允许无限量的流水线或保持活动的HTTP请求。 如果未指定,则此属性设置为100。
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. 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. 此连接器要创建的请求处理线程的最大数量,因此确定可以处理的最大并发请求数。 如果未指定,则此属性设置为200.如果执行程序与此连接器相关联,则此属性将被忽略,因为连接器将使用执行程序而不是内部线程池执行任务。 请注意,如果配置了执行程序,则为此属性设置的任何值都将被正确记录,但会将其(例如通过JMX)报告为-1,以表明它不被使用。
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 default conf/web.xml or in the web.xml of your web application. 连接器可以使用HTTP / 1.1 GZIP压缩来尝试保存服务器带宽。参数的可接受值为“关闭”(禁用压缩),“开”(允许压缩,导致文本数据压缩),“强制”(强制所有情况下的压缩)或数字整数值相当于“on”,但指定输出压缩前的最小数据量)。如果内容长度不知道,并且压缩设置为“开”或更积极,则输出也将被压缩。如果未指定,则此属性设置为“关闭”。注意:使用压缩(节省带宽)和使用sendfile功能(节省CPU周期)之间存在折衷。如果连接器支持发送文件功能,例如使用sendfile的NIO连接器将优先于压缩。症状将是静态文件大于48 Kb将被未压缩发送。您可以通过设置连接器的useSendfile属性来关闭sendfile,如下所述,或者在默认conf / web.xml或Web应用程序的web.xml中的DefaultServlet配置中更改发送文件使用阈值。
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元素中的名称的引用。 如果设置了此属性,并且命名的执行程序存在,则连接器将使用执行程序,并且所有其他线程属性将被忽略。 请注意,如果未为连接器指定共享执行程序,则连接器将使用专用的内部执行程序来提供线程池。

 

 

 

 

反倒是

posted @ 2019-05-07 22:14  bjlhx15  阅读(570)  评论(0编辑  收藏  举报
Copyright ©2011~2020 JD-李宏旭