本文使用的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是JMX远程使用的端口号,可以根据需要修改。-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,打开控制台,然后建立连接,填写地址、端口号、用户名和口令即可连接。