suyouming 2010-12-26
使用resin已经有四、五年了,但以前都是做一些小系统,resin的压力并不大,近段时间做一个大系统,日平均ip上10万,resin的压力非常的大,除了对程序做优化以外,resin的优化也小不了。一、优化配置 修改 conf/resin.conf 文章中的 JVM参数<jvm-arg>-Xms512m</jvm-arg><jvm-arg>-Xss128k</jvm-arg><jvm-arg>-Xmn184m</jvm-arg><jvm-arg>-XX:ParallelGCThreads=20</jvm-arg><jvm-arg>-XX:+UseConcMarkSweepGC</jvm-arg><jvm-arg>-XX:+UseParNewGC</jvm-arg><jvm-arg>-Xdebug</jvm-arg><jvm-arg>-Xloggc:gc.log</jvm-arg> 修改最大thread-max为2500<!--Maximumnumberofthreads.--><thread-max>2500</thread-max><!--Configuresthesockettimeout--><socket-timeout>65s</socket-timeout><!--Configuresthekeepalive--><keepalive-max>10240</keepalive-max><keepalive-timeout>30s</keepalive-timeout>二、利用resin-admin监控resin运行情况。第一行是Threadpool情况,如果发现Peak大于threadmax,就应该修改conf/resin.conf中的thread-max,相应的增大thread-max。第二行是Threads,如果长期出现在这里而又不是SUN的方法,或者resin的方法的话,就要对这些方法进行测试、优化。以下内容都是自己不断实验总结的,而非resin官方的建议,可能不适合你的情况,我的经验仅做为参考。)最近发现有人用黑客类工具恶意点击网站,或发送大量垃圾包,具体是什么不清楚,但是很明显是故意的,造成80端口无法正常访问,或访问速度极慢。用netstat-an>>c:\temp\aaa.txt命令查看了当时情况,发现某几个ip的连接数量巨大,是不正常的。不管是访问量大,还是有黑客骚扰,我想还是试试看把resin优化一下。首先要在访问量巨大的时候进行观察。先将resin.conf文件中的thread-min,thread-max,thread-keepalive三个参数设置的比较大,分别写上,1000,3000,1000,当然这是根据你的机器情况和可能同时访问的数量决定的,如果你的网站访问量很大的,应该再适当放大。然后观察任务管理器中的java线程变化情况,看看到底是线程达到多大的时候,java进程当掉的。我的是在379左右当掉。然后将thread-min,thread-max,thread-keepalive分别写为150,400,300;,也就是将当掉的时候的最大值稍微放大点,作为thread-max的值,因为该系统一般不会超过这个值。然后其他两个参数根据情况设置一下。这只是我的估计值,根据机器性能和访问量不同,应该有所不同。然后将accept-buffer-size值设置的较大,我设置到10000以上,这样可以让java能使用到更多的内存资源。这样的设置基本上能够满足resin的正常运行,当掉resin服务的情况大大减少,本设置适合于中小型网站。ResinThreadsResinwillautomaticallyallocateandfreethreadsastheloadrequires.Sincethethreadsarepooled,Resincanreuseoldthreadswithouttheperformancepenaltyofcreatinganddestroyingthethreads.Whentheloaddrops,Resinwillslowlydecreasethenumberofthreadsinthepooluntilismatchestheload.Mostuserscansetthread-maxtosomethinglarge(200orgreater)andthenforgetaboutthethreading.SomeISPsdedicateaJVMperuserandhavemanyJVMsonthesamemachine.Inthatcase,itmaymakesensetoreducethethread-maxtothrottletherequests.Sinceeachservletrequestgetsitsownthread,thread-maxdeterminesthemaximumnumberofconcurrentusers.Soifyouhaveapeakof100userswithslowmodemsdownloadingalargefile,you'llneedathread-maxofatleast100.Thenumberofconcurrentusersisunrelatedtothenumberofactivesessions.Unlesstheuserisactivelydownloading,hedoesn'tneedathread(exceptfor"keepalives").KeepalivesKeepalivesmakeHTTPandsrunrequestsmoreefficient.ConnectingtoaTCPserverisrelativelyexpensive.Theclientandserverneedtosendseveralpacketsbackandforthtoestablishtheconnectionbeforethefirstdatacangothrough.HTTP/1.1introducedaprotocoltokeeptheconnectionopenformorerequests.ThesrunprotocolbetweenResinandthewebserverpluginalsouseskeepalives.Bykeepingtheconnectionopenforfollowingrequests,Resincanimproveperformance.resin.confforthread-keepalive<resin...><thread-pool><thread-max>250</thread-max></thread-pool><server><keepalive-max>500</keepalive-max><keepalive-timeout>120s</keepalive-timeout>...TimeoutsRequestsandkeepaliveconnectionscanonlybeidleforalimitedtimebeforeResinclosesthem.Eachconnectionhasareadtimeout,request-timeout.Iftheclientdoesn'tsendarequestwithinthetimeout,ResinwillclosetheTCPsocket.ThetimeoutpreventsidleclientsfromhoggingResinresources....<thread-pool><thread-max>250</thread-max></thread-pool><server><httpport="8080"read-timeout="30s"write-timeout="30s"></http>......<thread-max>250</thread-max><server><cluster><client-live-time>20s</client-live-time><srunid="a"port="6802"read-timeout="30s"></srun></cluster>...Ingeneral,theread-timeoutandkeepalivesarelessimportantforResinstandaloneconfigurationsthanApache/IIS/srunconfigurations.VeryheavytrafficsitesmaywanttoreducethetimeoutforResinstandalone.Sinceread-timeoutwillclosesrunconnections,itssettingneedstotakeintoconsiderationtheclient-live-timesettingformod_cauchoorisapi_srun.client-live-timeisthetimethepluginwillkeepaconnectionopen.read-timeoutmustalwaysbelargerthanclient-live-time,otherwisethepluginwilltrytoreuseaclosedsocket.Pluginkeepalives(mod_caucho/isapi_srun)Thewebserverplugin,mod_caucho,needsconfigurationforitskeepalivehandlingbecauserequestsarehandleddifferentlyinthewebserver.UntilthewebserversendsarequesttoResin,itcan'ttellifResinhasclosedtheotherendofthesocket.IftheJVMhasrestartedorifclosedthesocketbecauseofread-timeout,mod_cauchowillnotknowabouttheclosedsocket.Somod_cauchoneedstoknowhowlongtoconsideraconnectionreusablebeforeclosingit.client-live-timetellsthepluginhowlongitshouldconsiderasocketusable.Becausethepluginisn'tsignalledwhenResinclosesthesocket,thesocketwillremainhalf-closeduntilthenextwebserverrequest.AnetstatwillshowthatasabunchofsocketsintheFIN_WAIT_2state.WithApache,theredoesn'tappeartobeagoodwayaroundthis.Ifthesebecomeaproblem,youcanincreaseread-timeoutandclient-live-timesotheJVMwon'tclosethekeepaliveconnectionsasfast.unix>netstat...localhost.32823localhost.6802327680327680CLOSE_WAITlocalhost.6802localhost.32823327680327680FIN_WAIT_2localhost.32824localhost.6802327680327680CLOSE_WAITlocalhost.6802localhost.32824327680327680FIN_WAIT_2...TCPlimits(TIME_WAIT)AclientandaserverthatopenalargenumberofTCPconnectionscanrunintooperatingsystem/TCPlimits.Ifmod_cauchoisn'tconfiguredproperly,itcanusetoomanyconnectionstoResin.Whenthelimitisreached,mod_cauchowillreport"can'tconnect"errorsuntilatimeoutisreached.Loadtestingorbenchmarkingcanrunintothesamelimits,causingapparentconnectionfailureseventhoughtheResinprocessisrunningfine.TheTCPlimitistheTIME_WAITtimeout.WhentheTCPsocketcloses,thesidestartingthecloseputsthesocketintotheTIME_WAITstate.AnetstatwillshortthesocketsintheTIME_WAITstate.ThefollowingshowsanexampleoftheTIME_WAITsocketsgeneratedwhilebenchmarking.Eachclientconnectionhasauniqueephemeralportandtheserveralwaysusesitspublicport:TypicalBenchmarkingNetstatunix>netstat...tcp00localhost:25033localhost:8080TIME_WAITtcp00localhost:25032localhost:8080TIME_WAITtcp00localhost:25031localhost:8080TIME_WAITtcp00localhost:25030localhost:8080TIME_WAITtcp00localhost:25029localhost:8080TIME_WAITtcp00localhost:25028localhost:8080TIME_WAIT...ThesocketwillremainintheTIME_WAITstateforasystem-dependenttime,generally120seconds,butusuallyconfigurable.Sincetherearelessthan32kephemeralsocketavailabletotheclient,theclientwilleventuallyrunoutandstartseeingconnectionfailures.Onsomeoperatingsystems,includingRedHatLinux,thedefaultlimitisonly4ksockets.Thefull32ksocketswitha120secondtimeoutlimitsthenumberofconnectionstoabout250connectionspersecond.Ifmod_cauchoorisapi_srunaremisconfigured,theycanusetoomanyconnectionsandrunintotheTIME_WAITlimits.Usingkeepaliveseffectivelyavoidsthisproblem.Sincekeepaliveconnectionsarereused,theywon'tgointotheTIME_WAITstateuntilthey'refinallyclosed.Asitecanmaximizethekeepalivesbysettingthread-keepalivelargeandsettinglive-timeandrequest-timeouttolargevalues.thread-keepalivelimitsthemaximumnumberofkeepaliveconnections.live-timeandrequest-timeoutwillconfigurehowlongtheconnectionwillbereused.Configurationforamedium-loadedApache...<thread-pool><thread-max>250</thread-max></thread-pool><server><keepalive-max>250</keepalive-max><keepalive-timeout>120s</keepalive-timeout><cluster><client-live-time>120s</client-live-time><srunid="a"port="6802"read-timeout="120s"></srun></cluster>...read-timeoutmustalwaysbelargerthanclient-live-time.Inaddition,keepalive-maxshouldbelargerthanthemaximumnumberofApacheprocesses.Apache1.3issuesUsingApacheasawebserveronUnixintroducesanumberofissuesbecauseApacheusesaprocessmodelinsteadofathreadingmodel.TheApacheprocessesdon'tsharethekeepalivesrunconnections.EachprocesshasitsownconnectiontoResin.Incontrast,IISusesathreadedmodelsoitcanshareResinconnectionsbetweenthethreads.TheApacheprocessmodelmeansApacheneedsmoreconnectionstoResinthanathreadedmodelwould.Inotherwords,thekeepaliveandTIME_WAITissuesmentionedaboveareparticularlyimportantforApachewebservers.It'sagoodideatousenetstattocheckthataloadedApachewebserverisn'trunningoutofkeepaliveconnectionsandrunningintoTIME_WAITproblems.先将resin.conf文件中的thread-min,thread-max,thread-keepalive三个参数设置的比较大,分别写上,1000,3000,1000,当然这是根据你的机器情况和可能同时访问的数量决定的,如果你的网站访问量很大的,应该再适当放大。然后观察任务管理器中的java线程变化情况,看看到底是线程达到多大的时候,java进程当掉的。我的是在379左右当掉。然后将thread-min,thread-max,thread-keepalive分别写为150,400,300;,也就是将当掉的时候的最大值稍微放大点,作为thread-max的值,因为该系统一般不会超过这个值。然后其他两个参数根据情况设置一下。这只是我的估计值,根据机器性能和访问量不同,应该有所不同。然后将accept-buffer-size值设置的较大,我设置到10000以上,这样可以让java能使用到更多的内存资源。这样的设置基本上能够满足resin的正常运行,当掉resin服务的情况大大减少,本设置适合于中小型网站。Resin优化:TheallocationofmemoryfortheJVMisspecifiedusing-XoptionswhenstartingResin(theexactoptionsmaydependupontheJVMthatyouareusing,theexamplesherearefortheSunJVM).JVMoptionpassedtoResinMeaning-Xmsinitialjavaheapsize-Xmxmaximumjavaheapsize-XmnthesizeoftheheapfortheyounggenerationResinstartupwithheapmemoryoptionsunix>bin/httpd.sh-Xmn100M-Xms500M-Xmx500Mwin>bin/httpd.exe-Xmn100M-Xms500M-Xmx500Minstallwinservice>bin/httpd.exe-Xmn100M-Xms500M-Xmx500M-install原文:http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtpJVM优化:java-Xms<size>setinitialJavaheapsize.default:Xms32mjava-Xmx<size>setmaximumJavaheapsize.default:Xmx128msetitlikethat:java-Xms=32m-Xmx=256mIftheproblempersist,increaseXmxmorethan256(512mforexample)-J-mx<num>Resin启动时通过bin目录下的wrapper.pl文件进行控制,我们可以修改这个文件来加一些参数,比如要加入Java的-Xms和-Xmx参数进行vi/usr/local/resin-2.1/bin/wrapper.pl找到并修改以下这行为:$JAVA_ARGS="-Xms512m-Xmx512m";具体参数请根据自己的应用进行调节Resin的优化---日志的设置2007年03月06日星期二10:25log设置<logname=""level="info"path="stdout:"rollover-period="1W"timestamp="[%Y/%m/%d%H:%M:%S.%s]"></log><logname="com.caucho.java"level="fine"path="stdout:"rollover-period="1W"timestamp="[%Y/%m/%d%H:%M:%S.%s]"></log><logname="com.caucho.loader"level="config"path="stdout:"rollover-period="1W"timestamp="[%Y/%m/%d%H:%M:%S.%s]"></log>name是指定对各个层次应用进行debug,name设定有几种情况,如:Name=’’name为空,这对所有应用、包括端口全面进行调试记载日志Name=’com.caucho.jsp’指定只对jsp进行调试记载日志Name=’com.caucho.java’指定只对java类进行调试Name=’com.caucho.server.port’指定只对端口、线程进行调试Name=’com.caucho.server.port.AcceptPool指定只对端口线程的创建和释放进行debug….level的级别一般有::OffSevereinfoconfigfinefinerfinestall这几中配置级别;Off关闭日志输出Severe只输出些严重的出错信息Info输出一般的综合信息Config输出配置信息Fine输出resin的跟踪信息Finer输出详细的跟踪信息Finest输出比Finer更详细的跟踪消息、细节问题All输出所有的访问输出消息path:输出文件路径指向,可以形式如path=’stdout:’注意后面有冒号;或指定绝对路径path=’/usr/local/resin-3.0.7/log/stdout.log’Timestamp:完整的输出日期格式[%Y/%m/%d%H:%M:%S.%s];一般设置日志文件一周轮循一次,即rollover-period=’1M’或rollover-period=’7D’,当满一周,系统会自动生成新日志记录文件,格式如:stderr.log.20041201stderr.log.20041208rollover-period=’D’天rollover-period=’h’小时rollover-period=’W’周rollover-period=’M’月当不需要改动程序时,关闭java自动编译会更快些.<compiling-loaderpath="webapps/WEB-INF/classes"></compiling-loader>加个属性batch="false"$JAVA_ARGS="-server";据说java中的-server参数是让本地化编译更完全.