Arthas快速开始及常用命令

前言

最开始接触Arthas,是在今年的三月份,那时候算是我的转正考试吧,总监让我回去看下线上诊断工具,并对团队里的人讲解一下,那时候看了小马哥的Arthas视频,当时B站上还没有这么多教学视频,只有小马哥讲的最详细。

Arthas-logo

Arthas主要是定位线上出现的问题,因为很多问题在测试环境中很难再重现。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
1.这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
2.我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
3.遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
4.线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
5.是否有一个全局视角来查看系统的运行状况?
6.有什么办法可以监控到JVM的实时运行状态?
7.怎么快速定位应用的热点,生成火焰图?

具体如何操作Arthas及其Arthas的高阶用法移步Arthas官方手册

快速开始

Arthas下载链接

解压压缩文件,找到arthas-boot.jar文件

1
java -jar arthas-boot.jar

如下我们可以看到在本机上运行的java程序,选择需要诊断的进程

1
2
3
4
5
6
7
[root@zyj software]# java -jar arthas-boot.jar 
[INFO] arthas-boot version: 3.1.7
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 26902 arthas-boot.jar
[2]: 1223 com.alibaba.edas.agent.AgentDaemon
[3]: 26760 huanletao-server-0.0.1-SNAPSHOT.jar
3

当我们看到下面这个界面,就说明我们可以执行相关的arthas命令了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[INFO] arthas home: /root/.arthas/lib/3.4.5/arthas
[INFO] Try to attach process 26760
[INFO] Attach process 26760 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'


wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.4.5
pid 26760
time 2021-01-08 23:37:12

[arthas@26760]$

执行dashboard命令可以看到,当前进程信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ID NAME                GROUP     PRIORI STATE  %CPU  DELTA_ TIME   INTER DAEMON 
-1 C2 CompilerThread0 - -1 - 0.25 0.012 0:10.6 false true
-1 C1 CompilerThread1 - -1 - 0.13 0.006 0:2.41 false true
89 Timer-for-arthas-da system 5 RUNNAB 0.09 0.004 0:0.06 false true
28 TaskAcceptor-target eurekaTas 5 TIMED_ 0.07 0.003 0:0.46 false true
50 TaskAcceptor-localh eurekaTas 5 TIMED_ 0.07 0.003 0:0.45 false true
87 arthas-NettyHttpTel system 5 RUNNAB 0.04 0.002 0:0.10 false true
-1 VM Periodic Task Th - -1 - 0.02 0.001 0:0.16 false true
-1 VM Thread - -1 - 0.0 0.000 0:1.45 false true
67 http-nio-8888-Clien main 5 RUNNAB 0.0 0.000 0:0.03 false true
15 ContainerBackground main 5 TIMED_ 0.0 0.000 0:0.00 false true
Memory used total max usage GC
heap 67M 77M 444M gc.copy.count 316
eden_space 16M 21M 122M gc.copy.time(ms) 1120
survivor_space 2M 2M 15M gc.marksweepcompact 3
tenured_gen 48M 53M 306M .count
nonheap 71M 82M -1 gc.marksweepcompact 221
code_cache 5M 14M 240M 2.10% .time(ms)
metaspace 58M 60M -1
Runtime
os.name Linux
os.version 3.10.0-1062.4.3.el7.x86_64
java.version

具体高阶命令可以查看官方文档

常用命令

这个图是官方钉钉群某个大佬总结出来的,基本所有命令都在上面了,讲的很详细,如有侵权请联系删

常用命令


重要issue链接

由于Arthas拿不到非静态对象,于是最后这篇文章在web开发诊断中及其重要

1
2
3
4
5
#可以用tt命令的-i参数来指定index,并且用-w参数来执行ognl表达式来获取spring context
$ tt -i 1000 -w 'target.getApplicationContext()'

#获取到spring context之后,就可以获取到任意的bean了,比如获取到helloWorldService,并调用getHelloMessage()函数
$ tt -i 1000 -w 'target.getApplicationContext().getBean("helloWorldService").getHelloMessage()'

Arthas的一些特殊用法

Alibaba Arthas实践–获取到SpringContext然后为所欲为


如果有小伙伴,想要一起交流学习的,欢迎添加博主微信。

weChat