{{ v.name }}
{{ v.cls }}类
{{ v.price }} ¥{{ v.price }}
引言
这段时间折腾了哈javaweb应用的压力测试,部署容器是tomcat7。期间学到了蛮多散碎的知识点,及时梳理总结,构建良好且易理解的知识架构把它们组织起来,以备忘。对web应用开发者来说,我们很关心应用可同时处理的请求数,以及响应时间。应用本身和它运行在其中的web容器是两个很重要的影响因素。对tomcat来说,每一个进来的请求(request)都需要一个线程,直到该请求结束。如果同时进来的请求多于当前可用的请求处理线程数,额外的线程就会被创建,直到到达配置的最大线程数(maxthreads属性值)。如果仍就同时接收到更多请求,这些来不及处理的请求就会在connector创建的serversocket中堆积起来,直到到达最大的配置值(acceptcount属性值)。至此,任何再来的请求将会收到connectionrefused错误,直到有可用的资源来处理它们。
这里我们关心的是tomcat能同时处理的请求数和请求响应时间,显然connector元素的maxthreads和acceptcount属性对其有直接的影响。无论acceptcount值为多少,maxthreads直接决定了实际可同时处理的请求数。而不管maxthreads如何,acceptcount则决定了有多少请求可等待处理。然而,不管是可立即处理请求还是需要放入等待区,都需要tomcat先接受该请求(即接受client的连接请求,建立socketchannel),那么tomcat同时可建立的连接数(maxconnections属性值)也会影响可同时处理的请求数。我们可把tomcat想象成一家医院,你来到医院大厅挂号看病,如果人家接受了,就相当于client和server建立socket连接了。接着你来到相应的科室,科室里每位医生都有一间诊室,这就相当于处理请求的线程;如果所有诊室都有病人,科室的调度护士会让你在科室小厅中耐心等待,直到他们通知你去几号诊室就诊;如果有空闲医生,你就可以立即就诊。有的病人到医院很仓促,结果轮到他挂号或者就诊了,他还在包里翻找病例本和医保卡,如果超过了护士或医生心里可承受的等待时间,他们就会让病人到旁边找去,先服务下位。这种情形跟connector元素的connectiontimeout属性所起的作用很相像。如果当前连接器(connector)在接受连接后,等待了指定的时间但仍未接收到requesturiline,就会抛出超时异常。
tomcat7的配置参考文档对相关属性已经描述的很详细了,这里把它们收集到一起:
protocol
setstheprotocoltohandleincomingtraffic.thedefaultvalueishttp/1.1whichusesanauto-switchingmechanismtoselecteitherablockingjavabasedconnectororanapr/nativebasedconnector.ifthepath(windows)orld_library_path(onmostunixsystems)environmentvariablescontainthetomcatnativelibrary,theapr/nativeconnectorwillbeused.ifthenativelibrarycannotbefound,theblockingjavabasedconnectorwillbeused.notethattheapr/nativeconnectorhasdifferentsettingsforhttpsthanthejavaconnectors.touseanexplicitprotocolratherthanrelyontheauto-switchingmechanismdescribedabove,thefollowingvaluesmaybeused:org.apache.coyote.http11.http11protocol-blockingjavaconnectororg.apache.coyote.http11.http11nioprotocol-nonblockingjavaconnectororg.apache.coyote.http11.http11aprprotocol-theapr/nativeconnector.customimplementationsmayalsobeused.takealookatourconnectorcomparisonchart.theconfigurationforbothjavaconnectorsisidentical,forhttpandhttps.formoreinformationontheaprconnectorandaprspecificsslsettingspleasevisittheaprdocumentation
maxthreads
themaximumnumberofrequestprocessingthreadstobecreatedbythisconnector,whichthereforedeterminesthemaximumnumberofsimultaneousrequeststhatcanbehandled.ifnotspecified,thisattributeissetto200.ifanexecutorisassociatedwiththisconnector,thisattributeisignoredastheconnectorwillexecutetasksusingtheexecutorratherthananinternalthreadpool.
acceptcount
themaximumqueuelengthforincomingconnectionrequestswhenallpossiblerequestprocessingthreadsareinuse.anyrequestsreceivedwhenthequeueisfullwillberefused.thedefaultvalueis100.
maxconnections
themaximumnumberofconnectionsthattheserverwillacceptandprocessatanygiventime.whenthisnumberhasbeenreached,theserverwillaccept,butnotprocess,onefurtherconnection.thisadditionalconnectionbeblockeduntilthenumberofconnectionsbeingprocessedfallsbelowmaxconnectionsatwhichpointtheserverwillstartacceptingandprocessingnewconnectionsagain.notethatoncethelimithasbeenreached,theoperatingsystemmaystillacceptconnectionsbasedontheacceptcountsetting.thedefaultvaluevariesbyconnectortype.forbiothedefaultisthevalueofmaxthreadsunlessanexecutorisusedinwhichcasethedefaultwillbethevalueofmaxthreadsfromtheexecutor.forniothedefaultis10000.forapr/native,thedefaultis8192.notethatforapr/nativeonwindows,theconfiguredvaluewillbereducedtothehighestmultipleof1024thatislessthanorequaltomaxconnections.thisisdoneforperformancereasons.ifsettoavalueof-1,themaxconnectionsfeatureisdisabledandconnectionsarenotcounted.
connectiontimeout
thenumberofmillisecondsthisconnectorwillwait,afteracceptingaconnection,fortherequesturilinetobepresented.useavalueof-1toindicateno(i.e.infinite)timeout.thedefaultvalueis60000(i.e.60seconds)butnotethatthestandardserver.xmlthatshipswithtomcatsetsthisto20000(i.e.20seconds).unlessdisableuploadtimeoutissettofalse,thistimeoutwillalsobeusedwhenreadingtherequestbody(ifany).
tomcat的httpconnector有三种:bio、nio、apr。从上面的属性描述中可以看出对于不同的connector实现,相同的属性可能会有不同的默认值和不同的处理策略,所以在调整配置前,要先弄清楚各种实现之间的不同,以及当前部署容器使用的是哪种connector。查阅tomcat7httpconnector配置文档connectorcomparison部分便可获知各种connector实现间的差异。怎样才能知道容器使用的是何种connector实现?启动tomcat后,访问serverstatuspage,看到如下信息即可知道使用的是何种connector:我的os是windows,所以tomcat默认使用的是aprconnector。在linux上,默认使用的是bioconnector。与nio相比,bio性能较低。将
connectiontimeout="20000" redirectport="8443"/> 修改为: connectiontimeout="20000" redirectport="8443"/> 就可将httpconnector切换至nio了。更多细节请参考修改tomcatconnector运行模式,优化tomcat运行性能 官网说明:http://tomcat.apache.org/tomcat-7.0-doc/config/http.html 一、最大线程数的设置 tomcat的server.xml中连接器设置如下 tomcat在配置时设置最大线程数,当前线程数超过这个数值时会出错,那么有没有办法捕获到这个错误,从而在client端显示出错信息? 2.如何加大tomcat连接数在tomcat配置文件server.xml中的 3.tomcat中如何禁止列目录下的文件在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下: 1. 2. 3.