Arthas简介:
Arthas是Alibaba开源的Java诊断工具。它可以协助解决以下问题:
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
是否有一个全局视角来查看系统的运行状况?
有什么办法可以监控到JVM的实时运行状态?
怎么快速定位应用的热点,生成火焰图?
怎样直接从JVM内查找某个类的实例?
我一般用来找出高并发场景中,影响性能的瓶颈所在,所以这里简单介绍一下如何使用Arthas找出系统性能瓶颈。
Arthas简单使用
1.下载Arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
2.先启动被诊断的进程,然后运行Arthas
java -jar arthas-boot.jar
运行后会显示正在运行的java程序,每个进程都有编号,如:
[INFO] arthas-boot version: 3.5.4
[INFO] Process 38220 already using port 3658
[INFO] Process 38220 already using port 8563
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 38220 study-0.0.1-SNAPSHOT.jar
3.输入被诊断的进程前面的序号,然后回车,等输出类似如下的日志以后,就可以开始使用Arthas的命令了
[INFO] arthas home: C:\Users\java_t_t\.arthas\lib\3.5.4\arthas
[INFO] The target process already listen port 3658, skip attach.
[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.5.4
main_class
pid 38220
time 2021-10-17 23:30:31
[arthas@38220]$
4.运行trace命令
trace -E com.example.study.controller.ResourceController test|uploadFileToLocal --skipJDKMethod false
其中:
-E:正则匹配函数
com.example.study.controller.ResourceController:被诊断的类
test|uploadFileToLocal:被诊断的方法(test和uploadFileToLocal)
--skipJDKMethod:是否需要跳过JDK方法(默认为true)
5.访问对应的接口,观察Arthas输出,如:
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 2) cost in 93 ms, listenerId: 8
`---ts=2021-10-18 00:55:32;thread_name=http-nio-8080-exec-3;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@318ba8c8
`---[1.7917ms] com.example.study.controller.ResourceController:test()
+---[0.1067ms] java.io.File:<init>() #29
+---[0.2952ms] java.io.FileReader:<init>() #30
+---[0.124699ms] java.io.BufferedReader:<init>() #31
+---[0.009499ms] java.lang.StringBuilder:<init>() #32
+---[min=0.0021ms,max=0.132899ms,total=0.352502ms,count=45] java.io.BufferedReader:readLine() #34
+---[min=0.001999ms,max=0.042ms,total=0.201797ms,count=44] java.lang.StringBuilder:append() #35
`---[0.0196ms] java.lang.StringBuilder:toString() #37
参考文档: