---
title: "远程JMX启动配置，用于jvisualvm工具监测使用"
author: "wangguorui89"
date: "2015-06-15T11:14:47Z"
tags: []
slug: "jmx-jvisualvm"
draft: false
lastmod: "2015-06-15T11:35:17Z"
id: 2219680
url: "https://wangguorui89.iteye.com/blog/2219680"
---

-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=127.0.0.1   
\-Dcom.sun.management.jmxremote.port=8009  
\-Dcom.sun.management.jmxremote.ssl=false  
\-Dcom.sun.management.jmxremote.authenticate=false

 

`1` `export CATALINA_OPTS="$CATALINA_OPTS`

`1` `-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=xxx.xxx.xxx.xxx  -Dcom.sun.management.jmxremote.port=xxx -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"`

参数说明：

 

`1` `-Djava.rmi.server.hostname=xxx.xxx.xxx.xxx 主机地址，我配置的是外网地址`

`1` `-Dcom.sun.management.jmxremote.port=xxx  端口号、配置完端口号之后需要将此端口开放出来  可以使用命令：iptables -A INPUT -p tcp --d port  端口号 -j ACCEPT`

`1` `-Dcom.sun.management.jmxremote.authenticate=true` `是否开启认证、false的时候可以不使用密码访问`

`1` `-Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access  以上配置是开启认证时使用的access文件和password文件`

其中jmxremote.access文件的内容可以配置为：

`1` `username1 readonly`

`2` `username2 readwrite`

jmxremote.password文件可以配置为：

`1` `username1 password1`

`2` `username2 password2`

将以上俩文件放在对应配置目录tomcat/conf/下

并且将以上俩个文件访问权限配置成：

`1` `chmod 600` `jmx*`

如果没有这一步、使用startup启动不了tomcat。

按照上述配置、再使用startup.sh启动tomcat。

接下来就是使用jvisualvm来远程监控tomcat了

右键点击远程-添加远程主机-输入主机ip地址

[![](http://static.oschina.net/uploads/space/2013/0827/161014_dnoT_146430.png)](http://static.oschina.net/uploads/space/2013/0827/161014_dnoT_146430.png)

[![](http://static.oschina.net/uploads/space/2013/0827/161026_bidh_146430.png)](http://static.oschina.net/uploads/space/2013/0827/161026_bidh_146430.png)

添加远程主机之后，点击远程主机右键-添加jmx连接：

[![](http://static.oschina.net/uploads/space/2013/0827/161216_lIXy_146430.png)](http://static.oschina.net/uploads/space/2013/0827/161216_lIXy_146430.png)

弹出登录框

[![](http://static.oschina.net/uploads/space/2013/0827/161301_b53F_146430.png)](http://static.oschina.net/uploads/space/2013/0827/161301_b53F_146430.png)

输入主机名:端口号，如果配置了用户名和密码，请输入用户名和密码。然后点击确定。

点击确定之后、在远程主机下会有一个jmx连接，打开连接，点击监视tab，可以看到如下界面了。

[![](http://static.oschina.net/uploads/space/2013/0827/161509_miAJ_146430.jpg)](http://static.oschina.net/uploads/space/2013/0827/161509_miAJ_146430.jpg)

在上图中可以看到cpu利用率和垃圾回收活动（这个在分析tomcat性能时也很重要）。然后是堆栈使用情况。下面是类的使用情况，最后一个是线程活动情况。

点击线程tab可以看到：

[![](http://static.oschina.net/uploads/space/2013/0827/161831_SIY1_146430.png)](http://static.oschina.net/uploads/space/2013/0827/161831_SIY1_146430.png)

上图可以非常清晰的看到线程活动情况，那些线程正在执行，哪些线程正在等待中，以及执行完毕的线程等。

这里可以看到每个线程的状态，点击某个线程右键可以查看该线程的详细情况：

[![](http://static.oschina.net/uploads/space/2013/0827/162045_ZkaY_146430.png)](http://static.oschina.net/uploads/space/2013/0827/162045_ZkaY_146430.png)

使用左上角的线程dump按钮，还可以看到线程的堆栈情况，这样就可以具体分析线程是在什么地方进入等待，什么地方进入休眠，以及什么地方一直处于执行状态。这也是一个性能分析利器哦！

[![](http://static.oschina.net/uploads/space/2013/0827/174110_s01F_146430.png)](http://static.oschina.net/uploads/space/2013/0827/174110_s01F_146430.png)

 

以上就是使用jvisualvm的jmx远程监控tomcat的简单配置。