Ollama 为您提供全面的 Arthas 使用指南,包含从入门到高级的完整教程
·
我将为您提供全面的 Arthas 使用指南,包含从入门到高级的完整教程。
1. Arthas 简介和安装
什么是 Arthas?
Arthas 是阿里巴巴开源的 Java 诊断工具,可以在不重启应用的情况下进行问题诊断。
安装方法
bash
# 方法1:直接下载
wget https://arthas.aliyun.com/arthas-boot.jar
# 方法2:使用包管理器
curl -L https://arthas.aliyun.com/install.sh | sh
# 方法3:Docker方式
docker pull arthas/arthas:latest
# 方法1:直接下载
wget https://arthas.aliyun.com/arthas-boot.jar
# 方法2:使用包管理器
curl -L https://arthas.aliyun.com/install.sh | sh
# 方法3:Docker方式
docker pull arthas/arthas:latest
启动 Arthas
bash
# 启动并选择目标Java进程
java -jar arthas-boot.jar
# 直接附加到指定PID
java -jar arthas-boot.jar --target-ip 127.0.0.1 --telnet-port 3658 --http-port 8563
# 查看帮助
java -jar arthas-boot.jar -h
# 启动并选择目标Java进程
java -jar arthas-boot.jar
# 直接附加到指定PID
java -jar arthas-boot.jar --target-ip 127.0.0.1 --telnet-port 3658 --http-port 8563
# 查看帮助
java -jar arthas-boot.jar -h
2. 基础命令
查看帮助和系统信息
bash
# 查看所有命令
help
# 查看系统状态
sysprop # 查看系统属性
sysenv # 查看环境变量
# 查看JVM信息
jvm # JVM基本信息
dashboard # 实时监控面板
# 查看所有命令
help
# 查看系统状态
sysprop # 查看系统属性
sysenv # 查看环境变量
# 查看JVM信息
jvm # JVM基本信息
dashboard # 实时监控面板
进程信息查看
bash
# 查看当前连接的进程
thread # 查看线程
thread -n 3 # 查看最忙的3个线程
thread -b # 查看阻塞线程
# 内存信息
memory # 内存概况
heapdump /tmp/dump.hprof # 堆转储
# 查看当前连接的进程
thread # 查看线程
thread -n 3 # 查看最忙的3个线程
thread -b # 查看阻塞线程
# 内存信息
memory # 内存概况
heapdump /tmp/dump.hprof # 堆转储
3. 类加载和监控
类加载查询
bash
# 查看已加载的类
sc -d *Controller # 查找Controller类
sc -d com.example.* # 查找包下的类
# 查看类详情
sc -d com.example.UserService
# 查看方法信息
sm com.example.UserService *Method # 查看类的方法
# 查看已加载的类
sc -d *Controller # 查找Controller类
sc -d com.example.* # 查找包下的类
# 查看类详情
sc -d com.example.UserService
# 查看方法信息
sm com.example.UserService *Method # 查看类的方法
方法监控
bash
# 监控方法调用
watch com.example.UserService getUserInfo '{params, returnObj, throwExp}' -n 5
# 监控方法执行时间
watch com.example.UserService * '{params, returnObj, #cost}' -n 10
# 监控方法调用
watch com.example.UserService getUserInfo '{params, returnObj, throwExp}' -n 5
# 监控方法执行时间
watch com.example.UserService * '{params, returnObj, #cost}' -n 10
4. 动态跟踪和诊断
方法执行跟踪
bash
# 跟踪方法调用路径
trace com.example.UserService getUserInfo -n 3
# 跟踪多层调用
trace com.example.UserService getUserInfo '#cost > 10' -n 5
# 按耗时过滤
trace com.example.* * '#cost > 100' -n 5
# 跟踪方法调用路径
trace com.example.UserService getUserInfo -n 3
# 跟踪多层调用
trace com.example.UserService getUserInfo '#cost > 10' -n 5
# 按耗时过滤
trace com.example.* * '#cost > 100' -n 5
观察方法执行
bash
# 观察方法入参和返回值
watch com.example.UserService login '{params, returnObj}' -x 3
# 观察异常
watch com.example.UserService * '{params, throwExp}' -e -x 2
# 观察特定条件
watch com.example.UserService getUserInfo 'params[0]=="admin"' -x 3
# 观察方法入参和返回值
watch com.example.UserService login '{params, returnObj}' -x 3
# 观察异常
watch com.example.UserService * '{params, throwExp}' -e -x 2
# 观察特定条件
watch com.example.UserService getUserInfo 'params[0]=="admin"' -x 3
5. 性能分析
方法执行耗时分析
bash
# 监控方法执行时间
monitor -c 5 com.example.UserService getUserInfo
# 统计方法调用
monitor -c 10 com.example.UserService *
# 带条件监控
monitor -c 5 com.example.UserService getUserInfo '#cost > 100'
# 监控方法执行时间
monitor -c 5 com.example.UserService getUserInfo
# 统计方法调用
monitor -c 10 com.example.UserService *
# 带条件监控
monitor -c 5 com.example.UserService getUserInfo '#cost > 100'
火焰图生成
bash
# 生成CPU火焰图
profiler start # 开始采样
profiler stop # 停止并生成火焰图
profiler list # 查看支持的事件
# 内存分配火焰图
profiler start --event alloc
profiler stop --format html
# 生成CPU火焰图
profiler start # 开始采样
profiler stop # 停止并生成火焰图
profiler list # 查看支持的事件
# 内存分配火焰图
profiler start --event alloc
profiler stop --format html
6. 代码热更新
热修复类
bash
# 1. 反编译类文件
jad --source-only com.example.UserService > /tmp/UserService.java
# 2. 编辑源代码(修复bug)
vim /tmp/UserService.java
# 3. 编译修改后的代码
mc /tmp/UserService.java -d /tmp
# 4. 重新加载类
redefine /tmp/com/example/UserService.class
# 1. 反编译类文件
jad --source-only com.example.UserService > /tmp/UserService.java
# 2. 编辑源代码(修复bug)
vim /tmp/UserService.java
# 3. 编译修改后的代码
mc /tmp/UserService.java -d /tmp
# 4. 重新加载类
redefine /tmp/com/example/UserService.class
热更新示例
bash
# 查看原始代码
jad com.example.BugService doSomething
# 编译新版本(修改了bug)
mc /tmp/BugService.java -d /tmp
# 重新定义类
redefine /tmp/com/example/BugService.class
# 验证修改是否生效
jad com.example.BugService doSomething
# 查看原始代码
jad com.example.BugService doSomething
# 编译新版本(修改了bug)
mc /tmp/BugService.java -d /tmp
# 重新定义类
redefine /tmp/com/example/BugService.class
# 验证修改是否生效
jad com.example.BugService doSomething
7. 线程分析
线程状态监控
bash
# 查看所有线程
thread
# 查看线程堆栈
thread 1 # 查看线程1的堆栈
thread -i 1000 # 每1秒刷新一次
# 查找阻塞线程
thread -b
# 统计线程状态
thread --state BLOCKED
# 查看所有线程
thread
# 查看线程堆栈
thread 1 # 查看线程1的堆栈
thread -i 1000 # 每1秒刷新一次
# 查找阻塞线程
thread -b
# 统计线程状态
thread --state BLOCKED
死锁检测
bash
# 检测死锁
thread -b
# 查看线程CPU占用
thread -n 3
# 监控线程创建
thread --all | grep -i "new"
# 检测死锁
thread -b
# 查看线程CPU占用
thread -n 3
# 监控线程创建
thread --all | grep -i "new"
8. 内存分析
内存泄漏检测
bash
# 查看堆内存
memory
# 查看对象实例数量
heapdump --live /tmp/heapdump.hprof
# 统计对象数量
vmtool -action getInstances -className java.lang.String -limit 10
# 查看堆内存
memory
# 查看对象实例数量
heapdump --live /tmp/heapdump.hprof
# 统计对象数量
vmtool -action getInstances -className java.lang.String -limit 10
垃圾回收监控
bash
# 查看GC情况
jvm | grep -i gc
# 触发GC
vmtool -action forceGc
# 查看堆内存详情
jvm --memory
# 查看GC情况
jvm | grep -i gc
# 触发GC
vmtool -action forceGc
# 查看堆内存详情
jvm --memory
9. 实战案例
案例1:CPU使用率过高
bash
# 1. 查看CPU占用最高的线程
thread -n 3
# 2. 跟踪可疑方法
trace com.example.HighCPUService * '#cost > 100'
# 3. 监控方法执行
watch com.example.HighCPUService processData '{params, #cost}' -n 10
# 4. 生成火焰图分析
profiler start
# 等待一段时间
profiler stop --format svg
# 1. 查看CPU占用最高的线程
thread -n 3
# 2. 跟踪可疑方法
trace com.example.HighCPUService * '#cost > 100'
# 3. 监控方法执行
watch com.example.HighCPUService processData '{params, #cost}' -n 10
# 4. 生成火焰图分析
profiler start
# 等待一段时间
profiler stop --format svg
案例2:内存泄漏排查
bash
# 1. 查看内存趋势
dashboard
# 2. 查看大对象
vmtool -action getInstances -className com.example.BigObject -limit 10
# 3. 堆转储分析
heapdump /tmp/leak.hprof
# 4. 监控对象创建
watch com.example.ObjectFactory createObject '{params, returnObj}' -n 5
# 1. 查看内存趋势
dashboard
# 2. 查看大对象
vmtool -action getInstances -className com.example.BigObject -limit 10
# 3. 堆转储分析
heapdump /tmp/leak.hprof
# 4. 监控对象创建
watch com.example.ObjectFactory createObject '{params, returnObj}' -n 5
案例3:方法调用失败分析
bash
# 1. 监控方法异常
watch com.example.UserService * '{params, throwExp}' -e -x 3
# 2. 跟踪调用链
trace com.example.UserService getUserInfo
# 3. 查看方法参数
watch com.example.UserService getUserInfo params -x 3
# 1. 监控方法异常
watch com.example.UserService * '{params, throwExp}' -e -x 3
# 2. 跟踪调用链
trace com.example.UserService getUserInfo
# 3. 查看方法参数
watch com.example.UserService getUserInfo params -x 3
10. 高级功能
条件表达式
bash
# 条件过滤
watch com.example.Service * 'params.length > 0 && #cost > 100'
# 复杂条件
watch com.example.Service * 'returnObj != null && returnObj.size() > 10'
# 条件过滤
watch com.example.Service * 'params.length > 0 && #cost > 100'
# 复杂条件
watch com.example.Service * 'returnObj != null && returnObj.size() > 10'
OGNL表达式
bash
# 调用静态方法
ognl '@java.lang.System@currentTimeMillis()'
# 查看Spring上下文
ognl '#context=@com.example.SpringContext@getContext(), #context.getBean("userService")'
# 修改静态变量(谨慎使用)
ognl '@com.example.Config@DEBUG=true'
# 调用静态方法
ognl '@java.lang.System@currentTimeMillis()'
# 查看Spring上下文
ognl '#context=@com.example.SpringContext@getContext(), #context.getBean("userService")'
# 修改静态变量(谨慎使用)
ognl '@com.example.Config@DEBUG=true'
批量操作
bash
# 批量监控多个方法
watch com.example.* * '{params, returnObj}' -n 5
# 批量跟踪
trace com.example.service.* * '#cost > 50'
# 批量监控多个方法
watch com.example.* * '{params, returnObj}' -n 5
# 批量跟踪
trace com.example.service.* * '#cost > 50'
11. Web Console
启动Web界面
bash
# 启动时指定HTTP端口
java -jar arthas-boot.jar --http-port 8563
# 访问Web界面
http://localhost:8563
# 启动时指定HTTP端口
java -jar arthas-boot.jar --http-port 8563
# 访问Web界面
http://localhost:8563
Web界面功能
- 实时监控仪表板
- 命令执行界面
- 火焰图可视化
- 历史命令记录
12. 集成和使用技巧
与Spring Boot集成
bash
# 在Spring Boot应用中直接使用
java -javaagent:arthas-agent.jar -jar app.jar
# 或在启动后附加
# 查找Spring Boot应用PID
jps | grep myapp
java -jar arthas-boot.jar <pid>
# 在Spring Boot应用中直接使用
java -javaagent:arthas-agent.jar -jar app.jar
# 或在启动后附加
# 查找Spring Boot应用PID
jps | grep myapp
java -jar arthas-boot.jar <pid>
生产环境使用建议
bash
# 1. 使用安全的端口
java -jar arthas-boot.jar --telnet-port 3658 --http-port 8563
# 2. 设置访问IP限制
java -jar arthas-boot.jar --target-ip 127.0.0.1
# 3. 使用身份验证(企业版)
java -jar arthas-boot.jar --username admin --password 123456
# 1. 使用安全的端口
java -jar arthas-boot.jar --telnet-port 3658 --http-port 8563
# 2. 设置访问IP限制
java -jar arthas-boot.jar --target-ip 127.0.0.1
# 3. 使用身份验证(企业版)
java -jar arthas-boot.jar --username admin --password 123456
常用命令别名
bash
# 创建常用命令的别名
alias watchUser='watch com.example.UserService * "{params, returnObj, #cost}" -n 5'
alias traceSlow='trace com.example.* * "#cost > 100" -n 3'
# 创建常用命令的别名
alias watchUser='watch com.example.UserService * "{params, returnObj, #cost}" -n 5'
alias traceSlow='trace com.example.* * "#cost > 100" -n 3'
13. 故障排查场景
慢查询分析
bash
# 1. 查看线程状态
thread -n 5
# 2. 跟踪数据库操作
trace com.example.*Mapper * '#cost > 1000'
# 3. 监控SQL执行
watch com.example.UserDAO * '{params, #cost}' -n 10
# 1. 查看线程状态
thread -n 5
# 2. 跟踪数据库操作
trace com.example.*Mapper * '#cost > 1000'
# 3. 监控SQL执行
watch com.example.UserDAO * '{params, #cost}' -n 10
接口超时分析
bash
# 1. 监控HTTP请求
watch org.springframework.web.servlet.DispatcherServlet doService '{params, #cost}' -n 5
# 2. 跟踪调用链路
trace com.example.Controller * --skipJDKMethod false
# 3. 检查外部调用
trace com.example.*Client * '#cost > 500'
# 1. 监控HTTP请求
watch org.springframework.web.servlet.DispatcherServlet doService '{params, #cost}' -n 5
# 2. 跟踪调用链路
trace com.example.Controller * --skipJDKMethod false
# 3. 检查外部调用
trace com.example.*Client * '#cost > 500'
14. 安全退出
优雅退出Arthas
bash
# 停止Arthas但不退出目标JVM
stop
# 重置所有增强的类
reset
# 完全退出
quit
# 关闭Arthas服务器
shutdown
# 停止Arthas但不退出目标JVM
stop
# 重置所有增强的类
reset
# 完全退出
quit
# 关闭Arthas服务器
shutdown
15. 最佳实践
诊断流程
- 定位问题:使用
dashboard快速查看系统状态 - 深入分析:使用
thread、memory等命令定位具体问题 - 代码级诊断:使用
watch、trace分析具体方法 - 解决方案:使用热更新或提供修复建议
注意事项
- 生产环境谨慎使用热更新
- 监控可能带来性能开销
- 及时停止不必要的监控命令
- 备份重要数据后再进行操作
这个完整的 Arthas 指南涵盖了从基础到高级的所有功能。您需要针对特定问题的详细诊断方案吗?
更多推荐



所有评论(0)