Remote Monitoring with VisualVM and JMX – 用VisualVM和JMX进行远程监控

最后修改: 2021年 12月 11日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this article, we’ll learn how to use VisualVM and Java Management Extensions (JMX) for remote monitoring of Java applications.

在这篇文章中,我们将学习如何使用VisualVM和Java管理扩展(JMX)对Java应用程序进行远程监控。

2. JMX

2.JMX[/strong

JMX is a standard API for the management and monitoring of JVM applications. The JVM has built-in instrumentation that JMX can use for this purpose. As a result, we usually call these utilities “out-of-the-box management tools” or, in this case, “JMX agents”.

JMX是一个用于管理和监控JVM应用程序的标准API。JVM有内置的仪器,JMX可用于此目的。因此,我们通常把这些实用程序称为 “开箱即用的管理工具”,或者在这种情况下称为 “JMX代理”。

3. VisualVM

3.VisualVM[/strong

VisualVM is a visual tool that provides lightweight profiling capabilities for the JVM. There are plenty of other mainstream profiling tools. However, VisualVM is free and comes bundled with the JDK 6U7 release until early updates of JDK 8. For other versions, Java VisualVM is available as a standalone application.

VisualVM是一个可视化工具,为JVM提供轻量级的剖析功能。有很多其他主流的剖析工具。但是,VisualVM 是免费的,并且捆绑在 JDK 6U7 版本中,直到 JDK 8 的早期更新。对于其他版本,Java VisualVM可以作为一个独立的应用程序提供。

VisualVM allows us to connect to both local and remote JVM applications for monitoring purposes.

VisualVM 允许我们连接到本地和远程JVM应用程序,以达到监控目的。

When launched on any machine, it auto-discovers and starts monitoring all JVM applications running locally. However, we need to connect remote applications explicitly.

当在任何机器上启动时,它自动发现并开始监控本地运行的所有JVM应用程序。然而,我们需要明确连接远程应用程序。

3.1. JVM Connection Modes

3.1.JVM连接模式

The JVM exposes itself for monitoring through tools such as jstatd or JMX. These tools, in turn, provide APIs for tools such as VisualVM to get profiling data.

JVM通过诸如jstatdJMX等工具暴露自己以进行监控。这些工具又为VisualVM等工具提供了API,以获得剖析数据。

The jstatd program is a daemon that’s bundled with the JDK. However, it has limited capability. For instance, we can’t monitor CPU usage, nor can we take thread dumps.

jstatd程序是一个与JDK捆绑的守护程序。然而,它的能力有限。例如,我们不能监控CPU的使用,也不能进行线程转储。

On the other hand, JMX technology doesn’t require any daemon to run on the JVM. Moreover, it can be used to profile both local and remote JVM applications. However, we do need to start the JVM with special properties to enable the out-of-the-box monitoring features. In this article, we’ll only focus on the JMX mode.

另一方面,JMX技术不需要在JVM上运行任何守护程序。此外,它可以用来对本地和远程JVM应用程序进行剖析。然而,我们确实需要用特殊的属性来启动JVM,以启用开箱即用的监控功能。在这篇文章中,我们将只关注JMX模式。

3.2. Launching

3.2.启动

As we saw earlier, our JDK version can either come bundled with VisualVM or not. In either case, we can launch it by executing the appropriate binary:

正如我们前面所看到的,我们的JDK版本可以与VisualVM捆绑,也可以不捆绑。无论哪种情况,我们都可以通过执行适当的二进制文件来启动它。

./jvisualvm

If the binary is present in the $JAVA_HOME/bin folder, then the above command will open the VisualVM interface, and it could be in a different folder if installed separately.

如果二进制文件存在于$JAVA_HOME/bin文件夹中,那么上述命令将打开VisualVM界面,如果单独安装,它可能在另一个文件夹中。

VisualVM will launch and load all the Java applications running locally by default:

VisualVM默认会启动并加载本地运行的所有Java应用程序。

visualvm launch

3.3. Features

3.3.特点

VisualVM provides several useful features:

VisualVM提供了几个有用的功能。

  • Display of local and remote Java application processes
  • Monitoring process performance in terms of CPU usage, GC activity, number of loaded classes, and other metrics
  • Visualizing threads in all processes and the times they spend in different states such as sleeping and waiting
  • Taking and displaying thread dumps for immediate insights into what is going on in the processes being monitored

The VisualVM features page has a more comprehensive list of available features. Like all well-designed software, VisualVM can be extended to access more advanced and unique features by installing third-party plugins available on the Plugins tab.

VisualVM的功能页面有一个更全面的可用功能列表。像所有设计良好的软件一样,VisualVM可以通过安装Plugins标签上的第三方插件来扩展,以获得更多高级和独特的功能。

4. Remote Monitoring

4.远程监控

In this section, we’ll demonstrate how to monitor a Java application using VisualVM and JMX remotely. We’ll also get a chance to explore all the necessary configurations and JVM startup options.

在本节中,我们将演示如何使用VisualVM和JMX远程监控一个Java应用程序。我们还将有机会探索所有必要的配置和JVM启动选项。

4.1. Application Configuration

4.1.应用配置

We launch most, if not all, Java applications with a startup script. In this script, the start command usually passes essential parameters to the JVM to specify the application’s needs, such as maximum and minimum memory requirements.

我们用一个启动脚本来启动大多数(如果不是全部)的Java应用程序。在这个脚本中,启动命令通常向JVM传递基本参数,以指定应用程序的需求,如最大和最小内存需求。

Assuming we have an application packaged as MyApp.jar, let’s see an example startup command that includes the main JMX configuration parameters:

假设我们有一个打包成MyApp.jar的应用程序,让我们看看一个包括主要JMX配置参数的启动命令示例。

java -Dcom.sun.management.jmxremote.port=8080 
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Xms1024m -Xmx1024m -jar MyApp.jar

In the command above, MyApp.jar is launched with out-of-the-box monitoring capability configured via port 8080. Furthermore, we deactivated SSL encryption and password authentication for simplicity.

在上面的命令中,MyApp.jar被启动,通过8080端口配置了开箱即用的监控功能。此外,为了简单起见,我们停用了SSL加密和密码验证。

4.2. VisualVM Configuration

4.2.VisualVM配置

Now that we have VisualVM running locally and our MyApp.jar running on a remote server, we can begin our remote monitoring session.

现在,我们已经在本地运行VisualVM,并在远程服务器上运行我们的MyApp.jar,我们可以开始我们的远程监控会话。

Right-click on the left panel and select Add JMX Connection:

在左侧面板上点击右键,选择添加JMX连接

visualvm jmx connection

Input the host:port combination in the Connection field in the resulting dialog box and click OK.

在产生的对话框中的主机:端口组合中输入连接字段,然后点击确定。

If successful, we should now be able to see a monitoring window by double-clicking the new connection from the left panel:

如果成功的话,我们现在应该可以通过双击左侧面板上的新连接看到一个监控窗口。

visualvm remote monitor

5. Conclusion

5.结论

In this article, we’ve explored remote monitoring of Java applications with VisualVM and JMX.

在这篇文章中,我们已经探讨了用VisualVM和JMX对Java应用程序进行远程监控。