Tomcat知识集锦
常见问题
零散问题
One or more listeners failed to start
报错信息:
Artifact pems-web-manager:war exploded: Error during artifact deployment. See server log for details.
org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
报错原因:某个Web应用的Servlet监听器配置出现问题(例如监听器类找不到),上面的出错信息不明确,检查web.xml即可。
Tomcat7生产环境配置
环境准备
下载软件
软件 | 版本 | 说明 |
Tomcat | 7.0.56 |
如果是64位Windows,可以下载:64-bit Windows zip |
JRE | 7u71 |
如果是64位Windows,可以下载:Windows x64的tar.gz版本 下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/jre7-downloads-1880261.html |
安装软件
- 解压Tomcat到某个位置,例如:D:\JavaEE\production\Tomcat7,确认Tomcat7\bin目录存在
- 解压JRE到%TOMCAT_HOME%\jre,确认%TOMCAT_HOME%\jre\bin目录存在
Tomcat配置
使用%TOMCAT_HOME%\jre作为Java运行环境
- 在%TOMCAT_HOME%下建立文件startup.bat,内容类似下面:
12345678910111213141516171819202122232425262728293031323334353637@echo offecho *******************************************************************************echo Portable Tomcat 7 by Alex Wangecho.pushd "%~dp0"for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set md=%%a-%%b-%%c)for /f "tokens=1-3 delims=/:." %%a in ("%TIME%") do (set mt=%%a%%b%%c)set CUR_TIME=%md%-%mt%set "JAVA_HOME="set "CATALINA_HOME=%CD%"set "JRE_HOME=%CATALINA_HOME%\jre"set "LOG_DIR=%CATALINA_HOME%\logs"rem 请根据服务器硬件配置、系统的特性填写下面一行rem 响应时间优先配置示例:-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=8 -XX:MaxGCPauseMillis=500rem 吞吐量优先的配置示例:-XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=8 -Xmn1280Mset JAVA_OPTS=-server -Xms2G -Xmx2G -Xss128K -XX:MaxPermSize=256Mrem 启用错误记录set JAVA_OPTS=%JAVA_OPTS% -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%LOG_DIR%\ -XX:ErrorFile=%LOG_DIR%\hs_err_pid%%p.logrem 启用JMX远程管理和黑匣子set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=7777 -Dcom.sun.management.jmxremote -XX:+UnlockCommercialFeatures -XX:+FlightRecorderrem 启用黑匣子默认记录行为set JAVA_OPTS=%JAVA_OPTS% -XX:FlightRecorderOptions=defaultrecording=true,disk=true,dumponexit=true,dumponexitpath=%LOG_DIR%\jfr\dump-on-exit-%CUR_TIME%.jfr,maxage=3600s,settings=%JRE_HOME%\lib\jfr\profile.jfcrem 记录GC详细日志set JAVA_OPTS=%JAVA_OPTS% -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:%LOG_DIR%\gc.logrem 启用远程调试set JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000echo Setting CATALINA_HOME %CATALINA_HOME%echo Setting JRE_HOME %JRE_HOME%echo Setting JAVA_OPTS %JAVA_OPTS%echo.echo Launching Tomcat 7 startup.bat...echo *******************************************************************************echo.call bin\startup.batpopd - 类似的,编写一个shutdown.bat
NT服务配置
如果不想使用脚本启动(上一段的方式),可以把Tomcat配置为NT服务,把下面的脚本存放到%TOMCAT_HOME%,传入服务名、显示名、服务描述三个参数即可安装服务,内存配置需要根据机器的性能进行调整:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@echo off pushd "%~dp0" setlocal set SERVICE_NAME=%~1 set DISPLAY_NAME=%~2 set SERVICE_DESC=%~3 set "CATALINA_HOME=%CD%" set "EXECUTABLE=%CATALINA_HOME%\bin\tomcat7.exe" set "CATALINA_BASE=%CATALINA_HOME%" set "JRE_HOME=%CATALINA_HOME%\jre" set "LOG_DIR=%CATALINA_HOME%\logs" set "CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar" rem 请根据服务器硬件配置、系统的特性填写下面一行 rem 响应时间优先配置示例:-XX:MaxPermSize=256M;-XX:+UseConcMarkSweepGC;-XX:+UseParNewGC;-XX:ParallelGCThreads=8;-XX:MaxGCPauseMillis=500;-XX:+UseParNewGC; rem 吞吐量优先的配置示例:-XX:+UseParallelGC;-XX:+UseParallelOldGC;-XX:ParallelGCThreads=8;-Xmn1280M; set "JVM_MEM_GC_OPTS=-XX:MaxPermSize=256M;" rem 启用JMX远程管理和黑匣子 set "JMX_OPTS=-Dcom.sun.management.jmxremote.authenticate=false;-Dcom.sun.management.jmxremote.ssl=false;-Dcom.sun.management.jmxremote.port=7777;-Dcom.sun.management.jmxremote;-XX:+UnlockCommercialFeatures;-XX:+FlightRecorder;" rem 启用错误记录 set "JVM_ERR_OPTS=-XX:+HeapDumpOnOutOfMemoryError;-XX:HeapDumpPath=%LOG_DIR%\;-XX:ErrorFile=%LOG_DIR%\hs_err_pid%%p.log;" set "JFR_AUTO_RCD=-XX:FlightRecorderOptions=defaultrecording=true,disk=true,dumponexit=true,dumponexitpath=%LOG_DIR%\jfr\dump-on-exit-%SERVICE_NAME%.jfr,maxage=3600s,settings=%JRE_HOME%\lib\jfr\profile.jfc;" .\bin\tomcat7 //IS//%SERVICE_NAME% --Startup=auto ^ --DisplayName="%DISPLAY_NAME%" --Description "%SERVICE_DESC%" ^ --Jvm="%CATALINA_HOME%\jre\bin\server\jvm.dll" ^ --Classpath="%CLASSPATH%" ^ --LogPath "%CATALINA_HOME%\logs\services\%SERVICE_NAME%" --LogLevel=Info --StdOutput auto --StdError auto ^ --StartMode=jvm --StartPath "%CATALINA_HOME%" --StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start ^ --StopMode=jvm --StopPath "%CATALINA_HOME%" --StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop ^ --JvmMs=2048 --JvmMx=2048 --JvmSs=128 ^ ++JvmOptions "%JVM_MEM_GC_OPTS%-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;%JMX_OPTS%%JVM_ERR_OPTS%%JFR_AUTO_RCD%" popd endlocal |
优化Tomcat配置参数(基于Java NIO)
参考下面的代码编辑%TOMCAT_HOME%\conf\server.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<!-- 注释掉 AJP --> <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> --> <!-- 使用Java NIO优化性能,注意删除原Connector元素的同名属性,线程数和缓存依据实际需要调整 --> <Connector port="8080" redirectPort="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" URIEncoding="UTF-8" useBodyEncodingForURI="true" maxThreads="1024" minSpareThreads="128" socket.appReadBufSize="1024" socket.appWriteBufSize="1024" /> |
Linux下的脚本示例(CentOS 6.3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/bin/bash PDTDIR=$(dirname $0) export CUR_TIME=`date "+%Y%m%d%H%M%S"` export JAVA_HOME= export CATALINA_HOME=$PDTDIR export JRE_HOME=$CATALINA_HOME/jre export LOG_DIR=$CATALINA_HOME/logs # 请根据服务器硬件配置、系统的特性填写下面一行 # 响应时间优先配置示例:-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=8 -XX:MaxGCPauseMillis=500 # 吞吐量优先的配置示例:-XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=8 -Xmn1280M export JAVA_OPTS="-server -Xms2G -Xmx2G -Xss256K -XX:MaxPermSize=256M" # 启用错误记录 export JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_DIR/ -XX:ErrorFile=$LOG_DIR/hs_err_pid%p.log" # 启用JMX远程管理和黑匣子 export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=7777 -Dcom.sun.management.jmxremote -XX:+UnlockCommercialFeatures -XX:+FlightRecorder" # 启用黑匣子默认记录行为 export JAVA_OPTS="$JAVA_OPTS -XX:FlightRecorderOptions=defaultrecording=true,disk=true,dumponexit=true,dumponexitpath=$LOG_DIR/jfr/dump-on-exit-$CUR_TIME.jfr,maxage=3600s,settings=$JRE_HOME/lib/jfr/profile.jfc" #rem 记录GC详细日志 export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:$LOG_DIR/gc.log" echo echo "*******************************************************************************" echo " Portable Tomcat 7 for Linux by Alex Wang (Last updated: May 7th, 2015 ) " echo "Setting CATALINA_HOME $CATALINA_HOME" echo "Setting JRE_HOME $JRE_HOME" echo "Setting JAVA_OPTS $JAVA_OPTS" echo echo "Launching Tomcat 7 startup.bat... " echo "*******************************************************************************" echo exec "$PDTDIR"/bin/startup.sh |
Leave a Reply