Tomcat 笔记

使用Tomcat时遇到的一些问题,记的一些笔记

Tomcat配置

<!-- 并发配置 -->
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1"
		    URIEncoding="UTF-8" 
			maxThreads="30000"
            minSpareThreads="512"
            maxSpareThreads="2048"
		    connectionTimeout="20000"
			keepAliveTimeout="15000"
            maxKeepAliveRequests="1"
		    redirectPort="8443" 
			enableLookups="false" acceptCount="35000" disableUploadTimeout="true" />

配置 ${TOMCAT_HOME}/bin/catalina.sh

# 修改配置文件 ${TOMCAT_HOME}/bin/catalina.sh   103行左右,添加以下内容

# OS specific support.  $var _must_ be set to either true or false.   
JAVA_OPTS="$JAVA_OPTS -Xms256m -Xmx1024m -XX:PermSize=128M -XX:MaxPermSize=256m"
if [ "$1" = "start" ];then
    echo "set console";
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=6688 -Djava.rmi.server.hostname=192.168.1.6";
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false";
    JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false";
#   JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.pwd.file=/root/soft/jdk8/jre/lib/management/jmxremote.password"
    JAVA_OPTS="$JAVA_OPTS -XX:+UnlockCommercialFeatures -XX:+FlightRecorder
else
  echo "shutdown";
fi;
// 参数解释
-Xms256m    #JVM初始分配的堆内存
-Xmx2048m    #JVM最大允许分配的堆内存,按需分配
-XX:PermSize=128M    #JVM初始分配的非堆内存
-XX:MaxPermSize=256M    #JVM最大允许分配的非堆内存,按需分配
-Dcom.sun.management.jmxremote.port    #这个是配置远程 connection 的端口号的,要确定这个端口没有被占用
-Dcom.sun.management.jmxremote.ssl=false    #是否启用ssl
-Dcom.sun.management.jmxremote.authenticate=false    #指定了JMX 是否启用鉴权(需要用户名,密码鉴权)
-Djava.rmi.server.hostname    #这个是配置server的IP的,可以不配置
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder  #不用于商业用途(使用JMC bean时要配置该项)
-Dcom.sun.management.jmxremote.pwd.file  #密码文件路径(例如: /root/soft/jdk8/jre/lib/management/jmxremote.password)  不使用密码可以不配置该项

还有加if else的原因:

Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 6688; nested exception is: 
        java.net.BindException: Address already in use (Bind failed)

如果不加if else,在关闭tomcat的时候会报错,提示端口已经占用。

org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/lib/xml-apis-1.0.b2.jar] to the cache

22-Jun-2017 08:21:34.719 WARNING [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/lib/xml-apis-1.0.b2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
22-Jun-2017 08:21:34.720 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.webresources.Cache.backgroundProcess The background cache eviction process was unable to free [10] percent of the cache for Context [/m] - consider increasing the maximum size of the cache. After eviction approximately [10,239] KB of data remained in the cache.

这个问题会在Tomcat 8里遇到

方法一 增大缓存
  Tomcat conf/context.xml
  在</Context>之前添加<Resources cachingAllowed="true" cacheMaxSize="102400" />

方法二 关闭缓存
    <Resources  cachingAllowed="false"  cacheMaxSize="0"  />
In your $CATALINA_BASE/conf/context.xml add block below before </Context>

<Resources cachingAllowed="true" cacheMaxSize="102400" />

or

<Resources  cachingAllowed="false"  cacheMaxSize="0"  />

Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 6688

Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 6688; nested exception is: 
        java.net.BindException: Address already in use (Bind failed)

References

[1] https://stackoverflow.com/questions/26893297/tomcat-8-throwing-org-apache-catalina-webresources-cache-getresource-unable-to
[2] http://tomcat.apache.org/tomcat-8.0-doc/config/resources.html
[3] http://www.youyong.top/article/1158d1fece13
[4] http://www.jianshu.com/p/5a04ae2fca8c
[5] http://www.toutiao.com/a6434416811978834177/ Tomcat 的优化 献给java 小白