Arthas 是阿里巴巴开源的一款 Java 诊断工具,它可以帮助开发人员在不需要修改代码或重启应用的情况下,实时监控和诊断 Java 应用程序的运行状态

https://arthas.aliyun.com/doc/

一、介绍:

主要功能:

         - 类加载诊断 - 查看类的加载信息、类结构等
         - 方法调用追踪 - 跟踪方法的调用路径、参数、返回值和异常
         - 性能分析 - 监控方法执行时间,定位性能瓶颈
         - 热更新代码 - 在不重启应用的情况下替换类文件
         - 查看运行时数据 - 检查对象属性、静态字段、系统属性等
         - 线程诊断 - 查看线程状态、死锁检测等
         - JVM 信息 - 查看 JVM 运行时信息、GC 情况等

典型使用场景:

        - 线上问题排查(无需重启应用)
        - 性能调优和问题定位
        - 方法调用链路追踪
        - 热修复紧急 bug
        - 监控生产环境运行状态

常用命令:

        - `dashboard` - 查看系统实时数据面板
        - `trace` - 追踪方法调用
        - `watch` - 观察方法执行数据
        - `tt` - 记录方法调用时空隧道
        - `jad` - 反编译类文件
        - `sc` - 搜索已加载的类
        - `sm` - 搜索已加载的方法

二、安装方式

1. 直接下载(推荐)

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

# 或使用 wget
wget https://arthas.aliyun.com/arthas-boot.jar

2. 使用 Maven 仓库

# 从 Maven 中央仓库下载
curl -O https://repo1.maven.org/maven2/com/taobao/arthas/arthas-boot/3.7.2/arthas-boot-3.7.2.jar

3. Windows 环境

直接在 PowerShell 或 CMD 中执行:

# 下载
Invoke-WebRequest -Uri "https://arthas.aliyun.com/arthas-boot.jar" -OutFile "arthas-boot.jar"

三、启动 Arthas

1. 启动并选择目标进程

java -jar arthas-boot.jar

启动后会列出所有 Java 进程,输入对应的序号即可附加到目标进程。

2. 指定 PID 启动

java -jar arthas-boot.jar <pid>

3. 指定 IP 和端口(远程连接)

java -jar arthas-boot.jar --target-ip 127.0.0.1 --telnet-port 3658 --http-port 8563

四、常用命令详解

1. dashboard - 实时数据面板

dashboard

显示线程、内存、GC、类加载等实时信息,按 q 退出。

2. thread - 线程诊断

# 查看所有线程
thread

# 查看最忙的前 N 个线程
thread -n 3

# 查找死锁
thread -b

# 查看指定线程状态
thread <thread-id>

3. sc - 搜索已加载的类

# 搜索类
sc demo.MathGame

# 模糊搜索
sc *MathGame*

# 查看类的详细信息
sc -d demo.MathGame

# 查看类的字段信息
sc -d -f demo.MathGame

4. sm - 搜索已加载的方法

# 查看类的所有方法
sm demo.MathGame

# 查看方法的详细信息
sm -d demo.MathGame add

5. jad - 反编译类

# 反编译整个类
jad demo.MathGame

# 反编译指定方法
jad demo.MathGame add

# 输出到文件
jad --source-only demo.MathGame > /tmp/MathGame.java

6. watch - 观察方法执行

# 观察方法的入参和返回值
watch demo.MathGame add "{params, returnObj}"

# 观察异常信息
watch demo.MathGame add "{params, throwExp}" -e

# 只观察成功返回
watch demo.MathGame add "{params, returnObj}" -s

# 设置观察次数
watch demo.MathGame add "{params, returnObj}" -n 5

# 添加条件过滤(参数大于100)
watch demo.MathGame add "{params, returnObj}" "params[0] > 100"

7. trace - 方法调用链路追踪

# 追踪方法调用路径和时间
trace demo.MathGame run

# 设置耗时阈值(只显示超过10ms的调用)
trace demo.MathGame run '#cost > 10'

# 限制追踪层级
trace demo.MathGame run -n 1 --depth 3

# 包含 JDK 类
trace demo.MathGame run -j

8. tt - 时空隧道(记录方法调用)

# 记录方法调用
tt -t demo.MathGame add

# 查看记录列表
tt -l

# 重放某次调用
tt -i 1000 -p

# 查看某次调用的详细信息
tt -i 1000

9. ognl - 执行 OGNL 表达式

# 获取静态字段
ognl '@java.lang.System@out.println("hello")'

# 调用静态方法
ognl '@com.example.Utils@getConfig()'

# 查看 Spring Bean
ognl '@org.springframework.web.context.ContextLoader@getCurrentWebApplicationContext().getBean("userService")'

10. vmtool - JVM 工具

# 强制 GC
vmtool --action forceGc

# 获取对象实例
vmtool --action getInstances --className java.lang.String --limit 10

11. heapdump - 堆转储

# 生成 heapdump 文件
heapdump /tmp/dump.hprof

# 只导出存活对象
heapdump --live /tmp/dump.hprof

12. logger - 查看和修改日志级别

# 查看所有 logger
logger

# 查看指定 logger
logger -n com.example.controller

# 动态修改日志级别
logger -n com.example.controller --level debug

五、实用技巧

1. 组合使用 watch 和 trace

# 先定位慢方法
trace com.example.Service process

# 再观察具体参数和返回值
watch com.example.Service process "{params, returnObj, #cost}"

2. 条件过滤

# 只追踪特定参数的调用
trace demo.MathGame add 'params[0] > 100'

# 只观察异常情况
watch demo.MathGame add "{throwExp}" -e

3. 批量操作

# 同时观察多个方法
watch demo.MathGame {add,sub} "{params, returnObj}"

4. 退出 Arthas

# 在 Arthas 控制台输入
quit

# 或停止当前会话
stop

六、性能测试示例

查询项目pid:
netstat -ano | findstr xxxxx
查询项目使用jdk:
wmic process where "ProcessId=xxxx" get CommandLine
指定相同版本的jdk 指定端口号 启动:
C:\Users\Administrator\.jdks\semeru-11.0.28\bin\java.exe -jar arthas-boot.jar --telnet-port 9998 --http-port 9999 xxxx
# 追踪 importPlItem 方法(使用 lazy 模式)
trace -L demo.MathGame add -n 10

# 清除之前的追踪
reset demo.MathGame add

# 重新追踪,显示更详细的信息
trace -L demo.MathGame add -n 1 --skipJDKMethod false

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐