这是我在微信上看到的自己觉得挺不错的文章,特地分享一波。。。
Arthas是什么?
Arthas是一款阿里巴巴开源的 Java 线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题。
Arthas诊断使用的是命令行交互模式,支持JDK6+,Linux、Mac、Windows 操作系统,命令还支持使用 tab
键对各种信息的自动补全,诊断起来非常利索。
这是它的官方网站:
https:// alibaba.github.io/artha s/index.html
Github地址:
https:// github.com/alibaba/arth as
Arthas能解决什么问题?
看下 Arthas
的官方解释。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
1、这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
2、我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
3、遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
4、线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
5、是否有一个全局视角来查看系统的运行状况?
6、有什么办法可以监控到JVM的实时运行状态?
它可以在线反编译类,可以不用加日志在线调试跟踪问题代码。
有不少公司在使用吧,下面列出了官方给出的使用登录顺序图。
![bbce00a9d5ae8742c45cb86ece5c3b18.png](https://i-blog.csdnimg.cn/blog_migrate/dff94b766c5de2ef6ce89a1a7c0afc42.jpeg)
快速安装
官方推荐使用 arthas-boot
进行安装,非常方便,以下是基于 Linux 系统环境进行演示,一般解决线上问题也是基于 Linux 环境。
第一步:下载
在任何目录下载 arthas-boot
这个包。
wget https:// alibaba.github.io/artha s/arthas-boot.jar
[root@VM_0_7_centos ~]# wget https://alibaba.github.io/arthas/arthas-boot.jar
--2019-07-30 14:48:31-- https://alibaba.github.io/arthas/arthas-boot.jar
Resolving alibaba.github.io (alibaba.github.io)... 185.199.108.153, 185.199.109.153, 185.199.110.153, ...
Connecting to alibaba.github.io (alibaba.github.io)|185.199.108.153|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 98637 (96K) [application/java-archive]
Saving to: ‘arthas-boot.jar’
100%[==========================================================================================================>] 98,637 32.8KB/s in 2.9s
2019-07-30 14:48:36 (32.8 KB/s) - ‘arthas-boot.jar’ saved [98637/98637]
第二步:运行
使用 java -jar
命令执行 arthas-boot
包。
java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.1
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 13062 spring-boot-best-practice-0.0.1-SNAPSHOT.jar
第三步:选择进程
运行 arthas-boot
后,控制台会显示所有 Java 进程,选择一个你需要诊断的进程。
如第二步所示,这里有只有一个 Java 进程,输入序号1,回车,Arthas会附到目标进程上,并输出日志:
[INFO] Start download arthas from remote server: https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.1.1/arthas-packaging-3.1.1-bin.zip
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.1.1/arthas
[INFO] Try to attach process 13062
[INFO] Attach process 13062 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O | .--. ''--. .--'| '--' | / O ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || | | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.1
pid 13062
time 2019-07-30 14:49:34
至此,安装、启动完成。
更多其他安装方式见:https://alibaba.github.io/arthas/install-detail.html
实战使用
启动完成后,当前光标会进入 arthas
的控制台,接受各种操作命令。
1、dashboard
显示当前系统的实时数据面板,按 ctrl+c 即可退出。
$ dashboard
![8f3da50983e822c9b49820753685962d.png](https://i-blog.csdnimg.cn/blog_migrate/1a03e459736118eca5df4090b75aa037.jpeg)
2、thread
查看当前 JVM 的线程堆栈信息。
thread id, 显示指定线程的运行堆栈:
$ thread 20
显示当前最忙的前N个线程并打印堆栈:
$ thread -n 3
![252d2c8e1187de2f2ff3bf07e6ac485f.png](https://i-blog.csdnimg.cn/blog_migrate/002c6de4ef7ed57b48e4f21fdf506f2c.jpeg)
3、sc
查看 JVM 已加载的类详细信息。
$ sc -d *Test
![b1247db913ca0278bda51fa2203c0ac4.png](https://i-blog.csdnimg.cn/blog_migrate/92300c90da05b7593262dee9550ba383.jpeg)
4、sm
查看已加载类的方法信息。
$ sm -d cn.javastack.springbootbestpractice.SpringBootBestPracticeApplication main
5、jad
反编译指定已加载类的源代码。
$ jad cn.javastack.springbootbestpractice.SpringBootBestPracticeApplication
![06bf1b7ca9031952397047cf04877366.png](https://i-blog.csdnimg.cn/blog_migrate/1b3e4ac5da9d0eeea316faf95e2bb269.jpeg)
6、trace
显示方法内部调用路径,非实时返回的命令并输出方法路径上的总耗时,以及的每个节点上的详细耗时。
$ trace -j cn.javastack.springbootbestpractice.web.JsonTest getUserInfo
![91f0636645e31a7c92b159774ff00106.png](https://i-blog.csdnimg.cn/blog_migrate/0c231781786ece7c71c7df7669a5f199.png)
-j:表示跳过 JDK 中的方法路径。
7、monitor
对某个方法的调用进行定时监控。
$ monitor cn.javastack.springbootbestpractice.web.JsonTest getUserInfo -c 5
![4d49cafeff034a7d52cf1ee46699aa6e.png](https://i-blog.csdnimg.cn/blog_migrate/8e63936a6d8e51649d36eed0de82cd0a.jpeg)
-c 5:表示每5秒统计一次,统计周期,默认值为120秒。
![e93dc3bb27b8e7b29a64aed41b2b0049.png](https://i-blog.csdnimg.cn/blog_migrate/ec319dec5d13c308a9fde3171b91c7f0.jpeg)
8、watch
观测方法执行数据,能方便的观察到指定方法的调用情况,如:返回值、抛出异常、入参等。
$ watch cn.javastack.springbootbestpractice.web.JsonTest getUserInfo '{params, returnObj}' -x 2 -b
![73b816067fc923bb018e75f444cecc3d.png](https://i-blog.csdnimg.cn/blog_migrate/fee74a20dfccfe8f249d17fe3d410205.jpeg)
以上监控的是一个方法的入参情况,在方法执行前监控:-b,遍历深度:-x 2。
9、quit/exit
退出当前 Arthas。
这个命令仅退出当前连接的客户端,附到目标进程上的 Arthas 会继续运行,端口不会关闭,下次连接时可以直接连接使用。
10、shutdown
关闭 Arthas 服务端,退出所有 Arthas 客户端。
以上演示了 10 个命令的基本使用,各种命令的使用详情可以在命令带 --help
进行查阅。
![42c818941d57b5ea05f204ac82d4690e.png](https://i-blog.csdnimg.cn/blog_migrate/816a03af454247e76872cb5d33de451f.jpeg)
更多其他命令请参考:
命令列表 - Arthas 3.1.1 文档alibaba.github.io原文:
https://mp.weixin.qq.com/s?__biz=MzI3ODcxMzQzMw==&mid=2247490276&idx=1&sn=ce18a2594542768af552c5be0dc913d1&chksm=eb539fd2dc2416c40e6ebfe3ee71e4af3c628fe0c46e5db99b4f11b8d6687f1695ea9ca0756d&mpshare=1&scene=23&srcid=&sharer_sharetime=1564623456009&sharer_shareid=053ac803faf174ddaef9340755aac692#rdmp.weixin.qq.com