对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。
这段时间折腾了哈java web应用的压力测试,部署容器是tomcat 7。期间学到了蛮多散碎的知识点,及时梳理总结,构建良好且易理解的知识架构把它们组织起来,以备忘。
对web应用开发者来说,我们很关心应用可同时处理的请求数,以及响应时间。应用本身和它运行在其中的web容器是两个很重要的影响因素。
对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。如果同时进来的请求多于当前可用的请求处理线程数,额外的线程就会被创建,直到到达配置的最大线程数(maxThreads属性值)。如果仍就同时接收到更多请求,这些来不及处理的请求就会在Connector创建的ServerSocket中堆积起来,直到到达最大的配置值(acceptCount属性值)。至此,任何再来的请求将会收到connection refused错误,直到有可用的资源来处理它们。
分析、梳理、组织
这里我们关心的是tomcat能同时处理的请求数和请求响应时间,显然Connector元素的maxThreads和acceptCount属性对其有直接的影响。无论acceptCount值为多少,maxThreads直接决定了实际可同时处理的请求数。而不管maxThreads如何,acceptCount则决定了有多少请求可等待处理。然而,不管是可立即处理请求还是需要放入等待区,都需要tomcat先接受该请求(即接受client的连接请求,建立socketchannel),那么tomcat同时可建立的连接数(maxConnections属性值)也会影响可同时处理的请求数。
我们可把tomcat想象成一家医院,你来到医院大厅挂号看病,如果人家接受了,就相当于client和server建立socket连接了。接着你来到相应的科室,科室里每位医生都有一间诊室,这就相当于处理请求的线程;如果所有诊室都有病人,科室的调度护士会让你在科室小厅中耐心等待,直到他们通知你去几号诊室就诊;如果有空闲医生,你就可以立即就诊。
有的病人到医院很仓促,结果轮到他挂号或者就诊了,他还在包里翻找病例本和医保卡,如果超过了护士或医生心里可承受的等待时间,他们就会让病人到旁边找去,先服务下位。这种情形跟Connector元素的connectionTimeout属性所起的作用很相像。如果当前连接器(Connector)在接受连接后,等待了指定的时间但仍未接收到requestURI line,就会抛出超时异常。
知识点收集
tomcat 7 的配置参考文档对相关属性已经描述的很详细了,这里把它们收集到一起:
protocol
Sets the protocol to handle incoming traffic. The default valueis HTTP/1.1 which uses an auto-switching mechanism to select either a blockingJava based connector or an APR/native based connector. If the PATH (Windows) orLD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcatnative library, the APR/native connector will be used. If the native librarycannot be found, the blocking Java based connector will be used. Note that theAPR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanismdescribed 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 Javaconnectors is identical, for http and https.
For more information on the APR connector and APR specific SSL settings pleasevisit the APR documentation
maxThreads
The maximum number of request processing threads to be createdby this Connector, which therefore determines the maximum number ofsimultaneous requests that can be handled. If not specified, this attribute isset to 200. If an executor is associated with this connector, this attribute isignored as the connector will execute tasks using the executor rather than aninternal thread pool.
acceptCount
The maximum queue length for incoming connection requests whenall possible request processing threads are in use. Any requests received whenthe queue is full will be refused. The default value is 100.
maxConnections
The maximum number of connections that the server will acceptand process at any given time. When this number has beenreached, the server will accept, but not process, one further connection. Thisadditional connection be blocked until the number of connections beingprocessed falls below maxConnections at which point the server will startaccepting and processing new connections again. Note that once the limit hasbeen reached, the operating system may still accept connections based on theacceptCount setting. Thedefault value varies by connector type. For BIO the default is the value of maxThreads unless an Executor isused in which case the default will be the value of maxThreads from theexecutor. 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 tothe highest multiple of 1024 that is less than or equal to maxConnections. Thisis done for performance reasons.
If set to a value of -1, the maxConnections feature is disabled and connectionsare not counted.
connectionTimeout
The number of milliseconds this Connector will wait,after accepting a connection, for the request URI lineto 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 thatthe standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20seconds). Unless disableUploadTimeout is set to false, this timeout willalso be used when reading the request body (if any).
进一步分析
tomcat的http connector有三种:bio、nio、apr。从上面的属性描述中可以看出对于不同的connector实现,相同的属性可能会有不同的默认值和不同的处理策略,所以在调整配置前,要先弄清楚各种实现之间的不同,以及当前部署容器使用的是哪种connector。
查阅Tomcat7 http connector 配置文档Connector Comparison部分便可获知各种connector实现间的差异。
怎样才能知道容器使用的是何种connector实现?启动tomcat后,访问Server Status Page,看到如下信息即可知道使用的是何种connector:
我的OS是windows,所以tomcat默认使用的是aprconnector。在linux上,默认使用的是bio connector。与nio相比,bio性能较低。将<TOMCAT_HOME>/conf/server.xml中的如下配置片段:
<Connectorport="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改为:
<Connectorport="8080"protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />
就可将http connector切换至nio了。更多细节请参考修改Tomcat Connector运行模式,优化Tomcat运行性能
官网说明:http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
一、最大线程数的设置
Tomcat的server.xml中连接器设置如下
<Connectorport="8080" maxThreads="150"minSpareThreads="25" maxSpareThreads="75" enableLookups="false"redirectPort="8443" acceptCount="100" debug="0"connectionTimeout="20000" disableUploadTimeout="true"/>
tomcat在配置时设置最大线程数,当前线程数超过这个数值时会出错,那么有没有办法捕获到这个错误,从而在client端显示出错信息?
2. 如何加大tomcat连接数
在tomcat配置文件server.xml中的<Connector />配置中,和连接数相关的参数有:
minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75
acceptCount:允许的最大连接数,应大于等于maxProcessors,默认值为100
enableLookups:是否反查域名,取值为:true或false。为了提高处理能力,应设置为false
connectionTimeout:网络连接超时,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。
其中和最大连接数相关的参数为maxProcessors和acceptCount。如果要加大并发连接数,应同时加大这两个参数。
web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右。tomcat5中的配置示例:
<Connector port="8080"
maxThreads="150" minSpareThreads="25"maxSpareThreads="75"
enableLookups="false" redirectPort="8443"acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
对于其他端口的侦听配置,以此类推。
3. tomcat中如何禁止列目录下的文件
在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下:
1. <init-param>
2. <param-name>listings</param-name>
3. <param-value>false</param-value>
4. </init-param>
4.如何加大tomcat可以使用的内存
tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大。
Unix下,在文件{tomcat_home}/bin/catalina.sh的前面,增加如下设置:
JAVA_OPTS='-Xms【初始化内存大小】 -Xmx【可以使用的最大内存】'
需要把这个两个参数值调大。例如:
JAVA_OPTS='-Xms256m -Xmx512m'
表示初始化内存为256MB,可以使用的最大内存为512MB
对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。的更多相关文章
- java的服务是每收到一个请求就新开一个线程来处理吗?tomcat呢?
首先,服务器的实现不止有这两种方式. 先谈谈题主说的这两种服务器模型: 1.收到一个请求就处理,这个时候就不能处理新的请求,这种为阻塞 这个是单线程模型,无法并发,一个请求没处理完服务器就会阻塞,不会 ...
- 一个请求过来都经过了什么?(Thrift版)
一.背景 最初遇到这个问题是去58面试.部门领导是原同事,所以面试比较水.水到什么程度呢? 面试就是走个形式而已,不会不过的. 一面面试官就问了一个问题:“一个请求过来都经过了什么?” 剩下的全是闲 ...
- 使用PHP文件锁写一个多个请求同时并发写入一个文件,要求不脏读、数据不丢失
使用PHP文件锁写一个多个请求同时并发写入一个文件,要求不脏读.数据不丢失. //并发文件操作 function filehandle($filename,$data){ $start = 0; $e ...
- 每次收到的 HTTP 请求,就可以打开一个 SqlSession,返回一个响应,就关闭它
mybatis – MyBatis 3 | 入门 http://www.mybatis.org/mybatis-3/zh/getting-started.html 作用域(Scope)和生命周期 理解 ...
- 从零开始一个http服务器(二)-请求request解析
从零开始一个http服务器 (二) 代码地址 : https://github.com/flamedancer/cserver git checkout step2 解析http request 观察 ...
- C# FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址。"的错误
FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址."的错误 解决方法是在原代码上增加这句话 reqFTP.UsePassive = f ...
- 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。
http://zhengxinlong.iteye.com/blog/848712 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name& ...
- 语义分割(semantic segmentation) 常用神经网络介绍对比-FCN SegNet U-net DeconvNet,语义分割,简单来说就是给定一张图片,对图片中的每一个像素点进行分类;目标检测只有两类,目标和非目标,就是在一张图片中找到并用box标注出所有的目标.
from:https://blog.csdn.net/u012931582/article/details/70314859 2017年04月21日 14:54:10 阅读数:4369 前言 在这里, ...
- eclipse需要的环境变量就两个,一个是java_home指向JDK。另一个是Tomcat,自己去preference-sever下new一个
1.eclipse需要的环境变量就两个,一个是java_home指向JDK.另一个是Tomcat,自己去preference-sever下new一个
随机推荐
- C#中获得汉字的首拼音(加强版)
/// <summary> /// 汉字拼音首字母列表 /// 包含了20901个汉字,收录的字符的Unicode编码范围为19968至40869 /// </summary> ...
- task_struct
Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; ...
- OC - 正则表达式 - RegexKitLite
正则表达式使用步骤: 1. 创建正则表达式对象, 设置约束条件; NSString *pattern = @"\\d{1,3}"; NSRegularExpression *reg ...
- [转]JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解
[转] JAVA环境变量JAVA_HOME.CLASSPATH.PATH设置详解 - dreamman的日志 - 网易博客http://blog.163.com/dreamman_yx/blog/st ...
- 小Z的创业经历 谢谢支持
写这篇文章的目的是跟大家分享下创业的一些想法,经历.希望对你有所帮助或有所思考. 我想用6篇文章介绍下前期创业经历 1.怎么创业了? 2.万事开头难,怎么开始呢? 3.我们的系统详情(上) 4.我们 ...
- bzoj 1070: [SCOI2007]修车 费用流
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2785 Solved: 1110[Submit][Status] ...
- Word添加带圈文字
这个在项目有编号李没有,只能一个一个输入 A.开始------------字体里选择带圈的字符号 B.插入,符号里选编号
- 【技术贴】webservice 调用 Transport error : 401 Error:Una
解决 webservice 调用之后报错:调用异常:Transport error : 401 Error:Unauthorized 授权失败. 加入如下代码 //Sap需要ws-security的认 ...
- Essential Sublime Text Plugins
Essential Sublime Text Plugins Add some killer tools to your arsenal. View them all at /repo/sublime ...
- [ecmall]Ecmall 后台添加模板编辑区
例如,想把品牌/index.php?app=brand页面做成可编辑的. 首先,找到后台admin\includes\menu.inc.php第61行 'template' => array( ...