tomcat 的acceptCount、acceptorThreadCount、maxConnections、maxThreads 如何确定

tomcat 的acceptCount、acceptorThreadCount、maxConnections、maxThreads 如何确定

acceptCount

连接在被ServerSocketChannel accept之前就暂存在这个队列中,acceptCount就是这个队列的最大长度。

ServerSocketChannel accept就是从这个队列中不断取出已经建立连接的的请求。

acceptorThreadCount

使用的Acceptor线程的个数,tomcat设置为1

Acceptor线程只负责从上述队列中取出已经建立连接的请求。

maxConnections

这里就是tomcat对于连接数的一个控制,即最大连接数限制。一旦发现当前连接数已经超过了一定的数量(NIO默认是10000),上述的Acceptor线程就被阻塞了,

即不再执行ServerSocketChannel的accept方法从队列中获取已经建立的连接。但是它并不阻止新的连接的建立,新的连接的建立过程不是Acceptor控制的

maxThread
业务线程数,默认是200

tomcat 的acceptCount、acceptorThreadCount、maxConnections、maxThreads 如何确定

1. 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 an Executor 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.

2. 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.

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.

3. 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.

tomcat默认设置

maxConnections表示有多少个socket连接到tomcat上。NIO模式下默认是10000。

maxThreads:tomcat起动的最大线程数,即同时处理的任务个数,默认值为200

acceptCount:当tomcat起动的线程数达到最大时,接受排队的请求个数,默认值为100

tomcat能支持最大连接数由maxConnections加上acceptCount来决定。同时maxThreads如何设定?

1. cpu bound:  maxThreads应该尽量设的小,降低同一时间内争抢cpu的线程个数,可以提高计算效率,提高系统的整体处理能力。

2. io bound:   maxThreads应该尽量设的大,这样 才能提高同时处理请求的个数,从而提高系统整体的处理能力。

此情况下因为tomcat同时处理的请求量会比较大,所以需要关注一下tomcat的虚拟机内存设置和linux的open file限制。

现实应用中,我们的操作都会包含以上两种类型(计算、等待),所以maxThreads的配置并没有一个最优值,一定要根据具体情况来配置。

最好的做法是:在不断测试的基础上,不断调整、优化,才能得到最合理的配置。

4. spring 设置

server.tomcat.max-threads=1024
server.tomcat.max-connections=2048

server.tomcat.accept-count=100

或
@Configuration
public class WebServerConfiguration {
@Bean
public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
tomcatFactory.setPort(8081);
tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
return tomcatFactory;
}
}
class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {
public void customize(Connector connector) {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
//设置最大连接数
protocol.setMaxConnections(2000);
//设置最大线程数
protocol.setMaxThreads(2000);
protocol.setConnectionTimeout(30000);
}
}

参考:

segmentfault: tomcat的acceptCount与maxConnections

spring: howto-embedded-servlet-containers

noodles: 对 Linux TCP 的若干疑点和误会

helloDog: sysctl.conf学习和调优

Linux Tcp 参数调优

高性能网络编程7--tcp连接的内存使用

写点什么:tomcat-connector的微调(1): acceptCount参数

上一篇:linux -目录结构


下一篇:hdu 4559 涂色游戏 博弈论