- 問題
SpringBootの組み込みTomcatを構成して、サービスをより効率的にする方法は?
- 基本構成
Spring Bootがサポートできる同時実行の最大量は、主にそのTomcat設定に依存します。これは、構成ファイルで変更できます。デフォルト設定では、Tomcatのスレッドの最大数は200であり、接続の最大数は10,000であることがわかります。この異なるSpringBootバージョンはわずかに異なる場合があります。この記事のテストは、Springboot2.0.7.RELEASEに基づいています。
- デフォルトの割り当て
/**
* Maximum amount of worker threads.
*/
private int maxThreads = 200;
/**
* Minimum amount of worker threads.
*/
private int minSpareThreads = 10;
/**
* Maximum size in bytes of the HTTP post content.
*/
private int maxHttpPostSize = 2097152;
/**
* Maximum size in bytes of the HTTP message header.
*/
private int maxHttpHeaderSize = 0;
/**
* Whether requests to the context root should be redirected by appending a / to
* the path.
*/
private Boolean redirectContextRoot = true;
/**
* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
* will use relative or absolute redirects.
*/
private Boolean useRelativeRedirects;
/**
* Character encoding to use to decode the URI.
*/
private Charset uriEncoding = StandardCharsets.UTF_8;
/**
* Maximum number of connections that the server accepts and processes at any
* given time. Once the limit has been reached, the operating system may still
* accept connections based on the "acceptCount" property.
*/
private int maxConnections = 10000;
/**
* Maximum queue length for incoming connection requests when all possible request
* processing threads are in use.
*/
private int acceptCount = 100;
- テスト手順
ソースコードを見ると、(org.springframework.boot.autoconfigure.web.ServerProperties)springBootにTomcatのデフォルト構成が組み込まれていることがわかります。ここで、効果をローカルに反映するために、構成パラメーターを意図的に調整します。圧力テストの場合は次のようになりますが、同時に、ストレステストインターフェイスで設定されたsleep(2000)シミュレーションスレッドは解放されません。
tomcat:
#最小线程数
min-spare-threads: 5
#最大线程数
max-threads: 5
#最大链接数
max-connections: 5
#最大等待队列长度
accept-count: 1
- この構成は圧力テストに対応します
100の同時実行性のストレステストを通じて、異常が85%に達したことがわかりました。ReadTimeoutとConnectTimeoutを構成して、2秒で同時に100のスレッドに到達するように構成したため、処理の最大スレッドは1のみで、キューはまた1.その結果、リクエストを処理するスレッドがないため、タイムアウトが発生し、もう一方はキューに入れられません。拒否しないでください。ローカルマシンに応じてカップを適切に構成した後、圧力テストの状況を確認します。最適化された構成は次のとおりです。
tomcat:
#最小线程数
min-spare-threads: 100
#最大线程数
max-threads: 600
#最大链接数
max-connections: 10000
#最大等待队列长度
accept-count: 1000
上の図に示すように、同時例外率100も0であり、すべて合格であり、応答時間もマイナスsleep(2000)であり、そのほとんどは10ミリ秒以内です。最適化効果は明らかです。