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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/856881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:15:53  来源:igfitidea点击:

How to activate JMX on my JVM for access with jconsole?

javajvmmonitoringjmxjconsole

提问by Mauli

How to activate JMX on a JVM for access with jconsole?

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

采纳答案by Mauli

The relevant documentation can be found here:

相关文档可以在这里找到:

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

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

Start your program with following parameters:

使用以下参数启动您的程序:

-Dcom.sun.management.jmxremote
-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

For instance like this:

例如像这样:

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=falseis not necessarily required but without it, it doesn't work on Ubuntu. The error would be something like this:

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

see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6754672

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

Also be careful with -Dcom.sun.management.jmxremote.authenticate=falsewhich makes access available for anyone, but if you only use it to track the JVM on your local machine it doesn't matter.

还要注意-Dcom.sun.management.jmxremote.authenticate=false哪个使任何人都可以访问,但如果您只使用它来跟踪本地计算机上的 JVM,则无关紧要。

Update:

更新

In some cases I was not able to reach the server. This was then fixed if I set this parameter as well: -Djava.rmi.server.hostname=127.0.0.1

在某些情况下,我无法访问服务器。如果我也设置了这个参数,这个问题就得到了解决:-Djava.rmi.server.hostname=127.0.0.1

回答by Thorbj?rn Ravn Andersen

Note, Java 6 in the latest incarnation allows for jconsole to attach itself to a running process even after it has been started without JMX incantations.

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

If that is available to you, also consider jvisualvm as it provides a wealth of information on running processes, including a profiler.

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

回答by Wasif

Run your java application with the following command line parameters:

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

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

It is important to use the -Dcom.sun.management.jmxremote.ssl=falseparameter if you don't want to setup digital certificates on the jmx host.

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

If you started your application on a machine having IP address 192.168.0.1, open jconsole, put 192.168.0.1:8855in the Remote Processfield, and click Connect.

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

回答by user3013578

I'm using WAS ND 7.0

我正在使用 WAS ND 7.0

My JVM need all the following arguments to be monitored in JConsole

我的 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

回答by alex.pulver

On Linux, I used the following params:

在 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

and also I edited /etc/hostsso that the hostname resolves to the host address (192.168.0.x) rather than the loopback address (127.0.0.1)

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

回答by Joel B

Running in a Docker container introduced a whole slew of additional problems for connecting so hopefully this helps someone. I ended up needed to add the following options which I'll explain below:

在 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

DOCKER_HOST_IP

Unlike using jconsole locally, you have to advertise a different IP than you'll probably see from within the container. You'll need to replace ${DOCKER_HOST_IP}with the externally resolvable IP (DNS Name) of your Docker host.

与在本地使用 jconsole 不同,您必须公布一个与您可能从容器内看到的 IP 不同的 IP。您需要替换${DOCKER_HOST_IP}为 Docker 主机的外部可解析 IP(DNS 名称)。

JMX Remote & RMI Ports

JMX 远程和 RMI 端口

It looks like JMX also requires access to a remote management interface (jstat) that uses a different portto transfer some data when arbitrating the connection. I didn't see anywhere immediately obvious in jconsoleto set this value. In the linked article the process was:

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

  • Try and connect from jconsolewith logging enabled
  • Fail
  • Figure out which port jconsoleattempted to use
  • Use iptables/firewallrules as necessary to allow that port to connect
  • 尝试连接jconsole并启用日志记录
  • 失败
  • 找出jconsole尝试使用的端口
  • 根据需要使用iptables/firewall规则以允许该端口连接

While that works, it's certainly not an automatable solution. I opted for an upgrade from jconsole to VisualVMsince it let's you to explicitly specify the port on which jstatdis running. In VisualVM, add a New Remote Host and update it with values that correlate to the ones specified above:

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

Add Remote Host

添加远程主机

Then right-click the new Remote Host Connection and Add JMX Connection...

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

Add JMX Connection

添加 JMX 连接

Don't forget to check the checkbox for Do not require SSL connection. Hopefully, that should allow you to connect.

不要忘记选中 的复选框Do not require SSL connection。希望这应该允许您连接。

回答by cstroe

I had this exact issue, and created a GitHub project for testing and figuring out the correct settings.

我遇到了这个确切的问题,并创建了一个 GitHub 项目来测试和找出正确的设置

It contains a working Dockerfilewith supporting scripts, and a simple docker-compose.ymlfor quick testing.

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

回答by Phani Kumar

along with below command line parameters ,

连同下面的命令行参数,

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

Sometimes in the linux servers , imx connection doesn't get succeeded. that is because , in cloud linux host, in /etc/hosts so that the hostname resolves to the host address.

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

the best way to fix it is, ping the particular linux server from other machine in network and use that host IP address in the

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

-Djava.rmi.server.hostname=IP address that obtained when you ping that linux server.

But never rely on the ipaddress that you get from linux server using ifconfig.me. the ip that you get there is masked one which is present in the host file.

但是永远不要依赖使用 ifconfig.me 从 linux 服务器获得的 ipaddress。您在那里获得的 IP 是主机文件中存在的掩码。

回答by Abhay S

First you need to check if your java process is already running with JMX parameters. Do this:

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

ps -ef | grep java

Check your java process you need to monitor. If you can see jmx rmi parameter Djmx.rmi.registry.port=xxxxthen use the port mentioned here in your java visualvm to connect it remotely under jmx connection.

检查您需要监控的 Java 进程。如果你可以看到 jmx rmi 参数Djmx.rmi.registry.port=xxxx然后使用你的 java visualvm 中提到的端口在 jmx 连接下远程连接它。

If it's not running through jmx rmi port then you need to run your java process with below mentioned parameters :

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

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

Note: port numbers are based on your choice.

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

Now you can use this port for jmx coneection. Here it is port 1234.

现在您可以使用此端口进行 jmx 连接。这里是端口1234

回答by Hari Krishna

Step 1:Run the application using following parameters.

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

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

Above arguments bind the application to the port 9999.

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

Step 2:Launch jconsole by executing the command jconsole in command prompt or terminal.

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

Select ‘Remote Process:' and enter the url as {IP_Address}:9999 and click on Connect button to connect to the remote application.

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

You can refer this linkfor complete application.

您可以参考此链接以获取完整的应用程序。