一. 打开VisualVM
D:\soft\tools\jdk_18.0_152\bin\jvisualvm.exe(本地jdk安装目录下的bin文件夹)
二. Jstatd方式
文件命名为 jstatd.all.policy,放到AVA_HOME/bin中,其内容如下,
grant codebase"file:${java.home}/../lib/tools.jar" { permissionjava.security.AllPermission; }; grant codebase"file:${java.home}/../lib/tools.jar" { permissionjava.security.AllPermission; }; |
执行命令:
jstatd -J-Djava.security.policy=jstatd.all.policy jstatd -J-Djava.security.policy=jstatd.all.policy |
关键点来了,本人连接的是Linux系统,hsotname -i 命令输出的一定要是本机真实IP地址,不能是127.0.0.1。如何修改?
1. hostname 命令查看本机name,假如为king。
2. 在hosts文件中添加HOST。 格式为:本机IP <HostName>,如192.168.1.8king
做完后再用hostname -i查看.
完成后再重新执行一次命令即可,然后打开visualVm,添加远程主机,系统会自动显示.
三. JMX链接
启动文件根据监控的服务不同而各异.一般tomcat的是start.sh,jboss的是run.sh,或者一些其他自定义的文件.
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=11020 -Djava.rmi.server.hostname=192.168.20.92 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=11020 -Djava.rmi.server.hostname=192.168.20.92 |
解释如下:
-Dcom.sun.management.jmxremote.port 远程主机端口号的 -Dcom.sun.management.jmxremote.ssl=false 是否使用SSL连接 -Dcom.sun.management.jmxremote.authenticate=false 是否开启远程服务权限 -Djava.rmi.server.hostname 远程主机名,使用IP地址 -Dcom.sun.management.jmxremote.port 远程主机端口号的 -Dcom.sun.management.jmxremote.ssl=false 是否使用SSL连接 -Dcom.sun.management.jmxremote.authenticate=false 是否开启远程服务权限 -Djava.rmi.server.hostname 远程主机名,使用IP地址 |
如果需要密码访问,可进行以下设置:
-Djava.rmi.server.hostname=192.168.1.8 -Dcom.sun.management.jmxremote.port=8088 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=/usr/java/default/jre/lib/management/jmxremote.password -Dcom.sun.management.jmxremote.access.file=/usr/java/default/jre/lib/management/jmxremote.access |
其中,jmxremote.access, jmxremote.password并不是一开始就有的,在java_home/jre/lib/mangement下有.
jmxremote.access内容
admin readwrite |
jmxremote.password内容
admin 123456 |
两个文件授权(必须按下面方式授权,chmod777都不行)
chmod 600 jmxremote.access chmod 600 jmxremote.password chown root:root jmxremote.access chown root:root jmxremote.password |
最后打开visualVm,添加远程主机,右键,添加JMX链接,加上端口号链接即可.
chmod 600 jmxremote.access chmod 600 jmxremote.password chown root:root jmxremote.access chown root:root jmxremote.password |
虽然已经对主监听端口设置了允许访问,但是随机端口号是java进程启动后,OS随机分配给jmxserver的,如果不关闭防火墙,就必须在每次server就绪后,检测一下随机端口,然后设置为允许访问。