Tomcat官网参考文档

目录

Tomcat 官网

Tomcat 参数配置

Connectors

maxThreads 最大线程数

minSpareThreads 最小空闲线程

maxConnections 最大连接数

acceptCount 最大等待数

connectionTimeout 等待连接超时时间(不是长连接存活时间)

keepAliveTimeout 连接存活时间(针对长连接)

asyncTimeout 异步请求超时时间


Tomcat 官网

Apache Tomcat 8 (8.5.82) - Documentation Indexhttps://tomcat.apache.org/tomcat-8.5-doc/index.html

Tomcat 参数配置

Apache Tomcat 8 Configuration Reference (8.5.82) - Overviewhttps://tomcat.apache.org/tomcat-8.5-doc/config/index.htmlThis manual contains reference information about all of the configuration directives that can be included in a conf/server.xml file to configure the behavior of the Tomcat Servlet/JSP container. It does not attempt to describe which configuration directives should be used to perform specific tasks - for that, see the various How-To documents on the main index page.

Connectors

Connectors目录下有HTTP/1.1 和HTTP/2,用到那个协议版本就看对应的配置,里面包含线程、连接、超时设置等配置。

Each incoming, non-asynchronous request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests are received, Tomcat will accept new connections until the current number of connections reaches maxConnections. Connections are queued inside the server socket created by the Connector until a thread becomes avaialble to process the connection. Once maxConnections has been reached the operating system will queue further connections. The size of the operating system provided connection queue may be controlled by the acceptCount attribute. If the operating system queue fills, further connection requests may be refused or may time out.

收到请求时,如果用于处理请求的线程池中有空闲线程,则选择一个线程处理该请求,否则创建新线程直至达到最大线程数,若已达到最大线程数,则建立连接直至达到最大连接数,该连接会在服务器中排队等待一个可用的线程,如果达到最大连接数,则这些请求会被放到一个队列中等待连接,如果达到最大等待数,则返回拒绝或超时。

这些配置都是针对服务端的,比如A调B,B调C,调整B中的这些配置,起作用的是A调B,如连接超时时间,含义是B收到A连接请求时,等待建立连接的时间,超过这个时间,B就主动断开,建立连接超时,假如A通过resttemplate调用B,可以在A应用设置resttemplate的连接超时时间,如果A设置小于B,则A先主动超时,一般来说客户端A设置小于服务端B。

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

配置方式:server.tomcat.max-threads=200 默认200

最大线程数如果设置过大,导致线程切换消耗更多的CPU,维护线程需要更多的内存,反而降低处理请求的效率。

2 minSpareThreads 最小空闲线程数

The minimum number of threads always kept running. This includes both active and idle threads. If not specified, the default of 10 is used. 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.

配置方式:server.tomcat.min-spare-threads=30  默认10

应用启动后,线程池初始大小就是最小空闲线程数。最小空闲线程数如果设置过小,首次接入大量请求或请求降低后再突增时,需要消耗大量CPU创建新线程,可能出现请求堆积,响应慢或超时。设置过大,则系统一直保持高线程数运行,浪费资源,如果请求量一直保持高TPS,并且不能接受首次接入请求出现过慢情况,可以适当把最小空闲线程数设大一点,同时可以把最大线程数设小一点,甚至可以把两者设置成相同的值,最小空闲线程数=最大线程数的好处是,首次接入大量请求,也不会触发创建新的线程,不会消耗大量CPU创建新线程,过多的请求会建立连接等待空闲线程处理。

3 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 NIO and NIO2 the default is 10000. For APR/native, the default is 8192.

For NIO/NIO2 only, setting the value to -1, will disable the maxConnections feature and connections will not be counted.

配置方式:server.tomcat.max-connections=1000 默认10000

4 acceptCount 最大等待数

The maximum length of the operating system provided queue for incoming connection requests when maxConnections has been reached. The operating system may ignore this setting and use a different size for the queue. When this queue is full, the operating system may actively refuse additional connections or those connections may time out. The default value is 100

配置方式:server.tomcat.accept-count=100 默认100

5 connectionTimeout 等待连接超时时间(不是长连接存活时间)

The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to false, this timeout will also be used when reading the request body (if any).

配置方式:server.tomcat.connection-timeout=5000

6 keepAliveTimeout 连接存活时间(针对长连接)

The number of milliseconds this Connector will wait for another HTTP request before closing the connection. The default value is to use the value that has been set for the connectionTimeout attribute. Use a value of -1 to indicate no (i.e. infinite) timeout.

spring cloud服务间调用采用ribbon,默认是长连接,ribbon客户端默认连接存活时间比tomcat服务端大,当请求量非常小,tomcat服务端超过了连接存活时间主动断开,此时ribbon不知道连接已断开,复用该连接来发送请求,导致NoHttpResponse报错。

配置方式:server.tomcat.keep-alive-timeout=20000

7 asyncTimeout 异步请求超时时间

The default timeout for asynchronous requests in milliseconds. If not specified, this attribute is set to the Servlet specification default of 30000 (30 seconds).

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值