日常應用中,單台Tomcat能支持最大的並發數是多少?
作為一個有經驗的Java Web開發人員對這個問題應該有大概的印象,並會讓問題再具體點,比如Tomcat版本,運行模式,並發請求允許的最大響應時間等,然后針對其中某個點搜索答案,而不應該低效的去直接搜這個答案。並且如果你沒相關知識,很容易被網上的知識誤導,因為很多都是很早之前配置的答案的轉載。
以現在主要用的Tomcat8為例,想要完全掌握配置,最好還是去官網去瀏覽鎖定自己想知道的相關問題的關鍵詞。https://tomcat.apache.org/tomcat-8.5-doc/config/http.html
從上面配置也可以看出Tomcat8在操作系統沒有裝arp庫支持時默認工作在NIO模式,默認支持的最大並發連接數是10000。
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.
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 documentation
注意這個值是Tomcat默認接受的並發連接數,是TCP連接層相關的參數。Tomcat在NIO模式時有一個線程專業接受請求連接,然后將其放到任務隊列,然后有工作線程從任務t隊列取出請求並並發處理(工作線程數通過maxThreads值控制,Tomcat默認是200),如果每個請求處理很快比如20ms,則工作線程1s內就能處理10000個請求,否則若請求處理很慢比如要幾秒,則請求隊列中的連接得到處理收到響應的時間也會變慢。
maxThreads、minSpareThreads是tomcat工作線程池的配置參數,maxThreads就相當於jdk線程池的maxPoolSize,而minSpareThreads就相當於jdk線程池的corePoolSize。
acceptCount、maxConnections是tcp層相關的參數。
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