如何在JVM上激活JMX以使用jconsole进行访问?


当前回答

首先,您需要检查java进程是否已经使用JMX参数运行。这样做:

ps -ef | grep java

检查您需要监视的java进程。如果你能看到jmx rmi参数Djmx.rmi.registry。Port =xxxx,然后在Java visualvm中使用这里提到的端口在JMX连接下远程连接它。

如果它没有通过jmx rmi端口运行,那么你需要使用以下提到的参数运行你的java进程:

-Djmx.rmi.registry.port=1234 -Djmx.rmi.port=1235 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

注意:端口号基于您的选择。

现在您可以将此端口用于jmx连接。这里是1234端口。

其他回答

在Docker容器中运行会带来一系列额外的连接问题,希望这能帮助到一些人。我最终需要添加以下选项,我将在下面解释:

-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=${DOCKER_HOST_IP}
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.rmi.port=9998

DOCKER_HOST_IP

与在本地使用jconsole不同,您必须通告一个不同的IP,而不是在容器中可能看到的IP。您需要将${DOCKER_HOST_IP}替换为Docker主机的外部可解析IP (DNS Name)。

JMX远程和RMI端口

看起来JMX还需要访问远程管理接口(jstat),该接口在仲裁连接时使用不同的端口传输一些数据。我在jconsole中没有看到任何地方明显需要设置这个值。在链接的文章中,过程是:

尝试在启用日志记录的情况下从jconsole连接 失败 找出jconsole试图使用的端口 根据需要使用iptables/防火墙规则来允许该端口连接

虽然这是可行的,但它肯定不是一个可自动化的解决方案。我选择从jconsole升级到VisualVM,因为它允许您显式地指定运行jstatd的端口。在VisualVM中,添加一个新的远程主机,并使用与上面指定的值相关的值更新它:

然后右键单击新的远程主机连接并添加JMX连接…

不要忘记勾选“不需要SSL连接”复选框。希望这能让你们建立联系。

我使用的是WAS ND 7.0

我的JVM需要在JConsole中监视以下所有参数

    -Djavax.management.builder.initial= 
    -Dcom.sun.management.jmxremote 
    -Dcom.sun.management.jmxremote.port=8855 
    -Dcom.sun.management.jmxremote.authenticate=false 
    -Dcom.sun.management.jmxremote.ssl=false

使用以下命令行参数运行java应用程序:

-Dcom.sun.management.jmxremote.port=8855
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

使用-Dcom.sun.management.jmxremote非常重要。如果不想在JMX主机上设置数字证书,则使用Ssl =false参数。

如果在IP地址为192.168.0.1的机器上启动应用程序,则打开jconsole,在Remote Process字段中输入192.168.0.1:8855,然后单击Connect。

使用远程进程选项运行本地进程JCONSOLE

为了在本地运行,这对我来说是可行的

我在vm args -中添加了这个

-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=6001
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=localhost
-Dcom.sun.management.jmxremote.rmi.port=6001

我通过Intellij终端打开JConsole 它给我显示了本地所有的PID值 所以我选择了远程进程,并使用主机- localhost:6001登录 用户名和密码为空 然后点击连接


确保端口6001上没有其他进程正在运行。您也可以使用其他端口。

在Linux上,我使用以下参数:

-Djavax.management.builder.initial= 
-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9010 
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false

我还编辑了/etc/hosts,以便主机名解析为主机地址(192.168.0.x),而不是环回地址(127.0.0.1)