当前位置:首页>Linux>Linux系统下Arthas安装与排查指南

Linux系统下Arthas安装与排查指南

  • 2026-09-08 11:37:31
Linux系统下Arthas安装与排查指南

一、Arthas简介

Arthas是Alibaba开源的Java诊断工具,能够帮助开发者快速定位线上问题,支持查看类加载信息、方法调用、线程堆栈、JVM信息等功能。

二、安装方式

1. 快速安装(推荐)

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

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

# 启动arthas(需要先有Java进程)
java -jar arthas-boot.jar

2. 通过YUM安装(CentOS/RHEL)

# 添加仓库
wget https://arthas.aliyun.com/arthas.repo -O /etc/yum.repos.d/arthas.repo

# 安装
yum install -y arthas

3. 完整版安装(包含所有依赖)

# 下载完整版
wget https://arthas.aliyun.com/download/latest_version?mirror=aliyun -O arthas.zip

# 解压
unzip arthas.zip -d arthas
cd arthas

# 启动
./as.sh

三、基本使用流程

1. 启动Arthas

# 选择Java进程启动
java -jar arthas-boot.jar

# 或指定PID启动
java -jar arthas-boot.jar <pid>

# 或直接连接
./as.sh <pid>

2. 常用排查命令

查看基本信息

# 查看仪表盘
dashboard

# 查看JVM信息
jvm

# 查看系统属性
sysprop

# 查看系统环境变量
sysenv

# 查看线程信息
thread
thread -n3# 查看前3个最忙的线程
thread -b# 查看阻塞线程
thread --state WAITING  # 查看等待状态的线程

类和方法排查

# 查看已加载类
sc -d com.example.ClassName
sc *Service  # 通配符查找

# 查看类的方法信息
sm com.example.ClassName
sm com.example.ClassName methodName

# 反编译代码
jad com.example.ClassName
jad com.example.ClassName methodName

# 查看类加载器
classloader
classloader -t# 查看类加载器树

方法监控

# 监控方法调用(耗时、成功率等)
monitor -c5 com.example.ClassName methodName

# 追踪方法调用路径
trace com.example.ClassName methodName

# 查看方法调用栈
stack com.example.ClassName methodName

# 查看方法参数和返回值
watch com.example.ClassName methodName "{params,returnObj}"-x2

# 方法执行时间统计
tt -t com.example.ClassName methodName

性能分析

# CPU Profiler(火焰图)
profiler start
profiler stop--format html

# 查看最耗时的调用
trace com.example.ClassName methodName --skipJDKMethodfalse

四、实战排查场景

场景1:CPU飙升排查

# 1. 查看最忙的线程
thread -n5

# 2. 查看具体线程堆栈
thread <thread-id>

# 3. 监控方法调用
monitor -c5 com.example.BusyClass heavyMethod

# 4. 追踪耗时方法
trace com.example.BusyClass heavyMethod -n5

场景2:内存溢出排查

# 1. 查看堆内存使用
jvm

# 2. 查看GC情况
jvm | grep-A10"Garbage Collectors"

# 3. 查看对象实例统计
heapdump --live /tmp/dump.hprof

# 4. 查看大对象
vmtool --action getInstances --className java.util.ArrayList --limit10

场景3:接口响应慢

# 1. 追踪整个调用链
trace com.example.controller.UserController getUserInfo -n5--skipJDKMethodfalse

# 2. 查看方法耗时分布
monitor -c5 com.example.service.UserService getUserInfo

# 3. 监控SQL执行时间
watch com.example.mapper.UserMapper selectById "{params,returnObj,throwExp}"-x2

场景4:线上代码热更新

# 1. 反编译类
jad --source-only com.example.BuggyClass > /tmp/BuggyClass.java

# 2. 修改源码
vim /tmp/BuggyClass.java

# 3. 重新编译并替换
mc /tmp/BuggyClass.java -d /tmp
redefine /tmp/com/example/BuggyClass.class

五、高级功能

1. 隧道连接(远程诊断)

# 启动时开启隧道服务
java -jar arthas-boot.jar --tunnel-server ws://arthas-tunnel-server:7777/ws

2. 后台异步任务

# 后台执行命令
trace com.example.ClassName methodName &  # &符号放到后台

# 查看后台任务
jobs

# 停止任务
kill <job-id>

3. 导出结果

# 将结果输出到文件
trace com.example.ClassName methodName > /tmp/trace.log 2>&1

# 使用重定向
dashboard | tee /tmp/dashboard.log

六、性能影响与注意事项

  1. 生产环境使用注意:

    • 非必要不长期运行,使用完毕及时退出(quit或exit)

    • trace、monitor等监控命令会带来性能开销(通常5-10%)

    • profiler start采样时CPU使用率会上升

  2. 权限问题:

    • 确保运行Arthas的用户与目标Java进程用户一致

    • 可能需要使用sudo启动

  3. 常见问题:

    • 连接不上:检查防火墙、端口是否被占用

    • 类找不到:检查类加载器,使用classloader命令切换

    • 无法attach:使用-v参数查看详细错误信息

七、快速参考命令

命令用途示例
dashboard实时监控面板dashboard -n 5
thread查看线程thread -n 3
sc查看类信息sc -d com.example.*
sm查看方法sm com.example.Class
jad反编译jad com.example.Class
watch监控方法watch Class method "{params,returnObj}"
trace跟踪调用链trace Class method
monitor监控统计monitor -c 5 Class method
heapdump导出堆快照heapdump /tmp/heap.hprof
profilerCPU分析profiler start

八、退出与清理

# 退出Arthas
quit
# 或
exit

# 停止所有后台任务
stop

# 彻底关闭Arthas(退出所有会话)
shutdown

最新文章

随机文章