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

时间:2009-05-13 09:02:13

标签: java jvm monitoring jmx jconsole

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

12 个答案:

答案 0 :(得分:259)

相关文件可在此处找到:

http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html

使用以下参数启动程序:

-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

例如:

java -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 \
  -jar Notepad.jar

-Dcom.sun.management.jmxremote.local.only=false不一定是必需的 但没有它,它在Ubuntu上不起作用。错误就像这样 这样:

01 Oct 2008 2:16:22 PM sun.rmi.transport. customer .TCPTransport$AcceptLoop executeAcceptLoop
WARNING: RMI TCP Accept-0: accept loop for ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=37278] throws
java.io.IOException: The server sockets created using the LocalRMIServerSocketFactory only accept connections from clients running on the host where the RMI remote objects have been exported.
    at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:89)
    at sun.rmi.transport. customer .TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:387)
    at sun.rmi.transport. customer .TCPTransport$AcceptLoop.run(TCPTransport.java:359)
    at java.lang.Thread.run(Thread.java:636)

请参阅http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6754672

还要注意-Dcom.sun.management.jmxremote.authenticate=false 使访问可供任何人使用,但如果您只使用它来跟踪JVM 你的本地机器没关系。

<强>更新

在某些情况下,我无法访问服务器。如果我也设置了此参数,则会修复此问题:-Djava.rmi.server.hostname=127.0.0.1

答案 1 :(得分:52)

在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。您需要使用Docker主机的外部可解析IP(DNS名称)替换${DOCKER_HOST_IP}

JMX Remote&amp; RMI端口

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

  • 尝试从启用了日志记录的jconsole连接
  • 故障
  • 找出尝试使用哪个端口jconsole
  • 根据需要使用iptables / firewall规则以允许该端口连接

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

Add Remote Host

然后右键单击新的远程主机连接和Add JMX Connection...

Add JMX Connection

请勿忘记选中Do not require SSL connection的复选框。希望这应该允许你连接。

答案 2 :(得分:9)

注意,最新版本中的Java 6允许jconsole将其自身附加到正在运行的进程,即使它在没有JMX咒语的情况下启动也是如此。

如果你可以使用它,也可以考虑使用jvisualvm,因为它提供了大量有关正在运行的进程的信息,包括分析器。

答案 3 :(得分:7)

我正在使用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

答案 4 :(得分:6)

在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)

答案 5 :(得分:2)

使用以下命令行参数运行您的Java应用程序:

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

如果您不想在jmx主机上设置数字证书,请务必使用 -Dcom.sun.management.jmxremote.ssl = false 参数。

如果您在具有IP地址 192.168.0.1 的计算机上启动了应用程序,请打开 jconsole ,将 192.168.0.1:8855 放入远程处理字段,然后单击连接

答案 6 :(得分:2)

步骤1:使用以下参数运行应用程序。

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

以上参数将应用程序绑定到端口9999。

步骤2:通过在命令提示符或终端中执行命令jconsole启动jconsole。

选择“远程进程:”并输入{IP_Address}:9999作为网址,然后单击“连接”按钮以连接到远程应用程序。

您可以参考此link进行完整的申请。

答案 7 :(得分:1)

以及以下命令行参数

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

有时在linux服务器中,imx连接不会成功。这是因为,在cloud linux主机中,在/ etc / hosts中,以便主机名解析为主机地址。

修复此问题的最佳方法是,从网络中的其他计算机ping特定的linux服务器,并在

中使用该主机IP地址。
-Djava.rmi.server.hostname=IP address that obtained when you ping that linux server.

但是永远不要依赖您使用ifconfig.me从linux服务器获得的ipaddress。您到达那里的IP被屏蔽了一个存在于主机文件中的IP。

答案 8 :(得分:1)

以下选项对我有用:

-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.rmi.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname={host name}

并记得在服务器中打开9010端口

sudo ufw allow 9010/udp
sudo ufw allow 9010/tcp
sudo ufw reload

答案 9 :(得分:0)

我有这个问题,created a GitHub project for testing and figuring out the correct settings

它包含一个支持脚本的Dockerfile,以及一个用于快速测试的简单docker-compose.yml

答案 10 :(得分:0)

首先,您需要检查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

答案 11 :(得分:0)

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

要在本地运行,这对我有用 -

我在我的 vm 参数中添加了这个 -

-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

I opened JConsole via Intellij Terminal
It was showing me all PID's in grey in local
So I selected remote process and logged in using host - localhost:6001
Keep empty username & password
Then click connect
  • 确保端口 6001 上没有其他进程正在运行。您也可以使用其他端口。