Tomcat配置、管理和問題解決 | Tomcat Configuration, Manangement and Trouble Shooting


Tomcat配置、管理和調優

Tomcat知識

關於tomcat AJP

由於tomcat的html和圖片解析功能相對其他服務器如apche等較弱,所以,一般都是集成起來使用,只有jsp和servlet服務交由 tomcat處理,而tomcat和其他服務器的集成,就是通過ajp協議來完成的。AJP協議AJP13是定向包協議。因為性能原因,使用二進制格式來 傳輸可讀性文本。

Tomcat開發模式

Web應用啟動時需要加載很多東東,特別是項目使用了Hibernate,耗時比較長,在開發 環境中調 試程序,每次修改class都要重新加載Web應用,很占用時間,要是能只替換修改的類就好了。於是搜索相關資料,Tomcat文檔說設置 reloadable=true可以動態加載,但是我試驗幾次不成功。還有高手說需要研究Tomcat的文件監聽機制,重新實現webapp 類加載器,偶 水平有限,看的一頭霧水。后來試出一個奇怪的辦法,將reloadable設為false,可以使修改后的class生效又不用重啟Web應 用。我的環 境:JDK6、Tomcat6、Eclipse3.2、MyEclipse5.1,具體設置:修改server.xml,
<Context path="/cyxt" docBase="D:\ccProject\dz0224_jn_gxx\mshs_cybs\cy_src\WebRoot"
        debug="5" privileged="true" reloadable="false" workDir="D:\tomcat\work\Catalina\localhost\hccy">
</Context>
雖然Tomcat6推薦使用TCD部署,但是server.xml還是起作用的。這樣就OK了,無論修改類或jsp都不需重啟,可以顯著提高 工作效率。

Tomcat啟動時候如何加載webapps下的xx.war和web app目錄
如果部署的xx.war有同名的web app目錄,就不會自動解壓war了,也不會更新war到app目錄,war里的內容跟app目錄不通。

SSL TLS版本,TOMCAT中server.xml sslProtocol配置

SSL:(Secure Socket Layer,安全套接字層),位於可靠的面向連接的網絡層協議和應用層協議之間的一種協議層。SSL通過互相認證、使用數字簽名確保完整性、使用加密確保 私密性,以實現客戶端和服務器之間的安全通訊。該協議由兩層組成:SSL記錄協議和SSL握手協議。
TLS:(Transport Layer Security,傳輸層安全協議),用於兩個應用程序之間提供保密性和數據完整性。該協議由兩層組成:TLS記錄協議和TLS握手協議。
SSL是Netscape開發的專門用戶保護Web通訊的,目前版本為3.0。最新版本的TLS 1.0是IETF(工程任務組)制定的一種新的協議,它建立在SSL 3.0協議規范之上,是SSL 3.0的后續版本。兩者差別極小,可以理解為SSL 3.1,它是寫入了RFC的。
TLS記錄格式與SSL記錄格式相同,但版本號的值不同,TLS的版本1.0使用的版本號為SSLv3.1。?
1) sslProtocol="TLS" will enable SSLv3 and TLSv1
2) sslProtocol="TLSv1.2" will enable SSLv3, TLSv1, TLSv1.1 and TLS v1.2
3) sslProtocol="TLSv1.1" will enable SSLv3, TLSv1, and TLSv1.1
4) sslProtocol="TLSv1" will enable SSLv3 and TLSv1
5) sslProtocol="SSL" will enable SSLv3 and TLSv1
6) sslProtocol="SSLv3" will enable SSLv3 and TLSv1
7) sslProtocol="SSLv2" won't work

Tomcat SSL protocol設置和Java Client:

<Connector executor="tomcatThreadPool" port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="/opt/....keystore" keystorePass="soudang123"
               clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8"/>

javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("TLS"); 

http://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLContext.html#getInstance%28java.lang.String%29

http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#SSLContext

Tomcat 安裝

CentOS7 Tomcat安裝成系統服務,以非root用戶啟動

centos7 使用 systemctl 替換了 service命令

# systemctl list-unit-files --type service #查看全部服務命令
# systemctl status name.service #查看服務命令
# systemctl start name.service #啟動服務
# systemctl stop name.service #停止服務
# systemctl restart name.service #重啟服務
# systemctl enable name.service #增加開機啟動
# systemctl disable name.service #刪除開機啟動
.service 可以省略

tomcat增加啟動參數:tomcat 需要增加一個pid文件

# vi tomcat/bin/setenv.sh
#add tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#add java opts
JAVA_OPTS="-server -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=1024m -Xms1024M -Xmx2048M -XX:MaxNewSize=256m"

增加tomcat.service

vim /usr/lib/systemd/system/tomcat.service
Unit]
Description=Tomcat
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/opt/tomcat8/tomcat.pid
ExecStart=/opt/tomcat8/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=user
Group=user

[Install]
WantedBy=multi-user.target

Tomcat  Service控制

# systemctl enable tomcat
# systemctl start tomcat

tomcat在啟動時候,執行start不會啟動兩個tomcat,保證始終只有一個tomcat服務在運行。

For more, refer to: http://www.jianshu.com/p/29ecd624046f

Tomcat配置管和理

Docs and References

https://tomcat.apache.org/tomcat-7.0-doc/config/http.html

Tomcat Max threads & Thread Pool

Tomcat 7 default Max threads

  • http-bio-8080 Max threads: 200
  • http-bio-8443 Max threads: 200
  • ajp-bio-8009 Max threads: 200

Set up maxThreads

<Connector executor="tomcatThreadPool" port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="/opt/tomcat-ddtservice/ddtservice.192.168.1.90.keystore" keystorePass="***" clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8"/>

Thread Pool

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />

<Connector executor="tomcatThreadPool" port="8090" protocol="HTTP/1.1"  connectionTimeout="45000" redirectPort="8443" URIEncoding="UTF-8"/>

<Connector port="8443" executor="tomcatThreadPool" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="/opt/tomcat-ddtservice/***.keystore" keystorePass="***"
clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8"/>

maxThreads vs maxConnections

Tomcat can work in 2 modes:

  • BIO (one thread per connection), or
  • NIO (many more connections than threads).

Tomcat7 is BIO by default, although consensus seems to be "don't use Bio because Nio is better in every way". You set this using the "protocol" parameter in the server.xml file - BIO will be "HTTP1.1" or "org.apache.coyote.http11.Http11Protocol" and NIO will be "org.apache.coyote.http11.Http11NioProtocol"

If you're using BIO then I believe they should be more or less the same. If you're using NIO then actually "maxConnections=1000" and "maxThreads=10" might even be reasonable. The defaults are maxConnections=10,000 and maxThreads=200. With NIO, each thread can server any number of connections, switching back and forth but retaining the connection so you don't need to do all the usual handshaking which is especially time-consuming with HTTPS but even an issue with HTTP. You can adjust the "keepAlive" parameter to keep connections around for longer and this should speed everything up.

關於Keep-alive設置

maxKeepAliveRequests    If not specified, this attribute is set to 100.  一般設置100~200.

keepAliveTimeout       The default value is to use the value that has been set for the connectionTimeout attribute.

<Connector executor="tomcatThreadPool" port="8090" protocol="HTTP/1.1" connectionTimeout="45000" redirectPort="8443" URIEncoding="UTF-8"/>

 

Tomcat Manager配置

http://www.365mini.com/page/tomcat-manager-user-configuration.htm

conf/tomcat-users.xml
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
<user username="admin" password="123456" roles="manager-script"/>
</tomcat-users>

Tomcat Manager 4種角色的大致介紹(下面URL中的*為通配符):
manager-gui
    允許訪問html接口(即URL路徑為/manager/html/*)
manager-script
    允許訪問純文本接口(即URL路徑為/manager/text/*)
manager-jmx
    允許訪問JMX代理接口(即URL路徑為/manager/jmxproxy/*)
manager-status
    允許訪問Tomcat只讀狀態頁面(即URL路徑為/manager/status/*)

  • manager-gui - allows access to the HTML GUI and the status pages
  • manager-script - allows access to the text interface and the status pages
  • manager-jmx - allows access to the JMX proxy and the status pages
  • manager-status - allows access to the status pages only

The HTML interface is protected against CSRF but the text and JMX interfaces are not. 

Tomcat8 manager 默認本機訪問,外部訪問403未授權

新建conf/Catalina/localhost/manager.xml 內容如下:無須重啟tomcat即可生效
<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
    <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

Doc: Each deployed webapp has a context.xml file that lives in $CATALINA_BASE/conf/[enginename]/[hostname] (conf/Catalina/localhost by default) and has the same name as the webapp (manager.xml in this case). If no file is present, default values are used.
So, you need to create a file conf/Catalina/localhost/manager.xml and specify the rule you want to allow remote access.

Host Manager application

 

  • admin-gui - allows access to the HTML GUI and the status pages
  • admin-script - allows access to the text interface and the status pages

Tomcat class不用重啟更新的辦法

1. tomcat server.xml: 設置reloadable="false"
<Context docBase="DDTService" path="/ddt" reloadable="false" source="org.eclipse.jst.jee.server:DDTService"/>
2. 在eclipse中以debug模式啟動Tomcat。

Tomcat控制台中文亂碼解決辦法

  1. 打開文件/tomcat/bin/catalina.bat
  2. set JAVA_OPTS= 的內容中添加選項-Dfile.encoding=GBK
  3. 重啟tomcat即可

(Jenkins console中文亂碼也隨即解決了)   

Tomcat環境下解決http 的get方式提交的中文亂碼問題

Tomcat在處理get和post請求的時候處理方式不同。

POST請求是將參數存放在請求數據包的消息體中

所以使用request.setCharacterEncoding("utf-8");可以處理

但是GET請求是將參數存放在url中,此時setCharacterEncoding就不起作用了,此時我們需要采用手寫代碼進行轉碼。

當然我們也可以修改tomcat配置文件來處理get請求的轉碼

方法一:改代碼

String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

方法二:改Tomcat下的server.xml配置文件下的Connector元素

添加:URIEncoding="UTF-8"  注1:是URIEncoding而不是URLEncoding

或者添加:useBodyEncodingForURI="true"

設置Java參數

bin\setenv.bat
set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m

or bin/setenv.sh
export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms512m -Xmx2048m -XX:PermSize=128m -XX:MaxPermSize=768m"

中文設置bin\setenv.bat
set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=GBK

JDK8中用metaspace代替permsize,因此在許多我們設置permsize大小的地方同樣需要修改配置為metaspace

將-XX:PermSize=200m;-XX:MaxPermSize=256m;

修改為:-XX:MetaspaceSize=200m;-XX:MaxMetaspaceSize=256m;

Tomcat java.lang.OutOfMemoryError: PermGen space

對tomcat,可以在catalina.bat中添加:
 "set CATALINA_OPTS=-Xms128M -Xmx256M
  set JAVA_OPTS=-Xms128M -Xmx256M",或者把%CATALINA_OPTS%和%JAVA_OPTS%代替為-Xms128M -Xmx256M

Tomcat java opts

setenv.bat

set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx1024m -XX:MaxPermSize=256m -server"

Tomcat in Eclipse out of memory: PermGen Size

解決方案為通過添加下面的參數增加分配給JVM的內存空間
-Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxNewSize=256m -XX:MaxPermSize=512m
網上的解決方案多半是針對純Tomcat的情況,並非通過Eclipse啟動的Tomcat。
或者修改eclipse.ini配置文件,不過這些方法都不適合Eclipse運行Tomcat拋出該異常的情況。
修改eclipse.ini配置文件增大的是Ecipse開發平台本身運行的JVM的空間,並非Eclipse啟動Tomcat的內存空間。
正確的方法是,點擊“Run” – “Run Configurations…”,選中Tomcat Server,進入Arguments分頁,在VM arguments中加入提升初始分配空間的參數

解決Tomcat無法shutdown進程

確定是web應用的問題,忽略日志中的嚴重警告,因為這是關閉tomcat時候引起的,正常情況下不會發生這種內存泄露情況,而且Tomcat6.18以上版本的Tomcat已經 做了內存泄露保護,交給Tomcat完成吧,我們只需要在shutdown.sh之后,補上一個kill -9 pid即可。要是嫌這樣太麻煩了,可以如下這樣改:

==============================bin/shutdown.sh

exec "$PRGDIR"/"$EXECUTABLE" stop  -force "$@" #加上 -force  

==============================bin/catalina.sh 

在PRGDIR=`dirname "$PRG"`后面加上

if [ -z "$CATALINA_PID" ]; then

      CATALINA_PID=$PRGDIR/CATALINA_PID

      cat $CATALINA_PID

fi

Tomcat解壓版注冊Windows服務

ependecy:bin\目錄下需要service.bat and Tomcat7.exe
    set "USER_INSTALL_DIR=C:\sd\install\DDT3forProduction"
    set "JAVA_HOME=%USER_INSTALL_DIR%\jdk1.6.0_45"   
    set "CATALINA_HOME=%USER_INSTALL_DIR%\apache-tomcat-7.0.52"
    service.bat install DDT3Tomcat
    net start DDT3Tomcat
    net stop DDT3Tomcat
    service.bat uninstall DDT3Tomcat



C:\sd\install\DDT3forProduction\apache-tomcat-7.0.52\bin>service.bat install
Installing the service 'Tomcat7' ...
Using CATALINA_HOME:    "C:\sd\install\DDT3forProduction\apache-tomcat-7.0.52"
Using CATALINA_BASE:    "C:\sd\install\DDT3forProduction\apache-tomcat-7.0.52"
Using JAVA_HOME:        "C:\sd\install\DDT3forProduction\jdk1.6.0_45"
Using JRE_HOME:         "C:\sd\install\DDT3forProduction\jdk1.6.0_45\jre"
Using JVM:              "C:\sd\install\DDT3forProduction\jdk1.6.0_45\jre\bin\server\jvm.dll"
拒絕訪問。
Failed to install serviceFailed installing 'Tomcat7' service
A: cmd.exe以管理員身份運行

net stop tertonDXTomcat

 

Tomcat undeploy and deploy war

#undeploy
wget --http-user=admin --http-password=admin "http://server206:8080/manager/text/undeploy?path=/ddt" -O -
#deploy
wget --http-user=admin --http-password=admin "http://server206:8080/manager/text/deploy?war=file:C:/Documents and Settings/Administrator/.jenkins/workspace/DDT3/DDTService/ddt.war&path=/ddt" -O -

Wget for Windows:

Source: http://www.gnu.org/software/wget/
Source: http://mirror.hust.edu.cn/gnu/wget/
Binary: http://www.onlinedown.net/softdown/80943_2.htm
Add wget home to system env path, restart tomcat to let Jenkins know the path.

Tomcat修改上傳war大小限制

http://server206:8080/manager    admin:admin    deploy war failure:
the request was rejected because its size (120592431) exceeds the configured maximum (52428800)
tomcat/webapps/manager/WEB-INF/web.xml
找到
      <multipart-config>
        <!-- 50MB max -->
        <max-file-size>52428800</max-file-size>
        <max-request-size>52428800</max-request-size>
        <file-size-threshold>0</file-size-threshold>
      </multipart-config>
    </servlet>
    <servlet>
修改為
    <!-- 100MB max -->
       <max-file-size>104758600</max-file-size>
       <max-request-size>104758600</max-request-size>
       <file-size-threshold>0</file-size-threshold>
     </multipart-config>
http://blog.csdn.net/b1412/article/details/7056250

 

Tomcat設置提交參數的最大值

關於maxPostSize,tomcat默認是2M,單位為字節。maxPostSize=”0”則表示不限制大小。

https://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html

maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="5120000"/>

 

Tomcat運行狀態war增加jar

http://linuxproblems.org/wiki/Add_or_update_files_in_a_war_file

http://blog.omnidarren.com/2013/07/install-jar-command-on-centos-6/

http://www.coderanch.com/t/619252/Tomcat/Tomcat-find-classes-WEB-INF

http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html

javax.servlet.ServletException: Error instantiating servlet class com.cci.framework.core.servlet.BarCodeServlet

   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)

        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)

        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)

org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)

org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)

        org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)

        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

       org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

        java.lang.Thread.run(Thread.java:744)

root cause

java.lang.Error: Unresolved compilation problems:

        The import org.jbarcode cannot be resolved

        The import org.jbarcode cannot be resolved

        The import org.jbarcode cannot be resolved

        The import org.jbarcode cannot be resolved

        JBarcode cannot be resolved to a type

        JBarcode cannot be resolved to a type

        Code128Encoder cannot be resolved

。。。

 

C:\Users\Mark>jar -uf C:\sd\releases\Production20151015\20151015_190545\ddt.war C:\sd\eclipse-jee-luna-SR1-win32\eclipse\workspace\DDTService\web\WEB-INF\lib\jbarcode-0.2.8.jar

Tomcat 修改默認ROOT路徑

在<host></host>標簽之間添加上:
<Context path="" docBase="myjsp" debug="0" reloadable="true" />
path是說明虛擬目錄的名字,如果你要只輸入ip地址就顯示主頁,則該鍵值留為空;
docBase是虛擬目錄的路徑,它默認的是$tomcat/webapps/ROOT目錄,現在我在webapps目錄下建了一個myjsp目錄,讓該目錄作為我的默認目錄。
debug和reloadable一般都分別設置成0和true。

Tomcat HTPPS設置

https://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html

Introduction to SSL/TLS

the data being sent is encrypted by one side

two-way process

 

Tips

  1. asked if he or she wishes to accept the Certificate at first time visit
  2. encryption/decryption is a computationally expensive process from a performance standpoint, It is not strictly necessary to run an entire web application over SSL, and indeed a developer can pick and choose which pages require a secure connection and which do not.

sensitive information:

  1.                          i.              login pages
  2.                        ii.              personal information pages
  3.                       iii.              shopping cart checkouts: credit card information
  4. using name-based virtual hosts on a secured connection can be problematic. If the domain names do not match, these browsers will display a warning to the client user.

 

JMX監控Tomcat

set JAVA_OPTS=-Dfile.encoding=GBK -Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m -Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"

Linux必須指定erver,否則連不上:
export JAVA_OPTS="-Dfile.encoding=UFT-8 -Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=120.24.88.139 -Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Tomcat session設置

三種方式優先級:1 < 2 <3
1. [tomcat home]\conf\web.xml:Tomcat默認session超時時間為30分鍾,可以根據需要修改,負數或0為不限制session失效時 間。
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
2. 在工程的web.xml中設置
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
3. 通過java代碼設置
session.setMaxInactiveInterval(30*60);//以秒為單位,即 在沒有30分鍾活動后,session將失效。

防止session超時的編程方法

http://zhumeng8337797.blog.163.com/blog/static/100768914201361622331186/

 

Jenkins in Tomcat

http://server206:8080/jenkins/job/DDT3

http://server206:8080/ddt

Email-ext:

https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin
    email-templates should be put in Jenkins home instead of jenkins directory in tomcat home (Jenkins>Configurations:Home directory)
        Home Directory:    C:\Documents and Settings\Administrator\.jenkins            /home/httpd/jenkins
        Workspace Root Directory: ${JENKINS_HOME}/workspace/${ITEM_FULLNAME}    ${ITEM_ROOTDIR}/workspace
        Build Record Root Directory: ${ITEM_ROOTDIR}/builds        ${ITEM_ROOTDIR}/builds
       
    Global properties
    Environment variables
        rooturl=http://server206:8080/jenkins/
        lang=zh_CN.GBK
    Tool Locations
        Ant home

 

::undeploy

wget --http-user=admin --http-password=admin "http://192.168.1.155:8080/manager/text/undeploy?path=/ddt" -O -

::deploy

wget --http-user=admin --http-password=admin "http://192.168.1.155:8080/manager/text/deploy?war=file:\\server206\Jenkins_workspace\DDT3Service\DDTService\ddt.war&path=/ddt" -O -

 

mount -o username=administrator //192.168.1.206/Jenkins_workspace /mnt

::undeploy

wget --http-user=admin --http-password=admin "http://192.168.1.155:8080/manager/text/undeploy?path=/ddt" -O -

::deploy

wget --http-user=admin --http-password=admin "http://192.168.1.155:8080/manager/text/deploy?war=file:/mnt/DDT3Service/DDTService/ddt.war&path=/ddt" -O –

 

Tomcat錯誤解決

java.io.NotSerializableException錯誤解決方法

十二月 24, 2015 8:03:15 下午 org.apache.catalina.session.StandardManager doLoad

嚴重: IOException while loading persisted sessions: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.cci.ddt.gov.view.ViewForShareHolder

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.cci.ddt.gov.view.ViewForShareHolder

        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)

http://blog.csdn.net/forandever/article/details/4553447

原因是:tomcat停止時,保存session資源,然后在重啟服務后,會嘗試恢復session。

在server.xml中Context下添加如下的內容:

<Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="false"/>

or

<Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="false">

        <Store className="org.apache.catalina.session.FileStore"/>

    </Manager>

 

Tomcat 8

OpenJDK 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0

OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0

 

http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html

Compatibility Guide for JDK 8 says that in Java 8 the command line flag MaxPermSize has been removed.

The reason is that the permanent generation was removed from the hotspot heap and was moved to native memory.

 

Error parsing HTTP request header: java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

十月 12, 2017 1:52:25 下午 org.apache.coyote.http11.AbstractHttp11Processor process
信息: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    at org.apache.coyote.http11.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:189)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1000)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745) 

Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了對於http頭的驗證。

RFC3986文檔規定,Url中只允許包含英文字母(a-zA-Z)、數字(0-9)、-_.~4個特殊字符以及所有保留字符。

RFC3986中指定了以下字符為保留字符:

!*'();:@&=+$,/?#[]

解決辦法:
配置tomcat的catalina.properties
添加或者修改:
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
當然還有另外一種方法,就是將所有的參數都進行編碼

 

 

 

 

Tomcat+java的web程序持續占cpu問題調試解決方法:

1、先用top查看占用cpu的進程id
$ top
top - 12:41:04 up 12 days, 20:46,  3 users,  load average: 4.77, 5.01, 4.95
Tasks: 223 total,   2 running, 221 sleeping,   0 stopped,   0 zombie
%Cpu(s): 93.5 us,  0.2 sy,  0.0 ni,  6.3 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16301388 total,  7697092 free,  5016448 used,  3587848 buff/cache
KiB Swap:  8191996 total,  8191996 free,        0 used. 10916832 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                         
27127 user      20   0 7951404 3.337g  14064 S 556.8 21.5 294:12.37 java                                                            
32197 user      20   0 5531164  21744  10256 S   3.7  0.1   0:00.11 jstack                                                          
 1900 mysql     20   0 3580036 499996   8168 S   1.0  3.1  80:09.60 mysqld                                                          
32060 user      20   0  146144   2128   1436 R   0.7  0.0   0:00.84 top                                                             
 2530 user      20   0  470192   5932   3668 S   0.3  0.0   0:07.09 ibus-daemon                                                     
22415 root      20   0       0      0      0 S   0.3  0.0   0:04.50 kworker/4:0                                                     
    1 root      20   0  193092   8444   2612 S   0.0  0.1   1:13.95 systemd                                                         
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.60 kthreadd                                                        
    3 root      20   0       0      0      0 S   0.0  0.0   0:04.79 ksoftirqd/0                                                     
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                    
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.69 migration/0                                                     
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                                          
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/0                                                         
   10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1                                                         
   11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/2                                                         
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/3                                                         
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/4                                                         
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/5                                                         
   15 root      20   0       0      0      0 S   0.0  0.0   3:05.18 rcu_sched                                                       
   16 root      20   0       0      0      0 S   0.0  0.0   0:33.15 rcuos/0
2、再用ps -ef | grep PID定位具體的進程主體;如是否是tomcat啟動的java程序
3、用ps -mp pid -o THREAD,tid,time打印出該進程下的線程占用cpu情況: ps -mp 27127 -o THREAD,tid,time
        找到了耗時最高的線程28802,占用CPU時間快兩個小時了!
[user@yfddt6Z ~]$ ps -mp 27127 -o THREAD,tid,time
USER     %CPU PRI SCNT WCHAN  USER SYSTEM   TID     TIME
user      4.1   -    - -         -      -     - 05:16:47
user      0.0  19    - futex_    -      - 27127 00:00:00
user      0.0  19    - poll_s    -      - 27128 00:00:00
user      0.5  19    - -         -      - 27130 00:44:40
user      0.5  19    - -         -      - 27131 00:44:40
user      0.5  19    - -         -      - 27132 00:44:40
user      0.5  19    - -         -      - 27133 00:44:40
user      0.5  19    - -         -      - 27134 00:44:39
user      0.5  19    - -         -      - 27135 00:44:39
user      0.0  19    - futex_    -      - 27136 00:03:20
user      0.0  19    - futex_    -      - 27137 00:00:00
user      0.0  19    - futex_    -      - 27138 00:00:03
user      0.0  19    - futex_    -      - 27139 00:00:00
user      0.0  19    - futex_    -      - 27140 00:01:05
user      0.0  19    - futex_    -      - 27141 00:00:47
user      0.0  19    - futex_    -      - 27142 00:00:00
user      0.0  19    - futex_    -      - 27143 00:03:33
user      0.0  19    - futex_    -      - 27144 00:00:00
user      0.0  19    - futex_    -      - 27147 00:00:07

4、其次將需要的線程ID轉換為16進制格式:printf "%x\n" tid
printf "%x\n" 27130
5、最后打印線程的堆棧信息:jstack pid | grep tid -A 30
        找到出現問題的代碼,並分析具體函數中是否有可能出現死循環的代碼段。
        通常問題出現在while, for之類的循環代碼片段。
[user@yfddt6Z ~]$ /usr/java/jdk1.7.0_79/bin/jstack 27127 | grep 69fa -A 30
27127: Unable to open socket file: target process not responding or HotSpot VM not loaded
The -F option can be used when the target process is not responding
[user@yfddt6Z ~]$ /usr/java/jdk1.7.0_79/bin/jstack -F 27127 | grep 69fa -A 30
Attaching to process ID 27127, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02
        
        
jstack command not found on centos
[root@yfddt5Z logs]# sudo updatedb
[root@yfddt5Z logs]# locate jstack
/usr/java/jdk1.7.0_79/bin/jstack
/usr/java/jdk1.7.0_79/man/ja_JP.UTF-8/man1/jstack.1
/usr/java/jdk1.7.0_79/man/man1/jstack.1

jstack: Unable to open socket file: target process not responding or HotSpot VM not loaded

設置Tomcat tmp目錄到linux系統的tmp目錄

[user@yfddt6Z tomcat7]$ top
top - 14:53:40 up 176 days,  2:46,  4 users,  load average: 5.28, 4.84, 2.76
Tasks: 230 total,   1 running, 226 sleeping,   2 stopped,   1 zombie
%Cpu(s): 94.0 us,  0.1 sy,  0.0 ni,  5.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16301388 total,   204244 free,  4237368 used, 11859776 buff/cache
KiB Swap:  8191996 total,  8191028 free,      968 used. 11471412 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                         
32119 user      20   0 6735608 2.340g  16612 S 565.7 15.0  67:57.98 java                                                            
    1 root      20   0  206972  22312   2620 S   0.0  0.1  10:29.64 systemd                                                         
    2 root      20   0       0      0      0 S   0.0  0.0   0:04.05 kthreadd                                                        
    3 root      20   0       0      0      0 S   0.0  0.0   0:22.83 ksoftirqd/0                                                     
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                    
    7 root      rt   0       0      0      0 S   0.0  0.0   0:03.51 migration/0                                                     
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                                          
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/0                                                         
   10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1                                                         
   11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/2                                                         
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/3                                                         
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/4                                                         
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/5                                                         
   15 root      20   0       0      0      0 S   0.0  0.0  32:05.79 rcu_sched                                                       
   16 root      20   0       0      0      0 S   0.0  0.0   4:52.26 rcuos/0                                                         
   17 root      20   0       0      0      0 S   0.0  0.0   4:55.87 rcuos/1                                                         
   18 root      20   0       0      0      0 S   0.0  0.0   4:54.44 rcuos/2                                                         
   19 root      20   0       0      0      0 S   0.0  0.0   4:57.84 rcuos/3                                                         
   20 root      20   0       0      0      0 S   0.0  0.0   4:52.57 rcuos/4                                                         
   21 root      20   0       0      0      0 S   0.0  0.0   5:15.51 rcuos/5    

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM