1. 程式人生 > >Tomcat調優總結(Tomcat自身優化、Linux內核優化、JVM優化)

Tomcat調優總結(Tomcat自身優化、Linux內核優化、JVM優化)

函數 基礎 order git sed .config cycle 運算 mage

Tomcat自身的調優是針對conf/server.xml中的幾個參數的調優設置。首先是對這幾個參數的含義要有深刻而清楚的理解。以tomcat8.5為例,講解參數。

同時也得認識到一點,tomcat調優也受制於linux內核。linux內核對tcp連接也有幾個參數可以調優。

因此我們可以將tomcat調優分為linux內核優化、java虛擬機調優和tomcat自身的優化。

一、Tomcat自身優化

1. maxThreads :tomcat創建的最大線程數,也就是同時處理的請求最大並發數。默認值是200

官網: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.

maxThreads如何配置(轉)

一般的服務器操作都包括量方面:1計算(主要消耗cpu),2等待(io、數據庫等)

第一種極端情況,如果我們的操作是純粹的計算,那麽系統響應時間的主要限制就是cpu的運算能力,此時maxThreads應該盡量設的小,降低同一時間內爭搶cpu的線程個數,可以提高計算效率,提高系統的整體處理能力。

第二種極端情況,如果我們的操作純粹是IO或者數據庫,那麽響應時間的主要限制就變為等待外部資源,此時maxThreads應該盡量設的大,這樣才能提高同時處理請求的個數,從而提高系統整體的處理能力。此情況下因為tomcat同時處理的請求量會比較大,所以需要關註一下tomcat的虛擬機內存設置和linux的open file限制。

我在測試時遇到一個問題,maxThreads我設置的比較大比如3000,當服務的線程數大到一定程度時,一般是2000出頭,單次請求的響應時間就會急劇的增加,

百思不得其解這是為什麽,四處尋求答案無果,最後我總結的原因可能是cpu在線程切換時消耗的時間隨著線程數量的增加越來越大,

cpu把大多數時間都用來在這2000多個線程直接切換上了,當然cpu就沒有時間來處理我們的程序了。

以前一直簡單的認為多線程=高效率。。其實多線程本身並不能提高cpu效率,線程過多反而會降低cpu效率。

當cpu核心數<線程數時,cpu就需要在多個線程直接來回切換,以保證每個線程都會獲得cpu時間,即通常我們說的並發執行。

所以maxThreads

的配置絕對不是越大越好。

現實應用中,我們的操作都會包含以上兩種類型(計算、等待),所以maxThreads的配置並沒有一個最優值,一定要根據具體情況來配置。

最好的做法是:在不斷測試的基礎上,不斷調整、優化,才能得到最合理的配置。

2. acceptCount:當tomcat的線程數達到了最大時,接收排隊的最大請求個數。默認值為100

官網: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.

maxThreads與acceptCount這兩個值是如何起作用的呢?

情況1:接受一個請求,此時tomcat起動的線程數沒有到達maxThreads,tomcat會起動一個線程來處理此請求。

情況2:接受一個請求,此時tomcat起動的線程數已經到達maxThreads,tomcat會把此請求放入等待隊列,等待空閑線程。

情況3:接受一個請求,此時tomcat起動的線程數已經到達maxThreads,等待隊列中的請求個數也達到了acceptCount,此時tomcat會直接拒絕此次請求,返回connection refused。

對於第3種情況,在看過一篇分析connection timeout問題產生的原因後,等待隊列的請求個數這個值可能是由acceptCount參數決定,也有可能由linux內核參數net.core.somaxconn決定。

關聯:我在網上看來一篇文章寫分析linux上TCP connection timeout的原因,這篇文章中提到一個內核參數 net.core.somaxconn。

我就想tomcat的acceptCount與net.core.somaxconn到底是什麽關系呢。

我做了一個實驗,

1. 我將tomcat的acceptCount設置為3000 ,net.core.somaxconn設置為8192

那麽我用ss -lt 指令查看在tomcat起的端口上的send_q值是3000 可見這是acceptCount的值。

2.我將tomcat的acceptCount設置為10000,net.core.somaxconn設置為8192

同樣用ss -lt指令查看在tomcat起的端口上的send_q值是8192,可見這是somaxconn的值。

所以,我總結的是,acceptCount設置的值要一般小於net.core.somaxconn這個參數,這樣acceptCount的值才會起作用。net.core.somaxconn 這個參數默認值是128 ,所以需要改這個參數值。後面再介紹改這個值的方法。

acceptCount如何配置?(轉)

我一般是設置的跟maxThreads一樣大,這個值應該是主要根據應用的訪問峰值與平均值來權衡配置的。

如果設的較小,可以保證接受的請求較快相應,但是超出的請求可能就直接被拒絕

如果設的較大,可能就會出現大量的請求超時的情況,因為我們系統的處理能力是一定的。

3. 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 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.

Tomcat允許的同時存在的最大連接數

acceptCount、maxConnections是tcp層相關的參數。

4.connectionTimeOut :connectionTimeOut=10000是說建立一個socket連接後,如果一直沒有收到客戶端的FIN,也沒有數據過來,那麽此連接也必須等到10s後,才能被超時釋放,我理解是tomcat就直接釋放這個連接。以毫秒為單位,server.xml默認設置是20秒。

官網: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).

修改方法:

vi server.xml 打開server.xml文件

<!--
 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 maxThreads="150" minSpareThreads="4"/>
 -->
修改為:
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1500" minSpareThreads="50" prestartminSpareThreads="true"/>


<Connector 
 port="8080" 
 protocol="HTTP/1.1" 
 connectionTimeout="20000" 
 redirectPort="8443" 
 />
修改為

<Connector executor ="tomcatThreadPool" port="8009" protocol="org.apache.coyote.http11.Http11Nio2Protocol" connectionTimeout="20000" maxConnections="10000" redirectPort="8443" acceptCount="1500"/>

下面的圖為TCP三次握手與accept交互

技術分享圖片

SYN隊列稱為半連接隊列,由內核參數 net.ipv4.tcp_max_syn_backlog 設置.

Accept隊列稱為完全連接隊列,三次握手已經完成,但還未被應用層接收(accept),但也處於ESTABLISHED狀態。隊列長度由listen的backlog參數和內核的 net.core.somaxconn 參數共同決定。由listen()函數的第二個參數 backlog 指定,內核硬限制由 net.core.somaxconn 限制,即隊列長度實際的值由min(backlog,somaxconn) 來決定

客戶端使用connect向服務器發送TCP連接,三次握手就發生了。當1.1步驟 客戶端首先發送SYN到達服務端後,內核會把連接信息放到SYN隊列中,同時回一個SYN+ACK包給客戶端。一段時間後,客戶端再次發來ACK包後,內核會把連接從SYN隊列中取出,再把這個連接放到ACCEPT隊列中。應用服務器調用accept時,其實就是直接從ACCEPT隊列中取出已經建立成功的連接套接字。

還有一張圖是TCP握手建立連接的流程和隊列

技術分享圖片

Tomcat原理概要

Tomcat大致分為兩個部分,Connector組件及Container組件。Connector組件負責控制入口連接,並關聯著一個Executor。Container負責Servlet容器的實現,Executor負責具體的業務邏輯,如Servlet的執行。一個請求到達服務器後,經過以下關鍵幾步,參見下圖:

技術分享圖片

  1. OS與客戶端握手並建立連接,並將建立的連接放入完成隊列,不妨叫Acceptor Queque。這個隊列的長度就是Connector的acceptCount值。

  2. Tomcat中的acceptor線程,不斷從Acceptor Queque中獲取連接。

  3. Acceptor Queque隊列中沒有連接,Acceptor線程繼續監視

  4. Acceptor Queque隊列中有新連接,Acceptor線程將檢查當前的連接數是否超過了maxConnections

  5. 如果超過maxConnections,則阻塞。直到連接數小於maxConnections,acceptor線程將請求交由Executor負責執行。

  6. Executor將分配worker線程來處理請求數據的讀取,處理(servlet的執行)以及響應。

    acceptCount

    acceptCount 實際上是Bind Socket時候傳遞的backlog值,在linux平臺下含義是已經建立連接還沒有被應用獲取的連接隊列最大長度。此時,如果請求個數達到了acceptCount,新進的請求將拋出refuse connection.

二、Linux內核參數優化

1. linux系統對當前用戶的單一進程同時可打開的文件數量的限制

查看系統允許當前用戶進程打開的文件數量的限制: ulimit -u 默認值為1024

對於想支持更高數量的TCP並發連接的通訊處理程序,就必須修改Linux對當前用戶的進程同時打開的文件數量的軟限制(soft limit)和硬限制(hardlimit)。其中軟限制是指Linux在當前系統能夠承受的範圍內進一步限制用戶同時打開的文件數;硬限制則是根據系統硬件資源狀況(主要是系統內存)計算出來的系統最多可同時打開的文件數量。通常軟限制小於或等於硬限制。

修改方法:

sudo vi /etc/security/limits.conf

增加如下:

prouser soft nofile 65536
prouser hard nofile 65536

修改完後保存此文件。

2.Linux網絡內核對TCP連接的有關限制

修改方法: sudo vi /etc/sysctl.conf 增加如下: net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 10000

net.core.somaxconn=8192 accept隊列的長度跟這個參數有關

sudo /sbin/sysctl -p
實時生效

三、JVM調優


JAVA_OPTS="$JAVA_OPTS -server -Xmn2000m -Xms4000m -Xmx4000m -XX:PermSize=128m -XX:+UseConcMarkSweepGC -XX:MaxPermSize=512m -Djuli-logback.configurationFile=file:$CATALINA_HOME/conf/logback.xml"

默認值:

<!--
 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 maxThreads="150" minSpareThreads="4"/>
 -->

修改為:

<Executor
 name="tomcatThreadPool"
 namePrefix="catalina-exec-"
 maxThreads="500"
 minSpareThreads="30"
 maxIdleTime="60000"
 prestartminSpareThreads = "true"
 maxQueueSize = "100"
/>

參數解釋:

maxThreads:最大並發數,默認設置 200,一般建議在 500 ~ 800,根據硬件設施和業務來判斷
minSpareThreads:Tomcat 初始化時創建的線程數,默認設置 25
maxIdleTime:如果當前線程大於初始化線程,那空閑線程存活的時間,單位毫秒,默認60000=60秒=1分鐘。
prestartminSpareThreads:在 Tomcat 初始化的時候就初始化 minSpareThreads 的參數值,如果不等於 true,minSpareThreads 的值就沒啥效果了
maxQueueSize:最大的等待隊列數,超過則拒絕請求

5.Connector 參數優化配置

默認值:

<Connector 
 port="8080" 
 protocol="HTTP/1.1" 
 connectionTimeout="20000" 
 redirectPort="8443" 
 />

修改為:

<Connector
 executor="tomcatThreadPool"
 port="8080"
 protocol="org.apache.coyote.http11.Http11Nio2Protocol"
 connectionTimeout="60000"
 maxConnections="10000"
 redirectPort="8443"
 enableLookups="false"
 acceptCount="100"
 maxPostSize="10485760"
 maxHttpHeaderSize="8192"
 compression="on"
 disableUploadTimeout="true"
 compressionMinSize="2048"
 acceptorThreadCount="2"
 compressableMimeType="text/html,text/plain,text/css,application/javascript,application/json,application/x-font-ttf,application/x-font-otf,image/svg+xml,image/jpeg,image/png,image/gif,audio/mpeg,video/mp4"
 URIEncoding="utf-8"
 processorCache="20000"
 tcpNoDelay="true"
 connectionLinger="5"
 server="Server Version 11.0"
 />

參數解釋:

protocol:Tomcat 8 設置 nio2 更好:org.apache.coyote.http11.Http11Nio2Protocol
protocol:Tomcat 6 設置 nio 更好:org.apache.coyote.http11.Http11NioProtocol
protocol:Tomcat 8 設置 APR 性能飛快:org.apache.coyote.http11.Http11AprProtocol 更多詳情:《Tomcat 8.5 基於 Apache Portable Runtime(APR)庫性能優化
connectionTimeout:Connector接受一個連接後等待的時間(milliseconds),默認值是60000。
maxConnections:這個值表示最多可以有多少個socket連接到tomcat上
enableLookups:禁用DNS查詢
acceptCount:當tomcat起動的線程數達到最大時,接受排隊的請求個數,默認值為100。
maxPostSize:設置由容器解析的URL參數的最大長度,-1(小於0)為禁用這個屬性,默認為2097152(2M) 請註意, FailedRequestFilter 過濾器可以用來拒絕達到了極限值的請求。
maxHttpHeaderSize:http請求頭信息的最大程度,超過此長度的部分不予處理。一般8K。
compression:是否啟用GZIP壓縮 on為啟用(文本數據壓縮) off為不啟用, force 壓縮所有數據
disableUploadTimeout:這個標誌允許servlet容器使用一個不同的,通常長在數據上傳連接超時。 如果不指定,這個屬性被設置為true,表示禁用該時間超時。
compressionMinSize:當超過最小數據大小才進行壓縮
acceptorThreadCount:用於接受連接的線程數量。增加這個值在多CPU的機器上,盡管你永遠不會真正需要超過2。 也有很多非維持連接,您可能希望增加這個值。默認值是1。
compressableMimeType:配置想壓縮的數據類型
URIEncoding:網站一般采用UTF-8作為默認編碼。
processorCache:協議處理器緩存的處理器對象來提高性能。 該設置決定多少這些對象的緩存。-1意味著無限的,默認是200。 如果不使用Servlet 3.0異步處理,默認是使用一樣的maxThreads設置。 如果使用Servlet 3.0異步處理,默認是使用大maxThreads和預期的並發請求的最大數量(同步和異步)。
tcpNoDelay:如果設置為true,TCP_NO_DELAY選項將被設置在服務器套接字,而在大多數情況下提高性能。這是默認設置為true。
connectionLinger:秒數在這個連接器將持續使用的套接字時關閉。默認值是 -1,禁用socket 延遲時間。
server:隱藏Tomcat版本信息,首先隱藏HTTP頭中的版本信息

6.隱藏或修改 Tomcat 版本號

 # cd /usr/local/tomcat/lib/
 # unzip catalina.jar
 # cd org/apache/catalina/util
 # vim ServerInfo.properties
 server.info=Apache Tomcat/8.5.16
 server.number=8.5.16.0
 server.built=Jun 21 2017 17:01:09 UTC

將以上去掉或修改版本號即可。

7.刪除禁用默認管理頁面以及相關配置文件

 # rm -rf /usr/local/apache-tomcat-8.5.16/webapps/*
 # rm -rf /usr/local/apache-tomcat-8.5.16/conf/tomcat-users.xml

參考內容:
https://tomcat.apache.org/tomcat-8.5-doc/config/
https://github.com/judasn/Linux-Tutorial/blob/master/Tomcat-Install-And-Settings.md
http://wiki.jikexueyuan.com/project/linux-in-eye-of-java/Tomcat-Install-And-Settings.html
http://netkiller.github.io/journal/tomcat.html
http://zjliu.me/2015/12/14/tomcat-config-connector/

Tomcat調優總結(Tomcat自身優化、Linux內核優化、JVM優化)