1. 程式人生 > >Tomcat 7 相關引數優化說明及配置最佳案例

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了: