Java 远程 JMX 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/834581/
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:01:19  来源:igfitidea点击:

Remote JMX connection

javajmxjconsole

提问by tuler

I'm trying to open a JMX connection to java application running on a remote machine.

我正在尝试打开一个到远程机器上运行的 java 应用程序的 JMX 连接。

The application JVM is configured with the following options:

应用程序 JVM 配置有以下选项:

  • com.sun.management.jmxremote
  • com.sun.management.jmxremote.port=1088
  • com.sun.management.jmxremote.authenticate=false
  • com.sun.management.jmxremote.ssl=false
  • com.sun.management.jmxremote
  • com.sun.management.jmxremote.port=1088
  • com.sun.management.jmxremote.authenticate=false
  • com.sun.management.jmxremote.ssl=false

I'm able to connect using localhost:1088using jconsole or jvisualvm. But I'm not able to connect using xxx.xxx.xxx.xxx:1088from a remote machine.

我可以localhost:1088使用 jconsole 或 jvisualvm进行连接。但我无法xxx.xxx.xxx.xxx:1088从远程机器连接使用。

There is no firewall between the servers, or on the OS. But to eliminate this possibility I telnet xxx.xxx.xxx.xxx 1088and I think it connects, as the console screen turns blank.

服务器之间或操作系统上没有防火墙。但是为了消除这种可能性,我telnet xxx.xxx.xxx.xxx 1088和我认为它会连接,因为控制台屏幕变为空白。

Both servers are Windows Server 2008 x64. Tried with 64-bit JVM and 32-bit, neither work.

两台服务器都是 Windows Server 2008 x64。用 64 位 JVM 和 32 位尝试过,都不起作用。

采纳答案by takete.dk

Had it been on Linux the problem would be that localhost is the loopback interface, you need to application to bind to your network interface.

如果是在 Linux 上,问题将是localhost 是环回接口,您需要应用程序绑定到您的网络接口

You can use the netstat to confirm that it is not bound to the expected network interface.

您可以使用 netstat 来确认它没有绑定到预期的网络接口。

You can make this work by invoking the program with the system parameter java.rmi.server.hostname="YOUR_IP", either as an environment variable or using

您可以通过将系统参数java.rmi.server.hostname="YOUR_IP"作为环境变量或使用

java -Djava.rmi.server.hostname=YOUR_IP YOUR_APP

回答by darko.topolsek

Try using ports higher than 3000.

尝试使用高于 3000 的端口。

回答by yohann.martineau

it seams that your ending quote comes too early. It should be after the last parameter.

看来你的结束语来得太早了。它应该在最后一个参数之后。

This trick worked for me.

这个技巧对我有用。

I noticed something interesting: when I start my application using the following command line:

我注意到一些有趣的事情:当我使用以下命令行启动我的应用程序时:

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

If I try to connect to this port from a remote machine using jconsole, the TCP connection succeeds, some data is exchanged between remote jconsole and local jmx agent where my MBean is deployed, and then, jconsole displays a connect error message. I performed a wireshark capture, and it shows data exchange coming from both agent and jconsole.

如果我尝试使用 jconsole 从远程机器连接到此端口,则 TCP 连接成功,在远程 jconsole 和部署我的 MBean 的本地 jmx 代理之间交换一些数据,然后 jconsole 显示连接错误消息。我执行了一个wireshark捕获,它显示了来自代理和jconsole的数据交换。

Thus, this is not a network issue, if I perform a netstat -an with or without java.rmi.server.hostname system property, I have the following bindings:

因此,这不是网络问题,如果我在有或没有 java.rmi.server.hostname 系统属性的情况下执行 netstat -an,我有以下绑定:

 TCP    0.0.0.0:9999           0.0.0.0:0              LISTENING
 TCP    [::]:9999              [::]:0                 LISTENING

It means that in both cases the socket created on port 9999 accepts connections from any host on any address.

这意味着在这两种情况下,在端口 9999 上创建的套接字都接受来自任何地址上的任何主机的连接。

I think the content of this system property is used somewhere at connection and compared with the actual IP address used by agent to communicate with jconsole. And if those address do not match, connection fails.

我认为这个系统属性的内容在连接的某个地方使用,并与代理用来与 jconsole 通信的实际 IP 地址进行比较。如果这些地址不匹配,则连接失败。

I did not have this problem while connecting from the same host using jconsole, only from real physical remote hosts. So, I suppose that this check is done only when connection is coming from the "outside".

使用 jconsole 从同一台主机连接时,我没有遇到这个问题,只有从真正的物理远程主机连接。所以,我认为只有当连接来自“外部”时才会进行此检查。

回答by h0nIg

http://blogs.oracle.com/jmxetc/entry/troubleshooting_connection_problems_in_jconsole

http://blogs.oracle.com/jmxetc/entry/troubleshooting_connection_problems_in_jconsole

If you are trying to access a server which is behind a NAT - you will most probably have to start your server with the option

如果您尝试访问位于 NAT 后面的服务器 - 您很可能必须使用该选项启动您的服务器

-Djava.rmi.server.hostname=<public/NAT address>

so that the RMI stubs sent to the client contain the server's public address allowing it to be reached by the clients from the outside.

以便发送到客户端的 RMI 存根包含服务器的公共地址,允许客户端从外部访问它。

回答by ianpojman

I have the same issue and I change any hostname that matches the local host name to 0.0.0.0, it seems to work after I do that.

我有同样的问题,我将与本地主机名匹配的任何主机名更改为 0.0.0.0,这样做后似乎可以工作。

回答by sorin

I've spend more than a day trying to make JMX to work from outside localhost. It seems that SUN/Oracle failed to provide a good documentation on this.

我花了一天多的时间试图让 JMX 在本地主机之外工作。似乎 SUN/Oracle 未能提供关于此的良好文档。

Be sure that the following command returns you a real IP or HOSTNAME. If it does return something like 127.0.0.1, 127.0.1.1 or localhost it will not work and you will have to update /etc/hostsfile.

确保以下命令为您返回真实 IP 或 HOSTNAME。如果它确实返回 127.0.0.1、127.0.1.1 或 localhost 之类的内容,它将无法工作,您将不得不更新/etc/hosts文件。

hostname -i

Here is the command needed to enable JMX even from outside

这是从外部启用 JMX 所需的命令

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.port=1100
-Djava.rmi.server.hostname=myserver.example.com

Where as you assumed, myserver.example.com must match what hostname -ireturns.

正如您所假设的, myserver.example.com 必须匹配hostname -i返回的内容。

Obviously, you need to be sure that the firewall does not block you, but I'm almost sure that this is not your problem, the problem being the last parameter that is not documented.

显然,您需要确保防火墙不会阻止您,但我几乎可以肯定这不是您的问题,问题是最后一个没有记录的参数。

回答by user3406690

Thanks a lot, it works like this:

非常感谢,它是这样工作的:

java -Djava.rmi.server.hostname=xxx.xxx.xxx.xxx-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=25000 -jar myjar.jar

java -Djava.rmi.server.hostname= xxx.xxx.xxx.xxx-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false - dcom.sun.management.jmxremote.port=25000 -jar myjar.jar

回答by Maoz Zadok

the thing that work for me was to set /etc/hosts to point the hostname to the ip and not to the loopback interface and than restart my application.

对我有用的是设置 /etc/hosts 将主机名指向 ip 而不是环回接口,然后重新启动我的应用程序。

cat /etc/hosts

猫 /etc/hosts

127.0.0.1      localhost.localdomain localhost
192.168.0.1    myservername

This is my configuration:

这是我的配置:

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

回答by LeslieM

In my testing with Tomcat and Java 8, the JVM was opening an ephemeral port in addition to the one specified for JMX. The following code fixed me up; give it a try if you are having issues where your JMX client (e.g. VisualVMis not connecting.

在我对 Tomcat 和 Java 8 的测试中,除了为 JMX 指定的端口之外,JVM 还打开了一个临时端口。以下代码修复了我;如果您在 JMX 客户端(例如VisualVM未连接)方面遇到问题,请尝试一下。

-Dcom.sun.management.jmxremote.port=8989
-Dcom.sun.management.jmxremote.rmi.port=8989

Also see Why Java opens 3 ports when JMX is configured?

另请参阅为什么在配置 JMX 时 Java 打开 3 个端口?

回答by Ajay Kumar

To enable JMX remote, pass below VM parameters along with JAVA Command.

要启用 JMX 远程,请将下面的 VM 参数与 JAVA 命令一起传递。

    -Dcom.sun.management.jmxremote 
    -Dcom.sun.management.jmxremote.port=453
    -Dcom.sun.management.jmxremote.authenticate=false                               
    -Dcom.sun.management.jmxremote.ssl=false 
    -Djava.rmi.server.hostname=myDomain.in