如何连接到 WSL2 中运行的 JMX 服务器

问题描述

我使用以下启用 JMX 服务器远程连接的参数运行我的 Java 应用程序:

-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=9998
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

我可以通过 VisualVM 成功连接到该 JMX 服务器,方法添加一个新的 JMX 连接到地址:<HOSTNAME>:9998

但是当我在 Windows Subsystem for Linux 2 (WSL2) 中运行该应用程序时,Visual VM 无法连接到 localhost:9998connection failed

据我所知,应用程序在 WSL2 中侦听的每个端口也在主机(Windows 本身)中打开,我可以使用 Win+R -> cmd -> telnet localhost 9998(连接成功)进行检查。 此外,我在 WSL2 中运行了一个 Nginx 实例,我可以使用 localhost 通过我的主机 (Windows) 上的任何浏览器连接到该实例。

解决方法

我无法使用上述任何建议让 Windows jconsole 连接到在 WSL2 中运行的 JVM,但是现在可以使用 Windows 11 的 Linux GUI 支持在 WSL2 中运行 Linux jconsole。完美运行。

,

这似乎是 Java 的一个普遍问题。

https://github.com/microsoft/WSL/discussions/6253

对我有用的解决方法是使用 JMX 服务器在 WSL2 中侦听“0.0.0.0”而不是“127.0.0.1”。