Resin 3 pro高并发,响应性与稳定性方案

时间:2022-03-19 00:18:44

以下方案我是在Intel xeon(至强) 3.2G 2个双核物理CPU+2G内存(Ecc)上进行:

resin版本为resin-pro-3.0.21,JVM为Jrockit 1.5_06, resin java 启动参数 -Xms256m -Xmx512m

1. 以下为resin.conf配置
<!--
- Resin 3.0 configuration file.
-->
<resin xmlns="http://caucho.com/ns/resin"
xmlns:resin="http://caucho.com/ns/resin/core">
<!--
- Logging configuration for the JDK logging API.
-->
<log name="" level="all" path="stdout:" timestamp="[%H:%M:%S.%s] "/>

<logger name="com.caucho.java" level="config"/>
<logger name="com.caucho.loader" level="config"/>

<dependency-check-interval>600s</dependency-check-interval>

<javac compiler="internal" args=""/>

<thread-pool>
<thread-max>10240</thread-max>
<spare-thread-min>50</spare-thread-min>
</thread-pool>

<min-free-memory>5M</min-free-memory>

<server>
<class-loader>
<tree-loader path="${resin.home}/lib"/>
<tree-loader path="${server.root}/lib"/>
</class-loader>

<keepalive-max>1024</keepalive-max>
<keepalive-timeout>60s</keepalive-timeout>

<resin:if test="${resin.isProfessional()}">
<select-manager enable="true"/>
</resin:if>

<bind-ports-after-start/>

<http server-id="" host="*" port="80"/>

<cluster>
<srun server-id="" host="127.0.0.1" port="6802"/>
</cluster>

<resin:if test="${resin.isProfessional()}">
<persistent-store type="cluster">
<init path="session"/>
</persistent-store>
</resin:if>

<ignore-client-disconnect>true</ignore-client-disconnect>

<resin:if test="${isResinProfessional}">
<cache path="cache" memory-size="20M"/>
</resin:if>

<web-app-default>
<class-loader>
<tree-loader path="${server.root}/ext-webapp"/>
</class-loader>

<cache-mapping url-pattern="/" expires="60s"/>
<cache-mapping url-pattern="*.gif" expires="600s"/>
<cache-mapping url-pattern="*.jpg" expires="600s"/>

<servlet servlet-name="directory"
servlet-class="com.caucho.servlets.DirectoryServlet">
<init enable="false"/>
</servlet>

<allow-servlet-el/>

<session-config>
<enable-url-rewriting>false</enable-url-rewriting>
</session-config>

</web-app-default>

<host-default>
<class-loader>
<compiling-loader path="webapps/WEB-INF/classes"/>
<library-loader path="webapps/WEB-INF/lib"/>
</class-loader>

<!--access-log path="logs/access.log"
format='%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'
rollover-period="1W"/-->

<web-app-deploy path="webapps"/>

<ear-deploy path="deploy">
<ear-default>
<!-- Configure this for the ejb server
-
- <ejb-server>
- <config-directory>WEB-INF</config-directory>
- <data-source>jdbc/test</data-source>
- </ejb-server>
-->
</ear-default>
</ear-deploy>

<resource-deploy path="deploy"/>

<web-app-deploy path="deploy"/>
</host-default>

<resin:import path="${resin.home}/conf/app-default.xml"/>

<host-deploy path="hosts">
<host-default>
<resin:import path="host.xml" optional="true"/>
</host-default>
</host-deploy>

<host id="" root-directory=".">
<web-app id="/" document-directory="d:/website/chat">
</web-app>
</host>

</server>
</resin>

2. 在应用的web.xml中加入resin status查看servlet映射
<servlet-mapping servlet-class='com.caucho.servlets.ResinStatusServlet'>
<url-pattern>/resin-status</url-pattern>
<init enable="read"/>
</servlet-mapping>
3. 启动resin,确认应用正常启动。

4. 写访问测试程序
import java.io.InputStream;
import java.net.URL;


public class TestURL
{
public static void main(String[] args) throws Exception
{
long a = System.currentTimeMillis();
System.out.println("Starting request url:");
for(int i = 0; i < 10000; i++){
URL url = new URL("http://192.168.1.200/main.jsp");

InputStream is = url.openStream();
is.close();
System.out.println("Starting request url:"+i);
}
System.out.println("request url end.take "+(System.currentTimeMillis()-a)+"ms");
}

}

5. 在Jbuilder中执行TestURL

在执行过程中,一边刷新http://192.168.1.200/resin-status,查看resin状态,在http://*:80 中的 Active Threads 和 Total,会一直增长,当长到512的时候不再增长,这时再刷新resin-status页面时,会发现打开很慢。原因是服务器已经达到最大连接数,在等待前面连接的释放而不能接受新的连接。

于是下载Resin 3.0.21源码,搜索 512,发现com.caucho.server.port.Port类中有以下代码:
// default timeout
private long _timeout = 65000;

private int _connectionMax = 512;//就是这行,查找resin所有源码后,发现没有对这个值进行设置
private int _minSpareConnection = 16;

private int _keepaliveMax = -1;

private int _minSpareListen = 5;
private int _maxSpareListen = 10;
将_connectionMax 改为 20480,然后重新编译并替换resin.jar中的Port类。

6. 重新启动Resin,再次运行TestURL进行测试,这次你会发现Threads Active 和 Total 一直变大,且可以超过512一直增大,在测试程序运行过程中刷新页面,页面响应性能还是不错的.

另,测试过程中Resin会打印出 1-3次 强制执行GC的信息,属于正常。

7.待测试完毕,Threads Active 和 Total 马上降为1.Idle为9,总内存为536.87Meg 空闲内存为480.33M

再经多次测试,结果一致,内存回收正常,表明当前 resin 稳定性和响应性可靠。