前言
Java VisualVM是jdk自带一款工具,可以十分友好的监控java进程相关的应用服务及中间件。
工具位置
jdk的bin目录下,找到jvisualvm.exe,双击打开即可。
功能介绍
1、抽样器和profiler,这两个差不多,用其中一个即可。

比如抽样器,点击CPU,就可以看到各个类以及方法执行的时间,可以监控哪个类的方法执行时间较长,一眼就能定位到具体的异常方法。

点击内存,也一样,很直观的就能找到哪个位置可能存在内存泄漏的情况。

2、安装visualGC插件
直接在java visualVM上安装是安装不上的,要去官网下载插件。
地址:https://visualvm.github.io/pluginscenters.html
java -version查一下java版本,然后根据版本下载对应的插件。


然后点击工具-插件,将地址改为正确的地址:

然后就可以安装插件了,勾选可用插件中的这两个:BTrace Workbench和Visual GC

点击安装即可。
装完后:


3、JVisualVM监控tomcat(在第2点的基础上)
1)、配置要监控的远程tomcat中的catalina.sh文件,加入以下部分:
找到JAVA_OPTS,在上方加入:(主要是端口port和主机hostname,记住端口不要和服务器上已经占用的发生冲突,如果是云服务器要开放端口。)
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.net.preferIPv4Stack=true -Djava.rmi.server.hostname=39.108.70.86"

保存,重启。
2)、远程-右键,添加主机:

添加jmx连接:


然后就和监控本地一样了!
总结:
个人经过测试,监控内网服务器没问题,但是阿里云服务器远程连接会报错:【无法使用 service:jmx:rmi:///jndi/rmi:///jmxrmi 连接到9004】,这是因为云服务器自己的安全策略阻挡了远程监控,所以VisualVM最好的使用场景是远程监控局域网内的服务器。
参考:https://coding.imooc.com/class/241.html,有兴趣的可以看看这门课程,这是我非常喜欢的一位老师。
网友评论