涉及內容Nginx、Varnish、JVM、Web服務器【Tomcat、Web應用開發(Filter、spring mvc、css、js、jsp)】javascript
一、儘可能縮短單個請求的處理時間css
二、儘量多的併發處理請求html
三、應用可以橫向擴展java
tomcat默認的配置已是通過優化的,我的可優化空間較小,主要調整的是,跟具體使用場景相關的設置。以下linux
一、合理分配Tomcat須要的內存web
主要是啓動tomcat的時候設置catalina.sh中的JAVA_OPTS,常見設置以下:spring
-server:啓用JDK的Server版本apache
-Xms:虛擬機初始化時的最小內存tomcat
-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.
在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.
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:是否反查域名,取值爲: true 或 false 。爲了提升處理能力,應設置爲 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)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 支持的參數
Attribute | Description | 描述 |
---|---|---|
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. |
Attribute | Description | 描述 |
---|---|---|
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元素中的名稱的引用。 若是設置了此屬性,而且命名的執行程序存在,則鏈接器將使用執行程序,而且全部其餘線程屬性將被忽略。 請注意,若是未爲鏈接器指定共享執行程序,則鏈接器將使用專用的內部執行程序來提供線程池。 |
反卻是