天天看点

深度解析阿里Arthas:从入门到精通,掌握Java应用问题排查利器

作者:摆脱格子衫

Arthas是阿里开源的一款Java诊断工具,支持在线排查Java应用问题。它能够帮助Java开发者快速定位并解决生产环境中的各种问题,例如性能问题、死锁问题、内存泄漏问题等。

深度解析阿里Arthas:从入门到精通,掌握Java应用问题排查利器

以下是Arthas教程的详细介绍,包括每个使用知识点的实例命令和输出:

  1. 安装Arthas

首先需要下载Arthas并安装,具体操作可以参考Arthas的官方文档。

  1. 连接目标Java进程

在Arthas安装完成后,使用以下命令连接目标Java进程:

$ java -jar arthas-boot.jar
$ attach PID
           

其中,PID是目标Java进程的进程ID。

输出:

成功连接到目标Java进程。

  1. 查看Java类信息

使用以下命令可以查看Java类的信息:

  • 查看类名:
$ class com.example.demo.DemoController
           

输出:

class com.example.demo.DemoController
           
  • 查看类的所有方法:
$ method com.example.demo.DemoController
           

输出:

com.example.demo.DemoController
public java.lang.String com.example.demo.DemoController.hello(java.lang.String)
public void com.example.demo.DemoController.handleException()
           
  • 查看类的所有属性:
$ field com.example.demo.DemoController
           

输出:

com.example.demo.DemoController
private static final org.slf4j.Logger com.example.demo.DemoController.logger
           
  1. 监控Java方法

使用以下命令可以监控Java方法的执行情况:

  • 监控指定方法的执行情况:
$ monitor -c 5 com.example.demo.DemoController hello
           

其中,-c 5表示监控5次方法执行情况,com.example.demo.DemoController表示类名,hello表示方法名。

输出:

Monitoring com.example.demo.DemoController#hello for 5 times, interval: 1s, timeout: 300s.
Press Ctrl+C to abort.
  Execution count: 1, Total cost: 43.291 ms, Avg cost: 43.291 ms, Min cost: 43.291 ms, Max cost: 43.291 ms, Exception count: 0, TPS: 23.13/s
  Execution count: 2, Total cost: 41.117 ms, Avg cost: 41.117 ms, Min cost: 41.117 ms, Max cost: 41.117 ms, Exception count: 0, TPS: 24.36/s
  Execution count: 3, Total cost: 39.304 ms, Avg cost: 39.304 ms, Min cost: 39.304 ms, Max cost: 39.304 ms, Exception count: 0, TPS: 25.42/s
  Execution count: 4, Total cost: 41.825 ms, Avg cost: 41.825 ms, Min cost: 39.304 ms, Max cost: 43.291 ms, Exception count: 0, TPS: 23.91/s
  Execution count: 5, Total cost: 42.246 ms, Avg cost: 49.2 ms, Min cost: 39.304 ms, Max cost: 83.554 ms, Exception count: 0, TPS: 24.44/s           
  • 监控所有方法的执行情况:
$ monitor -c 5 com.example.demo.DemoController
           

其中,-c 5表示监控5次方法执行情况,com.example.demo.DemoController表示类名。

输出:

Monitoring com.example.demo.DemoController for 5 times, interval: 1s, timeout: 300s.
Press Ctrl+C to abort.
  Execution count: 2, TPS: 1.0/s
  Execution count: 2, TPS: 1.0/s
  Execution count: 2, TPS: 1.0/s
  Execution count: 2, TPS: 1.0/s
  Execution count: 2, TPS: 1.0/s
           
  1. 查看Java线程信息

使用以下命令可以查看Java线程的信息:

  • 查看当前所有线程:
$ thread
           

输出:

Id   Group  State         Name
 1    main   RUNNABLE      ...
 2    main   WAITING       ...
 3    main   TIMED_WAITING ...
 4    main   BLOCKED       ...
           
  • 查看指定线程的信息:
$ thread 1
           

其中,1表示线程ID。

输出:

Thread[main,5,main]
  java.lang.Object.wait(Native Method)
  java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
  java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
  java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
           
  1. 查看Java内存信息

使用以下命令可以查看Java内存的信息:

  • 查看当前Java进程的内存情况:
$ jvm
           

输出:

Class Loaders: 7
Loaded Classes: 13198
Total Classes: 13241
Heap Memory Usage: init:2621440, used:32950664, committed:54525952, max:3715249152
Non-Heap Memory Usage: init:2555904, used:49171920, committed:51773440, max:-1
           
  • 查看指定Java类的实例情况:
$ jvm com.example.demo.User
           

其中,com.example.demo.User表示类名。

输出:

Class: com.example.demo.User
Loaded Count: 1
Instance Count: 1
Total Count: 1
           
  1. 调试Java应用

使用以下命令可以在Java应用中进行调试:

  • 打开Java应用的调试端口:
$ jad --source-only com.example.demo.DemoController hello
           

其中,com.example.demo.DemoController表示类名,hello表示方法名。

输出:

$ jad --source-only com.example.demo.DemoController hello
Decompiled code of com.example.demo.DemoController#hello is as follows:
           
  • 使用调试器连接Java应用的调试端口:
$ jdb -attach 127.0.0.1:8000           
  1. 其他命令

除了以上介绍的命令之外,Arthas还提供了一些其他有用的命令,例如:

  • 查看Java类的字节码:
$ sc com.example.demo.DemoController
           

其中,com.example.demo.DemoController表示类名。

输出:

Class: com.example.demo.DemoController
ClassLoader: sun.misc.Launcher$AppClassLoader@18b4aac2
Location: file:/Users/arthas/arthas-demo/target/classes/
Code Source: file:/Users/arthas/arthas-demo/target/classes/
Super Class: org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver
           
  • 查看Java类的加载情况:
$ classloader
           

输出:

ClassLoader tree:
sun.misc.Launcher$AppClassLoader@18b4aac2
  +- sun.misc.Launcher$ExtClassLoader@6cd87312
           
  • 查看当前系统的环境变量:
$ env
           

输出:

PATH: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
           
  • 查看当前的工作目录:
$ pwd
           

输出:

/Users/arthas
           
  • 查看当前的Java版本:
$ version
           

输出:

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)
           
  • 退出Arthas:
$ quit
           

输出:

Bye! Enjoy the day, enjoy the life~
           

以上是Arthas的一些常用命令,可以帮助开发者快速定位和解决Java应用中的问题。当然,Arthas还有许多其他的功能和命令,这里只是介绍了一部分,感兴趣的读者可以查看官方文档或者自行探索。

继续阅读