SpringBoot’s built-in Tomcat, in the default settings, the maximum number of threads of Tomcat is 200, and the maximum number of connections It's 10,000. By default, the maximum concurrency supported is 10,000, which means the number of connections supported
1, which is BIO, and one thread only processes one Socket Connection,
2, is NIO, one thread handles multiple Socket connections.
A single thread handling multiple connections usually does not cause too much of a problem because HTTP requests are not too time-consuming and multiple connections usually do not send messages at the same time. A thread processing multiple connections will be very slow and may time out
server.tomcat.accept-count
: Waiting queue length. When the number of allocable threads is used up, subsequent requests will enter the waiting queue to wait. When the waiting queue is full, processing will be refused. The default is 100.
server.tomcat.max-connections
: Maximum number of connections, default 10000
server .tomcat.max-threads
: Maximum number of working threads, default 200,
server.tomcat.min-spare-threads
: Minimum number of working threads , initialize the number of allocated threads, the default is 10
Under the default configuration, the connection will be refused after the number of connections exceeds 10,000
Under the default configuration, the number of triggered requests exceeds 200. After 100 Rejection processing (maximum number of worker threads and waiting queue length)
If the default configuration cannot meet the current needs, you can tune it yourself and manually modify the configuration for concurrent processing
Modify the pro file
server.port=7001 server.tomcat.accept-count=1000 server.tomcat.max-connections=10000 server.tomcat.max-threads=500 server.tomcat.min-spare-threads=100
Then package and restart the project
kill -9 9545 //Kill the process
ps -ef | grep java //View the port where the project is started
pstree -p 7968 | wc -l //View the number of processes
We can also use the configuration file and add the WebServerConfiguration.java file
import org.apache.catalina.connector.Connector; import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; //当Spring容器内没有TomcatEmbeddedServletContainerFactory这个bean时,会吧此bean加载进spring容器中 @Component public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory configurableWebServerFactory) { //使用对应工厂类提供给我们的接口定制化我们的tomcat connector ((TomcatServletWebServerFactory)configurableWebServerFactory).addConnectorCustomizers(new TomcatConnectorCustomizer() { @Override public void customize(Connector connector) { Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); //定制化keepalivetimeout,设置30秒内没有请求则服务端自动断开keepalive链接 protocol.setKeepAliveTimeout(30000); //当客户端发送超过10000个请求则自动断开keepalive链接 protocol.setMaxKeepAliveRequests(10000); } }); } }
Remarks:
MySQL database QPS capacity problem:
Primary key query: tens of millions of data == 1-10 ms
Unique index query: Ten million level data == 10-100 ms
Non-unique index query: Ten million level data == 100-1000ms
No index data: million level data == 1000ms
MySQL database TPS capacity problem:
Non-insert update and delete operation: Same query
Insertion operation: 1w~10w tps (depends on configuration optimization)
1. Apache Tomcat is combined to use Apache for static pages and Tomcat for dynamic pages. At the same time, the connectionTimeout time is reduced to cope with the situation of large concurrency and too late for thread recycling.
2. For the problem of excessive pressure, load balancing can be done. A TOMCAT cannot handle so many thread loads anyway, and if the JVM is too large, its memory management cost will increase significantly. A more reasonable and scientific approach is to use 2GB of memory and configure 3 to 4 TOMCAT instances, each with a memory of 512MB.
3. Database connection pool. Many people recommend using C3P0, which can improve the concurrent performance of database access several times.
4. The use of Tomcat cluster can maximize the performance of the server. You can deploy multiple Tomcats on servers with higher configurations, or you can deploy Tomcat on multiple servers separately, and integrate Apache and Tomcat. Still the JK way. It has been verified that in terms of the system's response to a large number of users, Apache 3Tomccat cluster > Apache 2Tomcat cluster > Apache integrated Tomcat > single Tomcat. And when using the Apache multi-Tomcat cluster deployment method, if one Tomcat goes down, the system can continue to be used. Therefore, when the performance of the hardware system is superior enough, you need to maximize the performance of the software, and you can add a Tomcat cluster.
1. Configure MPM (Multi Processing Modules). ThreadPerChild, this parameter is used to set the number of threads for each process. In Windows environment, the default value is 64 and the maximum value is 1920. It is recommended to set it between 100-500. If the server performance is high, the value will be larger, otherwise it will be smaller. The maximum number of requests each child process can handle is determined by the MaxRequestPerChild parameter. The value of this parameter depends more on the memory of the server. If the memory is relatively large, it can be set to a large parameter. Otherwise, set a smaller value. The recommended value is 3000.
2. Turn off DNS and name resolution HostnameLookups off
3. Turn on the UseCanonicalName module UseCanonicalName on
4. Turn off redundant modules. Generally speaking, the modules that do not need to be loaded include mod_include.so, mod_autoindex.so, and mod_access. so, mod_auth.so.
5. Turn on KeepAlive support
KeepAlive on, KeepAliveTimeout 15 MaxKeepAliveRequests 1000
Based on actual experience, improve system performance through Apache and Tomcat clusters The effect is very obvious. This method can maximize the use of hardware resources and share the pressure of a single Tomcat through the processing of multiple Tomcats.
The maximum number of connections allowed by the web server is also subject to the kernel parameter settings of the operating system. Usually it is about 2,000 for Windows and about 1,000 for Linux.
The above is the detailed content of How to solve the problem of maximum concurrency supported by springboot's built-in tomcat. For more information, please follow other related articles on the PHP Chinese website!