jvisualvm远程监控jvm设置

  有些时候,需要对特定环境中的Java应用进行实时性能分析,大部分非开发和测试环境(这两者可以使用jprofiler,最佳java性能诊断工具),一般都是用jvisualvm进行基本检测以最小化对系统的影响(其开启后,负载影响大约20%—30%),jvisualvm没有提供cli模式,只提供了GUI。在centos 6之后,默认不在安装图形化窗口如gnome,在有些生产环境中,因为网络和其他限制,无法通过系统盘或者yum安装相关界面。此时我们就需要预先在java启动脚本中进行JMX开启,在tomcat中,如下:

  export CATALINA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=1099 -Djava.rmi.server.hostname=172.18.30.193 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

172.18.30.193为本机IP地址。

  代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099; nested exception is: 
java.net.BindException: Address already in use: JVM_Bind

  上述选项必须设置在CATALINA_OPTS变量中,设置在JAVA_OPTS中,就会出现该问题。

# CATALINA_OPTS (Optional) Java runtime options used when the "start",
# "run" or "debug" command is executed.
# Include here and not in JAVA_OPTS all options, that should
# only be used by Tomcat itself, not by the stop process,
# the version command etc.
# Examples are heap size, GC logging, JMX ports etc.

# JAVA_OPTS (Optional) Java runtime options used when any command
# is executed.
# Include here and not in CATALINA_OPTS all options, that
# should be used by Tomcat and also by the stop process,
# the version command etc.
# Most options should go into CATALINA_OPTS.

  由tomcat的停止脚本来看,其实是调用另外一个java运行时socket发到8005过去的,所以放置在JAVA_OPTS中导致有开启了一次JMX(它还可以用来动态修改Java对象的值,spring也支持将bean暴露为JMX,它相比表达式技术的优势在于完全没有生成大量动态代理类的风险),所以出现这个错误。

原文地址:https://www.cnblogs.com/zhjh256/p/6022522.html