Arthas 指南
Arthas 指南
1. Artha解决了什么问题
在Java应用运维过程中棘手场景:
- 线上服务CPU突然飙高却找不到原因
- 关键接口响应变慢但日志看不出问题
- 内存泄漏导致频繁Full GC
- 需要热修复Bug但无法立即重启服务
传统解决方案:
- 加日志 → 重新部署 → 复现问题(周期长)
- 使用JDK工具(jstack/jmap)→ 信息碎片化
- 搭建复杂APM系统 → 成本高
Arthas提供了:
✔️ 零侵入:无需修改代码/重启服务
✔️ 全维度:JVM/线程/方法/类加载全方位观测
✔️ 强交互:实时诊断+动态热修复
✔️ 易用性:命令行交互,开箱即用
2. 快速安装与入门
2.1 多种安装方式
# 推荐方式 - 通过arthas-boot启动
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar# 其他安装方式
# 1. 全量包安装(包含完整依赖)
wget https://arthas.aliyun.com/arthas-packaging-3.6.7-bin.zip# 2. 通过as.sh快速启动
curl -L https://arthas.aliyun.com/install.sh | sh
2.2 基础工作流
# 1. 启动后选择目标Java进程
[INFO] arthas-boot version: 3.6.7
[INFO] Found existing java process...[1]: 1234 com.example.MainApp[2]: 5678 org.apache.catalina.startup.Bootstrap
Please select pid to attach [1-2]: 1# 2. 检查是否附着成功
[INFO] Successfully attach to pid: 1234
[arthas@1234]$
3. 核心功能全景
3.1 全局监控(Dashboard)
dashboard -i 2000 # 每2秒刷新一次
关键信息展示:
ID NAME STATE %CPU MEMORY TIME
1 main RUNNING 78% 1.2G/4G 12:34:56
2 http-nio-8080-exec-1 WAITING 2% 250M 00:01:23Memory: Heap=2.4G/4.0G, NonHeap=256M/512M
GC: PS Scavenge[count=12/time=345ms], PS MarkSweep[count=3/time=1.2s]
3.2 线程分析(Thread)
# 查看所有线程
thread# 找出CPU占用最高的3个线程
thread -n 3# 检测死锁
thread -b# 查看特定线程栈
thread 25
典型输出:
"http-nio-8080-exec-2" Id=25 cpu=98% WAITINGat java.lang.Object.wait(Native Method)at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)... 阻塞在任务队列获取
3.3 类/方法诊断
(1)类信息查询(sc)
sc -d com.example.UserService # 显示类详细信息
sc *.UserService # 模糊查询
输出包含:
- 类加载器
- 来源JAR包
- 继承关系
(2)方法查看(sm)
sm com.example.UserService getUserById # 特定方法
sm -d com.example.UserService # 类中所有方法详情
(3)反编译(jad)
jad com.example.UserService# 带源码高亮输出
jad --source-only com.example.UserService | highlight
3.4 方法级监控
(1)调用观测(watch)
watch com.example.UserService getUserById "{params,returnObj,throwExp}" -x 3
监控结果:
method=UserService.getUserById location=AtExit
params=@Object[][@Long[123]],
return=@User[id=123,name=张三],
throwExp=null
cost=12.34ms
(2)调用链路(trace)
trace com.example.OrderService createOrder -n 5
耗时分析:
`---[15.67ms] OrderService.createOrder()+---[2.34ms] UserService.getUserById() #12+---[10.56ms] PaymentService.process() #15`---[0.45ms] LogService.record() #18
(3)调用快照(tt)
# 记录调用现场
tt -t com.example.UserService getUserById -n 10# 回放特定调用
tt -i 1003 -p
3.5 动态热修复
# 1. 导出源码
jad --source-only com.example.BugService > BugService.java# 2. 修改代码(修复逻辑)
vim BugService.java# 3. 重新编译
mc BugService.java -d /tmp# 4. 热加载
redefine /tmp/com/example/BugService.class
4. 高级应用场景
4.1 内存泄漏排查
# 生成堆dump
heapdump /tmp/oom.hprof# 统计对象实例
vmtool --action getInstances --className java.util.HashMap --limit 10
4.2 性能火焰图
# 开始采样
profiler start# 30秒后停止并生成svg
profiler stop --format svg -o /tmp/flamegraph.svg
4.3 日志级别动态调整
# 查看logger状态
logger --name ROOT# 动态修改级别
logger --name ROOT --level DEBUG
5. 生产环境实战案例
案例1:线程池耗尽问题
现象:服务间歇性拒绝请求
排查:
# 1. 查看线程状态
thread -n 10 | grep pool# 2. 发现所有线程阻塞在DB查询
watch com.example.DBUtil query "{params, cost}" -x 3# 3. 定位到慢SQL,优化索引后解决
案例2:缓存穿透
现象:Redis CPU飙升
排查:
# 1. 追踪缓存方法调用
tt -t com.example.CacheService get -n 100# 2. 发现大量null值查询
watch com.example.CacheService get "{params[0], returnObj}" -x 2# 3. 添加布隆过滤器防护
案例3:类冲突问题
现象:NoSuchMethodError
排查:
# 1. 查看类加载来源
sc -d org.apache.commons.lang3.StringUtils# 2. 发现被老版本jar覆盖
# 3. 排除冲突依赖后解决
6. 最佳实践
- 安全防护:生产环境建议添加
--telnet-port 3658 --http-port 8563
限制访问 - 命令别名:创建
~/.arthasrc
定义常用命令组合 - 批量执行:使用
batch
命令执行脚本batch /tmp/arthas_commands.txt
- Web Console:通过
http://localhost:8563
访问Web界面
7. 总结
基础命令
- help - 查看帮助信息
- version - 显示 Arthas 版本
- quit/exit - 退出 Arthas
- jobs - 查看后台任务
- kill - 终止后台任务
- history - 查看命令历史
JVM 相关命令
- dashboard - 实时监控面板
- thread - 查看线程信息
thread -n 3
查看最忙的3个线程thread -b
查找阻塞线程thread id
查看指定线程堆栈
- jvm - 查看 JVM 信息
- sysprop - 查看/修改系统属性
- sysenv - 查看系统环境变量
- vmoption - 查看/修改 JVM 参数
类/方法相关命令
- sc (search class) - 查看类信息
sc -d com.example.Test
查看类详细信息
- sm (search method) - 查看方法信息
sm com.example.Test *
查看类中所有方法
- jad - 反编译类
jad com.example.Test
反编译类
- mc (memory compiler) - 编译.java文件为.class
- redefine - 热更新类
监控命令
- watch - 方法执行监控
watch com.example.Test method "{params,returnObj}" -x 2
- trace - 方法调用链路追踪
trace com.example.Test method -n 5
- stack - 方法调用路径
stack com.example.Test method
- tt (time tunnel) - 记录方法调用数据
tt -t com.example.Test method
tt -i 1000 -p
重放调用
其他诊断命令
- ognl - 执行ognl表达式
ognl '@java.lang.System@out.println("hello")'
- heapdump - 生成堆转储文件
heapdump /tmp/dump.hprof
- logger - 查看/修改logger
logger --name ROOT --level debug
- mbean - 查看 MBean 信息
- vmtool - 查询对象信息
vmtool --action getInstances --className java.lang.String --limit 10
高级功能
- profiler - 生成火焰图
profiler start
/profiler stop
- grep - 管道过滤
thread | grep 'main'
- tee - 重定向输出
thread | tee /tmp/thread.log
Arthas的核心价值:
- 实时诊断:快速定位CPU/内存/线程问题
- 动态干预:无需重启修复问题
- 全栈观测:从JVM到方法调用链路
学习建议路线:
- 掌握基础命令(dashboard/thread/watch)
- 熟练方法级诊断(trace/tt)
- 实践高级功能(redefine/profiler)
资源推荐:
- 官方文档
- 在线沙箱
- GitHub案例库