Arthas 使用教程

一、简介

Arthas 是阿里开源的一款 Java 诊断工具,支持动态监控和诊断 Java 应用。它可以在不重启应用的情况下,实时查看 JVM 状态、方法调用、线程信息等,非常适合生产环境中的问题排查和性能优化。官网地址:https://arthas.aliyun.com/doc/commands.html

二、安装 Arthas

下载arthas-boot.jar,然后用java -jar的方式启动:

curl -O https://arthas.aliyun.com/arthas-boot.jar

三、启动 Arthas

  1. 在终端中运行以下命令启动 Arthas:

    java -jar arthas-boot.jar
    
  2. 启动后,Arthas 会列出当前运行的 Java 进程。

    $ $ java -jar arthas-boot.jar
    * [1]: 35542
      [2]: 71560 math-game.jar
    
  3. 输入对应的编号选择要监控的进程,Arthas 会 attach 到目标进程上,并输出日志:

    [INFO] Try to attach process 71560
    [INFO] Attach process 71560 success.
    [INFO] arthas-client connect 127.0.0.1 3658
    ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
    /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
    |  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
    |  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
    `--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'
    
    
    wiki: https://arthas.aliyun.com/doc
    version: 3.0.5.20181127201536
    pid: 71560
    time: 2018-11-28 19:16:24
    
    $
    

四、常用命令

4.1 查看帮助

输入 help 查看所有命令:

help
4.2 查看 JVM 信息

使用 dashboard 命令查看 JVM 的实时状态:

$ dashboard
ID     NAME                   GROUP          PRIORI STATE  %CPU    TIME   INTERRU DAEMON
17     pool-2-thread-1        system         5      WAITIN 67      0:0    false   false
27     Timer-for-arthas-dashb system         10     RUNNAB 32      0:0    false   true
11     AsyncAppender-Worker-a system         9      WAITIN 0       0:0    false   true
9      Attach Listener        system         9      RUNNAB 0       0:0    false   true
3      Finalizer              system         8      WAITIN 0       0:0    false   true
2      Reference Handler      system         10     WAITIN 0       0:0    false   true
4      Signal Dispatcher      system         9      RUNNAB 0       0:0    false   true
26     as-command-execute-dae system         10     TIMED_ 0       0:0    false   true
13     job-timeout            system         9      TIMED_ 0       0:0    false   true
1      main                   main           5      TIMED_ 0       0:0    false   false
14     nioEventLoopGroup-2-1  system         10     RUNNAB 0       0:0    false   false
18     nioEventLoopGroup-2-2  system         10     RUNNAB 0       0:0    false   false
23     nioEventLoopGroup-2-3  system         10     RUNNAB 0       0:0    false   false
15     nioEventLoopGroup-3-1  system         10     RUNNAB 0       0:0    false   false
Memory             used   total max    usage GC
heap               32M    155M  1820M  1.77% gc.ps_scavenge.count  4
ps_eden_space      14M    65M   672M   2.21% gc.ps_scavenge.time(m 166
ps_survivor_space  4M     5M    5M           s)
ps_old_gen         12M    85M   1365M  0.91% gc.ps_marksweep.count 0
nonheap            20M    23M   -1           gc.ps_marksweep.time( 0
code_cache         3M     5M    240M   1.32% ms)
Runtime
os.name                Mac OS X
os.version             10.13.4
java.version           1.8.0_162
java.home              /Library/Java/JavaVir
                       tualMachines/jdk1.8.0
                       _162.jdk/Contents/Hom
                       e/jre
4.3 监控方法执行时间

使用 watch 命令监控方法的执行时间:

watch com.example.MyClass myMethod '{params, returnObj}' -x 2
4.4 生成线程快照

使用 thread 命令生成线程快照:

thread
4.5 查看方法调用链路

使用 trace 命令查看方法的调用链路和执行时间:

trace com.example.MyClass myMethod
4.6 动态修改日志级别

使用 logger 命令动态修改日志级别:

logger --name ROOT --level debug
4.7 查看类加载信息

使用 sc 命令查看类的加载信息:

sc -d com.example.MyClass
4.8 查看方法信息

使用 sm 命令查看类的方法信息:

sm com.example.MyClass
4.9 反编译类

使用 jad 命令反编译类:

jad com.example.MyClass
4.10 退出 Arthas

使用 quit 或 exit 命令退出 Arthas:

quit

五、实际案例

  1. 定位高 CPU 问题
    • 启动 Arthas 并 Attach 到目标进程。
    • 使用 dashboard 查看 JVM 状态,找到占用 CPU 高的线程。
    • 使用 thread <线程ID> 查看线程的堆栈信息。
    • 使用 watch 或 trace 命令定位具体的方法。
  2. 分析方法性能
    • 使用 trace 命令查看方法的调用链路和执行时间。
    • 根据输出结果,找到性能瓶颈。
  3. 动态修改日志级别
    • 使用 logger 命令动态修改日志级别。
    • 查看日志输出,确认修改生效。
  4. 反编译类
    • 使用 jad 命令反编译类
    • 查看反编译后的代码,分析问题。

六、高级功能

  1. 批量执行命令
    • 将多个命令写入脚本文件,批量执行:
    • batch -f commands.txt
  2. Web Console
    • 启动 Arthas 的 Web Console:java -jar arthas-boot.jar --telnet-port 9999 --http-port 8563
    • 访问 http://localhost:8563,使用 Web 界面操作 Arthas。
  3. 集成到 Spring Boot
    • 在 Spring Boot 项目中集成 Arthas:
    <dependency>
    	<groupId>com.taobao.arthas</groupId>
    	<artifactId>arthas-spring-boot-starter</artifactId>
    	<version>3.6.7</version>
    </dependency>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值