调优
(1)加大Tomcat的链接数
和最大连接数相关的参数为maxProcessors和acceptCount。如果要加大并发连接数,应同时加大这两个参数。 acceptCount:允许的最大连接数,应大于等于maxProcessors,默认值为100
web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右。
示例:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="512" minSpareThreads="50" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="512"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="UTF-8"/>
注:URIEncoding="UTF-8"是设置请求参数的编码格式。
(2)加大tomcat可以使用的内存
tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大。
Windows下,在文件{tomcat_home}/bin/catalina.bat,Unix下,在文件{tomcat_home}/bin/catalina.sh的前面,增加如下设置:
JAVA_OPTS='-Xms【初始化内存大小】 -Xmx【可以使用的最大内存】'
需要把这个两个参数值调大。例如:
JAVA_OPTS='-Xms256m -Xmx512m'
表示初始化内存为256MB,可以使用的最大内存为512MB。
自己服务器用到:在{tomcat_home}/bin/catalina.sh文件的前面增加以下一行代码。
CATALINA_OPTS="$CATALINA_OPTS -Xms512m -Xmx512m"
配置开机启动
安装jsvc
#cd /usr/local/tomcat/bin
#tar xvfz jsvc.tar.gz
#cd jsvc-src
#sh support/buildconf.sh
#chmod 755 configure
#./configure --with-java=/wapservice/server/jdk1.5.0_04 (改成你的JDK的位置)
#make
添加脚本让tomcat自动启动
#useradd tomcat5 添加用户
#groupadd tomcat 添加组
#usermod -G tomcat tomcat5 把tomcat5加入tomcat组
# chown -R tomcat5 /wapservice/server/tomcat-5.5.27 设置用户tomcat5对tomcat的权限
#cp /usr/local/tomcat/bin/jsvc-src/native/Tomcat5.sh /etc/init.d/tomcat 移动文件tomcat5.sh到/etc/init.d/里面并改名为tomcat
#chmod 755 /etc/init.d/tomcat
#vi /etc/init.d/tomcat
配置文件片段:
#!/bin/sh
##############################################################################
#
# Copyright 2004 The Apache Software Foundation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# Small shell script to show how to start/stop Tomcat using jsvc
# If you want to have Tomcat running on port 80 please modify the server.xml
# file:
#
# <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
# <Connector className="org.apache.catalina.connector.http.HttpConnector"
# port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME=/usr/src/jdk1.5.0_22
CATALINA_HOME=/var/tomcat5
DAEMON_HOME=/var/tomcat5/bin
TOMCAT_USER=tomcat
TMP_DIR=/var/tmp
CATALINA_OPTS=" -Xms512m -Xmx1024m -DEWAY_HOME=/var/tomcat5/webapps/flexcc/eway-home"
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar
case "$1" in
start)
#
# Start Tomcat
#
$DAEMON_HOME/jsvc-src/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Djava.io.tmpdir=$TMP_DIR \
-outfile $CATALINA_HOME/logs/catalina.out \
-errfile '&1' \
$CATALINA_OPTS \
-cp $CLASSPATH \
org.apache.catalina.startup.Bootstrap
#
# To get a verbose JVM
#-verbose \
# To get a debug of jsvc.
#-debug \
;;
stop)
#
# Stop Tomcat
#
PID=`cat /var/run/jsvc.pid`
kill $PID
;;
*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac
测试,执行命令:service tomcat start|stop|restart|status
把这段代码保存为 /etc/rc.d/init.d/tomcat ,然后运行
#chkconfig --add tomcat
用--list看一下是否系统已有tomcat启动文件
#ckhconfig --list
即可把tomcat 添加为系统服务自动随系统启动了。
遇到的问题:
[root@asterisk ~]# chkconfig --add tomcat
service tomcat does not support chkconfig
解决的办法:
在脚本的最前面加上
#!/bin/bash
# chkconfig: 2345 10 90
# description: Starts and Stops the Tomcat daemon.
再运行命令
sudo /sbin/chkconfig –add tomcat
sudo /sbin/chkconfig –list
看到tomcat,一切搞定。