简体   繁体   English

使用 VisualVM 分析 tomcat 应用程序

[英]Profiling tomcat application with VisualVM

I am trying to run visualvm under the username tomcat6 because apparently visualvm can only find applications running under its username.我试图在用户名 tomcat6 下运行 visualvm,因为显然 visualvm 只能找到在其用户名下运行的应用程序。 So by default it is only finding applications running under my username.因此,默认情况下,它只会查找在我的用户名下运行的应用程序。 I have been able to connect visualvm with tomcat6 through jmx but that lacks the fine granularity of instrumented profiling.我已经能够通过 jmx 将 visualvm 与 tomcat6 连接起来,但这缺乏检测分析的精细粒度。

I tried the following to run visualvm under the username tomcat6 but got the following error that I don't understand.我尝试以下在用户名 tomcat6 下运行 visualvm,但出现以下我不明白的错误。

$ sudo -u tomcat6 jvisualvm
No protocol specified
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
...

Unfortunately only sampling is available in remote mode so JMX will lack instrumentation tools.不幸的是,远程模式下只有采样可用,因此JMX将缺乏检测工具。

Actually your approach to running visualvm under tomcat6 user is correct.实际上,您在tomcat6用户下运行visualvm的方法是正确的。 You should take a look at this question on how to run X11 applications under sudo.你应该看看这个关于如何在 sudo 下运行X11应用程序的问题。

The easiest way to pass DISPLAY and XAUTHORITY environment variables is to use sudo -E command to preserve current user environment.传递DISPLAYXAUTHORITY环境变量的最简单方法是使用sudo -E命令保留当前用户环境。

Also if you can't see your process under tomcat6 user you should check if CATALINA_TMPDIR is pointing to /tmp .此外,如果您在tomcat6用户下看不到您的进程,则应检查CATALINA_TMPDIR是否指向/tmp Otherwise you should pass it to visualvm否则你应该把它传递给visualvm

jvisualvm -J-Djava.io.tmpdir="${CATALINA_TMPDIR}"

Actually there is a lot of alternatives like yourkit or jprofiler shipped with java agents which allows remote instrumentation profiling.实际上,有很多替代方案,如yourkitjprofiler随 java 代理一起提供,它们允许远程检测分析。

If the computer running your application is remote -- like a server -- then you can't run GUI applications without some work.如果运行你的应用程序的计算机是远程的——比如服务器——那么你不能在没有工作的情况下运行 GUI 应用程序。 It's probably going to be easier to enable remote access to VisualVM .启用对VisualVM的远程访问可能会更容易。

You can use two techniques to attach to a remote JVM: using jstatd or using JMX.您可以使用两种技术连接到远程 JVM:使用jstatd或使用 JMX。 I'm not sure what you think you are losing by using JMX, but evidently jstatd doesn't give you access to profiling tools, CPU monitor, etc.).我不确定您认为使用 JMX 会失去什么,但显然jstatd不会让您访问分析工具、CPU 监视器等)。

You need to configure your JVM and Tomcat to allow for remote access.您需要配置 JVM 和 Tomcat 以允许远程访问。 That requires 3 steps:这需要3个步骤:

  1. Enable remote JMX.启用远程 JMX。 Turns out, there's a guide for that .事实证明, 有一个指南

  2. Fix the "wandering port" used for RMI.修复用于 RMI 的“流浪端口”。 There's a guide for that , too.也有一个指南

  3. (Optional) Arrange for secure remote-access to the server. (可选)安排对服务器的安全远程访问。 The easiest way to do that would be to use ssh -Lport:localhost:port with a series of -L arguments to forward multiple ports from your workstation to your server.最简单的方法是使用ssh -Lport:localhost:port和一系列-L参数将多个端口从工作站转发到服务器。 Map all the ports you had to configure in steps #1 and #2.映射您在步骤#1 和#2 中必须配置的所有端口。 If you don't do this, you'll need to have non-firewalled access to all the aforementioned ports.如果您不这样做,则需要对上述所有端口进行非防火墙访问。

  4. Restart your JVM and connect with JVisualVM.重新启动 JVM 并连接 JVisualVM。

Update 2022-06-01更新 2022-06-01

Note that the "wandering port" problem has been fixed at the JVM level, so there is no need for application (ie Tomcat) support for that.请注意,“流浪端口”问题已在 JVM 级别修复,因此无需应用程序(即 Tomcat)支持。 Item #1 for Tomcat 8.5 and later contains updated instructions making item #2 unnecessary with a recent JVM. Tomcat 8.5 及更高版本的第 1 项包含更新的指令,使最新的 JVM 不需要第 2 项。

The easiest is to open a remote JXM port on Tomcat in order to be able to remotely (from your desktop computer) connect to your remote Tomcat (on your server) with jvisualvm.最简单的方法是在 Tomcat 上打开一个远程 JXM 端口,以便能够使用 jvisualvm 远程(从您的台式计算机)连接到您的远程 Tomcat(在您的服务器上)。

You need to pass the following system properties to your JVM :您需要将以下系统属性传递给您的 JVM:

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=<whatever_port_you_want>
-Dcom.sun.management.jmxremote.ssl=false

Then open jvisualvm on your local computer (JVM version needs to be the same or newer), File -> Add Remote Host -> Enter the name on the Host.然后在本地电脑上打开jvisualvm(JVM版本需要相同或更新),文件->添加远程主机->在主机上输入名称。 It will create an entry for this host.它将为此主机创建一个条目。 Right lick on this Entry -> Add JMX connection -> Enter the port -> OK右键这个条目 -> 添加 JMX 连接 -> 输入端口 -> 确定

jvisualvm will then be able to access remotely to your application. jvisualvm 将能够远程访问您的应用程序。

You can also secure the connection if needed by using the following system properties (you need to create the files and locate them where you want :如果需要,您还可以使用以下系统属性保护连接(您需要创建文件并将它们定位到您想要的位置:

-Dcom.sun.management.jmxremote.password.file=jmxremote.password 
-Dcom.sun.management.jmxremote.access.file=jmxremote.access 

These properties needs to be added to the CATALINA_OPTS environment variable.这些属性需要添加到 CATALINA_OPTS 环境变量中。 Fr exemple :法例:

export CATALINA_OPTS = "$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8888 "

More info on JMX lies here : https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html有关 JMX 的更多信息在这里: https ://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html

I tried to do something similar but I was not allowed to install JVisualVM on the server.我尝试做类似的事情,但不允许在服务器上安装 JVisualVM。 Having JVisualVM connect to the remote machine never seemed to work correctly.让 JVisualVM 连接到远程机器似乎永远无法正常工作。 I suspect firewall rules were blocking part of the the network connections.我怀疑防火墙规则阻止了部分网络连接。 The only way I found to remotely profile the server was via an ssh tunnel.我发现远程分析服务器的唯一方法是通过 ssh 隧道。

  1. Set the JMX port in CATALINA_OPTS on the server在服务器上的 CATALINA_OPTS 中设置 JMX 端口

    CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=13333 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false; export CATALINA_OPTS CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=13333 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false; 导出 CATALINA_OPTS

  2. On your desktop machine open an ssh connection to the server在您的桌面计算机上打开与服务器的 ssh 连接

    ssh -D 61444 username@tomcat.server.address ssh -D 61444 用户名@tomcat.server.address

  3. Add a flag to JVisualVM so that it will proxy its network connection向 JVisualVM 添加一个标志,以便它代理其网络连接

    "C:\Program Files\Java\jdk1.7.0_79\bin\jvisualvm.exe" -J-Dnetbeans.system_socks_proxy=localhost:61444 -J-Djava.net.useSystemProxies=true “C:\Program Files\Java\jdk1.7.0_79\bin\jvisualvm.exe”-J-Dnetbeans.system_socks_proxy=localhost:61444 -J-Djava.net.useSystemProxies=true

  4. Have JVisualVM connect to the jmxport and the network traffic is tunneled via ssh.让 JVisualVM 连接到 jmxport,网络流量通过 ssh 进行隧道传输。

    Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM