tomcat 7 连接数和线程数配置

引言

这段时间折腾了哈 java web 应用的压力测试,部署容器是tomcat 7。期间学到了蛮多散碎的知识点,及时梳理总结,构建良好且易理解的知识架构把它们组织起来,以备忘。
对 web 应用开发者来说,很关心应用可同时处理的请求数,以及响应时间。应用本身和运行它的 web 容器是两个很重要的影响因素。

对 tomcat 来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。如果同时进来的请求多于当前可用的请求处理线程数,额外的线程就会被创建,直到到达配置的最大线程数(maxThreads 属性值)。如果仍就接收到更多请求,这些来不及处理的请求就会在 Connector 创建的 Server Socket 中堆积起来,直到到达最大的配置值(acceptCount 属性值)。至此,任何再来的请求将会收到 connection refused 错误,直到有可用的资源来处理它们。

分析影响应用性能的配置项

这里我们关心的是 tomcat 能同时处理的请求数和请求响应时间,显然 Connector 元素的 maxThreads 和 acceptCount 属性对其有直接的影响。无论 acceptCount 值为多少,maxThreads 直接决定了实际可同时处理的请求数。而不管 maxThreads 如何,acceptCount 则决定了可以有多少请求等待处理。然而,不管是可立即处理请求还是需要放入等待区,都需要 tomcat 先接受该请求(即接受client的连接请求,建立socket channel),那么 tomcat 同时可建立的连接数(maxConnections属性值)也会影响可同时处理的请求数。

我们可以把 tomcat 想象成一家医院,你来到医院大厅挂号看病,如果人家接受了,就相当于 client 和 server 建立 socket 连接了。接着你来到相应的科室,科室里每位医生都有一间诊室,这就相当于处理请求的线程;如果所有诊室都有病人,科室的调度护士会让你在科室小厅中耐心等待,直到他们通知你去几号诊室就诊;如果有空闲医生,你就可以立即就诊。

有的病人到医院很仓促,轮到他挂号或者就诊时,还在包里翻找病例本和医保卡。如果超过了护士或医生可承受的等待时间,他们就会让病人到旁边找去,先服务下一位。这种情形跟 Connector 元素的 connectionTimeout 属性所起的作用很相像。如果连接器(Connector)在接受连接后,等待了指定的时间但仍未接收到 request URI line,就会抛出超时异常。

详述各个配置项

tomcat 7 的配置参考文档对相关属性已经描述的很详细了,这里把它们收集到一起:

protocol

Sets the protocol to handle incoming traffic. The default value is HTTP/1.1 which uses an auto-switching mechanism to select either a blocking Java based connector or an APR/native based connector. If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. If the native library cannot be found, the blocking Java 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.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector
org.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

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.

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.

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

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

http 连接器

tomcat 的 http connector 有三种:bio、nio、apr。从前面的描述中可以看出对于不同的 connector 实现,相同的属性可能会有不同的默认值和不同的处理策略,所以在调整配置前,要先弄清楚各种实现之间的不同,以及当前部署容器使用的是哪种 connector。

查阅 Tomcat 7 http connector config reference Connector Comparison 部分便可获知各种 connector 实现间的差异。

如何知道容器使用的是何种 connector 实现?启动 tomcat 后,访问 Server Status Page,看到如下信息即可知道使用的是何种 connector:

我是 windows,所以 tomcat 默认使用的是 apr connector。在linux上,默认使用的是 bio connector。与 nio 相比,bio 性能较差。将 <TOMCAT_HOME>/conf/server.xml 中的如下配置片段:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

修改为:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" />

就可将 http connector 切换至 nio 了。更多细节请参考 修改 Tomcat Connector 运行模式,优化 Tomcat 运行性能

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容