一、环境
远程服务器:Linux Red Hat 4.8.5-16
远程JDK: 1.8
远程tomcat: 8.5.12
本地Win10
二、准备工作 需要开通服务器上的两个远程监控端口,我使用的是8100 和 8101
使用宝塔非常容易开通端口。
开放端口及查看已开放的端口可以参考:https://blog.csdn.net/lvqingyao520/article/details/81075094
三、tomcat 脚本修改setenv.sh
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=服务器ip
-Dcom.sun.management.jmxremote.port=8100
-Dcom.sun.management.jmxremote.rmi.port=8101
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.password.file=/usr/java/jdk1.8.0_121/jre/lib/management/jmxremote.password
-Dcom.sun.management.jmxremote.access.file=/usr/java/jdk1.8.0_121/jre/lib/management/jmxremote.access"
如果authenticate=false的时候不需要配置paasword.file和access.file.
四、修改JDK 的配置文件
安全起见,这里设定authenticate=true
在/usr/java/jdk1.8.0_121/jre/lib/management 中copy jmxremote.password.template 另存为 jmxremote.password
可以修改密码
monitorRole tomcat
controlRole tomcat
五、启动tomcat
Error: Password file read access must be restricted: /usr/java/jdk1.8.0_121/jre/lib/management/jmxremote.password
修改jmxremote.password 文件权限,password.file的读写权限是400,仅运行tomcat服务的操作系统用户可以访问
# chmod 0400 jmxremote.password
重新启动OK
六、回到本地启动 jvisualvm.exe
连接成功
PS: 一开始tomcat 配置在 JAVA_OPTS,shutdown tomcat的时候报错
Error: JMX connector server communication error: service:jmx:rmi://iZwz91ho98e6b56xsjkmcpZ:8101
因为添加到JAVA_OPTS中的配置会在启动和关闭的时候都尝试运行,所以关闭tomcat的时候就会报端口已在使用的提示。解决办法很简单,把监控的配置加入到CATALINA_OPTS即可,这里的配置只会在tomcat启动的时候运行。