本文使用的tomcat版本為7.0.77。
要通過JMX遠程監控Tomcat,首先需要進行Tomcat的JMX遠程配置。
注意:此配置添加在catalina.bat文件開頭的注釋行(rem或#)后面即可。
不需鑒權的配置:
先修改Tomcat的啟動腳本,windows下為bin/catalina.bat(linux下為catalina.sh),添加以下內容:
set JMX_REMOTE_CONFIG=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false set CATALINA_OPTS=%CATALINA_OPTS% %JMX_REMOTE_CONFIG%
linux為 JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=8999,是jmxremote使用的端口號,可修改。
-Dcom.sun.management.jmxremote.authenticate=false,表示不需要鑒權,主機+端口號即可監控。
需要鑒權的配置:
set JMX_REMOTE_CONFIG=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access set CATALINA_OPTS=%CATALINA_OPTS% %JMX_REMOTE_CONFIG%
復制並修改授權文件
JAVA_HOME/jre/lib/management下有jmxremote.access和jmxremote.password.template的模板文件,將兩個文件復制到CATALINA_BASE/conf目錄下
*重命名jmxremote.password.template文件為jmxremote.password
*修改CATALINA_BASE/conf/jmxremote.access 添加內容:
monitorRole readonly
controlRole readwrite
*修改CATALINA_BASE/conf/jmxremote.password 添加內容:
monitorRole liuke #可自定義
controlRole liuke #可自定義
使用jconsole或jvisualvm測試JMX
運行JAVA_HOME/bin目錄下的jconsole或jvisualvm,打開控制台,然后建立連接,填寫地址、端口號、用戶名、口令即可連接。