平常應用中,單臺Tomcat能支持最大的併發數是多少?html
做爲一個有經驗的Java Web開發人員對這個問題應該有大概的印象,並會讓問題再具體點,好比Tomcat版本,運行模式,併發請求容許的最大響應時間等,而後針對其中某個點搜索答案,而不該該低效的去直接搜這個答案。而且若是你沒相關知識,很容易被網上的知識誤導,由於不少都是很早以前配置的答案的轉載。apache
以如今主要用的Tomcat8爲例,想要徹底掌握配置,最好仍是去官網去瀏覽鎖定本身想知道的相關問題的關鍵詞。https://tomcat.apache.org/tomcat-8.5-doc/config/http.htmlsegmentfault
從上面配置也能夠看出Tomcat8在操做系統沒有裝arp庫支持時默認工做在NIO模式,默認支持的最大併發鏈接數是10000。tomcat
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 is10000
. For APR/native, the default is8192
.併發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.less
Sets the protocol to handle incoming traffic. The default value is
HTTP/1.1
which uses an auto-switching mechanism to select either a Java NIO based connector or an APR/native based connector. If thePATH
(Windows) orLD_LIBRARY_PATH
(on most unix systems) environment variables contain the Tomcat native library, and theAprLifecycleListener
that is used to initialize APR has itsuseAprConnector
attribute set totrue
, the APR/native connector will be used. If the native library cannot be found or the attribute is not configured, the Java NIO 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.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 documentationsocket
注意這個值是Tomcat默認接受的併發鏈接數,是TCP鏈接層相關的參數。Tomcat在NIO模式時有一個線程專業接受請求鏈接,而後將其放到任務隊列,而後有工做線程從任務t隊列取出請求並併發處理(工做線程數經過maxThreads值控制,Tomcat默認是200),若是每一個請求處理很快好比20ms,則工做線程1s內就能處理10000個請求,不然若請求處理很慢好比要幾秒,則請求隊列中的鏈接獲得處理收到響應的時間也會變慢。tcp
maxThreads、minSpareThreads是tomcat工做線程池的配置參數,maxThreads就至關於jdk線程池的maxPoolSize,而minSpareThreads就至關於jdk線程池的corePoolSize。ide
acceptCount、maxConnections是tcp層相關的參數。this
tomcat有一個acceptor線程來accept socket鏈接,而後有工做線程來進行業務處理。對於client端的一個請求進來,流程是這樣的:tcp的三次握手創建鏈接,創建鏈接的過程當中,OS維護了半鏈接隊列(syn隊列)以及徹底鏈接隊列(accept隊列),在第三次握手以後,server收到了client的ack,則進入establish的狀態,而後該鏈接由syn隊列移動到accept隊列。tomcat的acceptor線程則負責從accept隊列中取出該connection,接受該connection,而後交給工做線程去處理(
讀取請求參數、處理邏輯、返回響應等等;若是該鏈接不是keep alived的話,則關閉該鏈接,而後該工做線程釋放回線程池,若是是keep alived的話,則等待下一個數據包的到來直到keepAliveTimeout,而後關閉該鏈接釋放回線程池
),而後本身接着去accept隊列取connection(噹噹前socket鏈接超過maxConnections的時候,acceptor線程本身會阻塞等待,等鏈接降下去以後,纔去處理accept隊列的下一個鏈接
)。acceptCount指的就是這個accept隊列的大小。https://segmentfault.com/a/1190000008064162