Resin3.1 优化(转http://www.blogjava.net/jelver/articles/183376.html)

本文介绍Resin服务器在高并发场景下的优化策略,包括调整JVM参数、线程池配置及日志设置等,并分享了作者在实践中总结的经验。
使用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 <!-- Maximum number of threads. --> <thread-max>2500</thread-max><!-- Configures the socket timeout --><socket-timeout>65s</socket-timeout><!-- Configures the keepalive --><keepalive-max>10240</keepalive-max><keepalive-timeout>30s</keepalive-timeout> 二、利用resin-admin监控resin运行情况。 第一行是Thread pool情况,如果发现Peak大于thread max,就应该修改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服务的情况大大减少,本设置适合于中小型网站。 Resin Threads Resin will automatically allocate and free threads as the load requires. Since the threads are pooled, Resin can reuse old threads without the performance penalty of creating and destroying the threads. When the load drops, Resin will slowly decrease the number of threads in the pool until is matches the load. Most users can set thread-max to something large (200 or greater) and then forget about the threading. Some ISPs dedicate a JVM per user and have many JVMs on the same machine. In that case, it may make sense to reduce the thread-max to throttle the requests. Since each servlet request gets its own thread, thread-max determines the maximum number of concurrent users. So if you have a peak of 100 users with slow modems downloading a large file, you'll need a thread-max of at least 100. The number of concurrent users is unrelated to the number of active sessions. Unless the user is actively downloading, he doesn't need a thread (except for "keepalives"). Keepalives Keepalives make HTTP and srun requests more efficient. Connecting to a TCP server is relatively expensive. The client and server need to send several packets back and forth to establish the connection before the first data can go through. HTTP/1.1 introduced a protocol to keep the connection open for more requests. The srun protocol between Resin and the web server plugin also uses keepalives. By keeping the connection open for following requests, Resin can improve performance. resin.conf for thread-keepalive <resin ...><thread-pool><thread-max>250</thread-max></thread-pool><server><keepalive-max>500</keepalive-max><keepalive-timeout>120s</keepalive-timeout> ... Timeouts Requests and keepalive connections can only be idle for a limited time before Resin closes them. Each connection has a read timeout, request-timeout. If the client doesn't send a request within the timeout, Resin will close the TCP socket. The timeout prevents idle clients from hogging Resin resources. ... <thread-pool><thread-max>250</thread-max></thread-pool><server><http port="8080" read-timeout="30s" write-timeout="30s"></http> ... ... <thread-max>250</thread-max><server><cluster><client-live-time>20s</client-live-time><srun id="a" port="6802" read-timeout="30s"></srun></cluster> ... In general, the read-timeout and keepalives are less important for Resin standalone configurations than Apache/IIS/srun configurations. Very heavy traffic sites may want to reduce the timeout for Resin standalone. Since read-timeout will close srun connections, its setting needs to take into consideration the client-live-time setting for mod_caucho or isapi_srun. client-live-time is the time the plugin will keep a connection open. read-timeout must always be larger than client-live-time, otherwise the plugin will try to reuse a closed socket. Plugin keepalives (mod_caucho/isapi_srun) The web server plugin, mod_caucho, needs configuration for its keepalive handling because requests are handled differently in the web server. Until the web server sends a request to Resin, it can't tell if Resin has closed the other end of the socket. If the JVM has restarted or if closed the socket because of read-timeout, mod_caucho will not know about the closed socket. So mod_caucho needs to know how long to consider a connection reusable before closing it. client-live-time tells the plugin how long it should consider a socket usable. Because the plugin isn't signalled when Resin closes the socket, the socket will remain half-closed until the next web server request. A netstat will show that as a bunch of sockets in the FIN_WAIT_2 state. With Apache, there doesn't appear to be a good way around this. If these become a problem, you can increase read-timeout and client-live-time so the JVM won't close the keepalive connections as fast. unix&gt; netstat ... localhost.32823 localhost.6802 32768 0 32768 0 CLOSE_WAIT localhost.6802 localhost.32823 32768 0 32768 0 FIN_WAIT_2 localhost.32824 localhost.6802 32768 0 32768 0 CLOSE_WAIT localhost.6802 localhost.32824 32768 0 32768 0 FIN_WAIT_2 ... TCP limits (TIME_WAIT) A client and a server that open a large number of TCP connections can run into operating system/TCP limits. If mod_caucho isn't configured properly, it can use too many connections to Resin. When the limit is reached, mod_caucho will report "can't connect" errors until a timeout is reached. Load testing or benchmarking can run into the same limits, causing apparent connection failures even though the Resin process is running fine. The TCP limit is the TIME_WAIT timeout. When the TCP socket closes, the side starting the close puts the socket into the TIME_WAIT state. A netstat will short the sockets in the TIME_WAIT state. The following shows an example of the TIME_WAIT sockets generated while benchmarking. Each client connection has a unique ephemeral port and the server always uses its public port: Typical Benchmarking Netstat unix&gt; netstat ... tcp 0 0 localhost:25033 localhost:8080 TIME_WAIT tcp 0 0 localhost:25032 localhost:8080 TIME_WAIT tcp 0 0 localhost:25031 localhost:8080 TIME_WAIT tcp 0 0 localhost:25030 localhost:8080 TIME_WAIT tcp 0 0 localhost:25029 localhost:8080 TIME_WAIT tcp 0 0 localhost:25028 localhost:8080 TIME_WAIT ... The socket will remain in the TIME_WAIT state for a system-dependent time, generally 120 seconds, but usually configurable. Since there are less than 32k ephemeral socket available to the client, the client will eventually run out and start seeing connection failures. On some operating systems, including RedHat Linux, the default limit is only 4k sockets. The full 32k sockets with a 120 second timeout limits the number of connections to about 250 connections per second. If mod_caucho or isapi_srun are misconfigured, they can use too many connections and run into the TIME_WAIT limits. Using keepalives effectively avoids this problem. Since keepalive connections are reused, they won't go into the TIME_WAIT state until they're finally closed. A site can maximize the keepalives by setting thread-keepalive large and setting live-time and request-timeout to large values. thread-keepalive limits the maximum number of keepalive connections. live-time and request-timeout will configure how long the connection will be reused. Configuration for a medium-loaded Apache ... <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><srun id="a" port="6802" read-timeout="120s"></srun></cluster> ... read-timeout must always be larger than client-live-time. In addition, keepalive-max should be larger than the maximum number of Apache processes. Apache 1.3 issues Using Apache as a web server on Unix introduces a number of issues because Apache uses a process model instead of a threading model. The Apache processes don't share the keepalive srun connections. Each process has its own connection to Resin. In contrast, IIS uses a threaded model so it can share Resin connections between the threads. The Apache process model means Apache needs more connections to Resin than a threaded model would. In other words, the keepalive and TIME_WAIT issues mentioned above are particularly important for Apache web servers. It's a good idea to use netstat to check that a loaded Apache web server isn't running out of keepalive connections and running into TIME_WAIT problems. 先将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优化: The allocation of memory for the JVM is specified using -X options when starting Resin (the exact options may depend upon the JVM that you are using, the examples here are for the Sun JVM). JVM option passed to Resin Meaning -Xms initial java heap size -Xmx maximum java heap size -Xmn the size of the heap for the young generation Resin startup with heap memory options unix&gt; bin/httpd.sh -Xmn100M -Xms500M -Xmx500M win&gt; bin/httpd.exe -Xmn100M -Xms500M -Xmx500M install win service&gt; bin/httpd.exe -Xmn100M -Xms500M -Xmx500M -install 原文:http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtp JVM 优化: java -Xms<size> set initial Java heap size. default:Xms32m java -Xmx<size> set maximum Java heap size. default:Xmx128m set it like that: java -Xms=32m -Xmx=256m If the problem persist, increase Xmx more than 256 ( 512m for example ) -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:25 log设置 <log name="" level="info" path="stdout:" rollover-period="1W" timestamp="[%Y/%m/%d %H:%M:%S.%s] "></log><log name="com.caucho.java" level="fine" path="stdout:" rollover-period="1W" timestamp="[%Y/%m/%d %H:%M:%S.%s] "></log><log name="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 的级别一般有:: Off Severe info config fine finer finest all 这几中配置级别; 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.20041201 stderr.log.20041208 rollover-period=’D’ 天 rollover-period=’h’ 小时 rollover-period=’W’ 周 rollover-period=’M’ 月 当不需要改动程序时,关闭java自动编译会更快些. <compiling-loader path="webapps/WEB-INF/classes"></compiling-loader> 加个属性 batch="false" $JAVA_ARGS="-server"; 据说java中的-server参数是让本地化编译更完全.
<think>好的,我现在需要帮用户解决Java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0的问题。首先,我要回忆一下这个错误的原因是什么。根据用户提供的引用内容,特别是引用[3]提到,这是由于编译版本和运行版本不一致导致的,也就是说用高版本的JDK编译的类文件在低版本的JRE上运行了。 首先,我得确认用户是否了解JDK和JRE的版本对应关系。比如,major.minor version 51.0对应的是哪个Java版本?我记得Java版本和主版本号之间有一定的对应关系,比如Java 7对应51,Java 8对应52,但这里错误是51.0,应该是Java 7编译的。所以用户可能在用Java 7编译,然后在更低版本的Java上运行,比如Java 6或者更早。 接下来,我需要考虑解决方案。根据引用[4]提到的参考解决方案,可能需要整编译版本或者运行环境。因此,解决方案可能包括:检查并统一JDK和JRE的版本,或者整项目的编译级别。 然后,用户可能需要具体的步骤来检查他们的环境。例如,如何检查当前使用的Java版本?在命令行中使用java -version和javac -version,比较两者的输出是否一致。如果不一致,就需要配置环境变量,确保编译和运行时使用相同的版本。 另外,如果用户使用的是IDE,比如Eclipse或IntelliJ,可能需要指导他们如何在IDE中设置项目的Java编译器版本。比如在Eclipse中,项目属性里的Java Compiler设置,或者在Maven项目中配置pom.xml的maven-compiler-plugin。 还有可能需要提到构建工具如Maven或Gradle的配置,因为这些工具可能会覆盖默认的编译版本设置。例如,在Maven的pom.xml中指定正确的source和target版本。 此外,用户可能不知道如何确定已安装的JDK版本,可能需要指导他们如何查找系统中安装的JDK路径,并正确设置JAVA_HOME环境变量。 最后,需要提醒用户如果必须使用低版本运行环境,就需要用对应的低版本JDK重新编译项目,以确保兼容性。如果项目依赖的库需要更高版本的Java,可能需要升级运行环境或寻找替代库。 总结一下,解决方案应该分步骤,包括检查版本、统一版本、配置开发工具和构建工具,以及必要时整项目设置。同时,引用用户提供的参考资料,特别是引用[3]和[4],以增强可信度。</think>### 解决方案:`Unsupported major.minor version 51.0`错误 此错误表示 **编译时使用的JDK版本高于运行时环境版本**。具体来说,`major.minor version 51.0`对应Java 7,说明代码是用Java 7编译的,但运行时环境是Java 6或更低版本[^3][^4]。以下是详细解决步骤: --- #### **步骤1:确认版本差异** 1. **检查运行时Java版本** 在命令行中执行: ```bash java -version ``` 若输出类似`1.6.0_45`,则运行时环境是Java 6,无法运行Java 7编译的类文件。 2. **检查编译时Java版本** 在命令行中执行: ```bash javac -version ``` 若输出为`1.7.0`,则编译版本为Java 7。 --- #### **步骤2:统一JDK和JRE版本** - **方案1:升级运行时环境** 安装与编译版本一致的Java版本(例如Java 7或更高),并确保环境变量`JAVA_HOME`和`PATH`指向新版本。 ```bash # 示例:设置环境变量(Linux/Mac) export JAVA_HOME=/usr/lib/jvm/java-7-openjdk export PATH=$JAVA_HOME/bin:$PATH ``` - **方案2:降低编译版本** 若必须使用低版本JRE(如Java 6),需在编译时指定目标版本: ```bash javac -source 1.6 -target 1.6 YourClass.java ``` 或在构建工具(Maven/Gradle)中配置: ```xml <!-- Maven示例 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> ``` --- #### **步骤3:检查IDE配置** - **Eclipse/IntelliJ** 在IDE中设置项目的编译兼容性: 1. 右键项目 → Properties → Java Compiler 2. 确保`Compiler compliance level`与运行时环境一致(如1.6)。 - **Maven/Gradle项目** 检查构建脚本中的`sourceCompatibility`和`targetCompatibility`,确保版本匹配。 --- #### **步骤4:验证依赖库兼容性** 某些第三方库可能需要更高版本的Java。通过以下命令检查依赖库的编译版本: ```bash javap -v YourClass.class | grep "major version" ``` 若依赖库版本高于运行时环境,需升级JRE或替换为兼容版本。 --- ### 关键点总结 | 问题根源 | 解决方法 | 验证方式 | |---------|---------|---------| | 编译版本 > 运行版本 | 统一版本或降低编译目标 | `java -version`与`javac -version`一致 | | 构建工具配置错误 | 修改Maven/Gradle配置 | 检查构建脚本的`source`和`target` | | 依赖库版本冲突 | 更换依赖或升级JRE | `javap`分析类文件版本 | [^3]: 编译版本和运行版本不一致导致的问题。 : 参考解决方案:[版本兼容性整](http://www.blogjava.net/Jay2009/archive/2009/04/23/267108.html) ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值