简体   繁体   English

如何在 Cloud Foundry 上获取 Java 应用程序的线程转储?

[英]How to obtain threaddump of a java application on Cloud foundry?

I tried a couple of options我尝试了几个选项

a.一种。 cf java thread-dump myapp -i0 (Only 1 instance is there so 0) cf java thread-dump myapp -i0 (只有 1 个实例,所以 0)
Response is nothing.回应不算什么。 logs of myapp show 2019-12-13T14:52:41.15+0800 [SSH/0] OUT Successful remote access by 10.xxx:35764 2019-12-13T14:52:41.84+0800 [SSH/0] OUT Remote access ended for 10.xxx:35764 myapp 的日志显示 2019-12-13T14:52:41.15+0800 [SSH/0] OUT 成功远程访问 10.xxx:35764 2019-12-13T14:52:41.84+0800 [SSH/0] OUT 远程访问结束对于 10.xxx:35764

b.I did cf ssh myapp我做了cf ssh myapp

/home/vcap/app/.java-buildpack/open_jdk_jre/bin/ has no jstack and jmap command doesn't work either /home/vcap/app/.java-buildpack/open_jdk_jre/bin/ has the following components /home/vcap/app/.java-buildpack/open_jdk_jre/bin/ 没有 jstack 并且 jmap 命令也不起作用 /home/vcap/app/.java-buildpack/open_jdk_jre/bin/ 具有以下组件

java爪哇
orbd servertool orbd 服务器工具
java-buildpack-memory-calculator-3.13.0_RELEASE java-buildpack-memory-calculator-3.13.0_RELEASE
pack200包200
tnameserv域名服务
jjs知乎
policytool政策工具
unpack200 jvmkill-1.16.0_RELEASE unpack200 jvmkill-1.16.0_RELEASE
rmid米德
keytool钥匙工具
rmiregistry注册

Question is-> How to get the java threaddump?问题是-> 如何获取java threaddump?

I believe the easiest option is to run cf logs to watch your app logs.我相信最简单的选择是运行cf logs来查看您的应用程序日志。 Then in a second terminal, cf ssh to the app and run ps aux and look for the process id of the Java process.然后在第二个终端中, cf ssh到应用程序并运行ps aux并查找 Java 进程的进程 ID。 Then run kill -3 <pid> .然后运行kill -3 <pid> Your Java process will catch this signal and issue a thread dump.您的 Java 进程将捕获此信号并发出线程转储。 It will go to STDOUT, which conveniently goes to your app's log stream so it should show up in the first terminal.它将转到 STDOUT,它方便地转到您的应用程序的日志流,因此它应该显示在第一个终端中。

The crummy part about this is that cf logs will inject some additional text at the beginning of every line, which makes it hard to load the thread dump into tools.关于这个的糟糕部分是cf logs会在每一行的开头注入一些额外的文本,这使得很难将线程转储加载到工具中。 You can usually strip that off with cut or awk , or you can redirect cf logs to a file, open with a text editor and find/replace the leading text.您通常可以使用cutawk去除它,或者您可以将cf logs重定向到文件,使用文本编辑器打开并查找/替换前导文本。

Besides that you have a couple other options:除此之外,您还有其他几个选择:

  • If you're using Spring, enable Spring Boot Actuators.如果您使用的是 Spring,请启用 Spring Boot Actuators。 You can then use the /actuator/threaddump endpoint to generate and download a thread dump (note the format for this is JSON, not the standard format)然后,您可以使用/actuator/threaddump端点生成和下载线程转储(请注意,此格式是 JSON,而不是标准格式)

  • Use an APM tool or profiler like NewRelic, AppDynamics, Dynatrace or YourKit.使用 APM 工具或分析器,如 NewRelic、AppDynamics、Dynatrace 或 YourKit。 These are well supported by the Java buildpack and most will just work. Java buildpack 很好地支持这些,并且大多数都可以正常工作。

  • Use JMX.使用 JMX。 It's easy enough to enable with the Java buildpack, just set the env variable JBP_CONFIG_JMX to '{enabled: true}' and restage your app.使用 Java buildpack 很容易启用,只需将环境变量JBP_CONFIG_JMX设置为'{enabled: true}'并重新启动您的应用程序。 Once enabled, you can open a tunnel with cf ssh -N -T -L 5000:localhost:5000 <APP_NAME> .启用后,您可以使用cf ssh -N -T -L 5000:localhost:5000 <APP_NAME>打开隧道。 Then open jvisualvm , make a new "local" JMX connection to "localhost:5000" and connect.然后打开jvisualvm ,建立一个新的“本地”JMX 连接到“localhost:5000”并连接。 Click the "Threads" tab and click the "Thread Dump" button.单击“线程”选项卡,然后单击“线程转储”按钮。 It might be a little slow as it's going across the tunnel, but give it a few seconds to finish and you should have a thread dump.它在穿过隧道时可能会有点慢,但给它几秒钟的时间来完成,你应该有一个线程转储。

What won't work on Cloud Foundry are jstack and jcmd .在 Cloud Foundry 上不起作用的是jstackjcmd The Java buildpack, at the time of writing, installs a JRE and these tools are no longer shipped with the OpenJDK JRE and for legal reasons can't be bundled with it.在撰写本文时,Java buildpack 安装了 JRE,并且这些工具不再随 OpenJDK JRE 一起提供,并且出于法律原因不能与其捆绑。 If you were inclined to do so, you could scp the tools and required shared libraries from a JDK up to your app container and run them, but it's a lot of work and not fun.如果您愿意这样做,您可以将工具和所需的共享库从 JDK scp到您的应用程序容器并运行它们,但这需要大量工作,而且并不有趣。 I'm not super familiar with the cf java plugin, but I suspect it tries to use these tools, which used to be available, and no longer are there.我对cf java插件不是很熟悉,但我怀疑它试图使用这些工具,这些工具曾经可用,但不再存在。

Hope that helps!希望有帮助!

您应该能够使用kill -3 <PID>获得 ThreadDumps 并在堆分析器中打开它,例如eclipse 内存分析器 (MAT)并选择Thread Stacks

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

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