在网上查找了一些资料,经过一一测试,来做下总结.
有两种方式进行远程监控,Jstatd链接和JMX链接.
一. 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
解释如下:
- <span style="font-size:14px;">-Dcom.sun.management.jmxremote.port 远程主机端口号的
- -Dcom.sun.management.jmxremote.ssl=false 是否使用SSL连接
- -Dcom.sun.management.jmxremote.authenticate=false 是否开启远程服务权限
- -Djava.rmi.server.hostname 远程主机名,使用IP地址</span>
<span style="font-size:14px;">-Dcom.sun.management.jmxremote.port 远程主机端口号的
-Dcom.sun.management.jmxremote.ssl=false 是否使用SSL连接
-Dcom.sun.management.jmxremote.authenticate=false 是否开启远程服务权限
-Djava.rmi.server.hostname 远程主机名,使用IP地址</span>
注意:这些配置都不可少,我开始少了hostname,搞了半天也没连接成功.
如果需要密码访问,可进行以下设置:
- -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
-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
monitorRole QED (QED为密码)
controlRole R&D
jmxremote.access在JAVA_HOME\jre\lib\management下有模板
jmxremote.access 中显示
monitorRole readonly
controlRole readwrite
最后打开visualVm,添加远程主机,右键,添加JMX链接,加上端口号链接即可.