2019独角兽企业重金招聘Python工程师标准>>>
Arthas
是Alibaba开源的Java诊断工具,深受开发者喜爱。
当你遇到以下类似问题而束手无策时,Arthas
可以帮助你解决:
- 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
- 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态?
Arthas
采用命令行交互模式,同时提供丰富的 Tab
自动补全功能,进一步方便进行问题的定位和诊断。
官网链接:https://alibaba.github.io/arthas/ ps详细的命令以及说明可以去官网查看。
1:安装环境 jdk1.8+,ubuntu 14.04, ubuntu要有telnet unzip等基本常用工具。
2:官网提供两种安装方式
自动安装:
Linux/Unix/Mac
Arthas 支持在 Linux/Unix/Mac 等平台上一键安装,请复制以下内容,并粘贴到命令行中,敲 回车
执行即可:
curl -L https://alibaba.github.io/arthas/install.sh | sh
我最开始使用自动安装遇到一个问题 :Unable to access jarfile /root/.arthas/lib/3.0.4/arthas/arthas-core.jar
发现这个arthas-core.jar 一直下载不下来,后来换成手动安装方式。
手动安装:
Linux/Unix/Mac
1:获取安装包
wget https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.0.4/arthas-packaging-3.0.4-bin.zip
2:解压安装包
unzip arthas-packaging-3.0.4-bin.zip
3:安装
./install-local.sh
4:运行
./as.sh
官网提供的手动安装 版本的as.sh脚本运行时报错,后来我先自动安装了一下,拿到自动安装的as.sh脚本,替换了手动的as.sh
快速入门
1. 启动Demo
import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class Demo { static class Counter { private static AtomicInteger count = new AtomicInteger(0); public static void increment() { count.incrementAndGet(); } public static int value() { return count.get(); } } public static void main(String[] args) throws InterruptedException { while (true) { Counter.increment(); System.out.println("counter: " + Counter.value()); TimeUnit.SECONDS.sleep(1); } } }
把上面的内容保存到Demo.java
里,然后在命令行下编绎启动:
javac Demo.java java Demo
也可以把代码保存到IDE里,然后启动。
2. 启动arthas
Linux/Unix/Mac
在命令行下面执行:
./as.sh
执行该脚本的用户需要和目标进程具有相同的权限。比如以
admin
用户来执行:sudo su admin && ./as.sh
或sudo -u admin -EH ./as.sh
。 详细的启动脚本说明,请参考这里。 如果attatch不上目标进程,可以查看~/logs/arthas/
目录下的日志。
选择应用java进程:
$ ./as.sh Arthas script version: 3.0.2 Found existing java process, please choose one and hit RETURN. * [1]: 95428 [2]: 22647 org.jetbrains.jps.cmdline.Launcher [3]: 21736 [4]: 13560 Demo
Demo进程是第4个,则输入4,再输入回车/enter。Arthas会attach到目标进程上,并输出日志:
Connecting to arthas server... current timestamp is 1536656867 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.1-RC-SNAPSHOT pid: 13560 timestamp: 1536656867894
Windows
打开Dos命令行窗口,在解压的arthas目录下执行as.bat pid
。
3. 查看dashboard
输入dashboard,按enter/回车,会展示当前进程的信息,按ctrl+c
可以中断执行。
$ dashboard ID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON 17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false 27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true 11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true 9 Attach Listener system 9 RUNNAB 0 0:0 false true 3 Finalizer system 8 WAITIN 0 0:0 false true 2 Reference Handler system 10 WAITIN 0 0:0 false true 4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true 26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true 13 job-timeout system 9 TIMED_ 0 0:0 false true 1 main main 5 TIMED_ 0 0:0 false false 14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false 18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false 23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false 15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false false Memory used total max usage GC heap 32M 155M 1820M 1.77% gc.ps_scavenge.count 4 ps_eden_space 14M 65M 672M 2.21% gc.ps_scavenge.time(m 166 ps_survivor_space 4M 5M 5M s) ps_old_gen 12M 85M 1365M 0.91% gc.ps_marksweep.count 0 nonheap 20M 23M -1 gc.ps_marksweep.time( 0 code_cache 3M 5M 240M 1.32% ms) Runtime os.name Mac OS X os.version 10.13.4 java.version 1.8.0_162 java.home /Library/Java/JavaVir tualMachines/jdk1.8.0 _162.jdk/Contents/Hom e/jre
4. watch
通过watch命令来查看Counter.value()
函数的返回值:
$ watch Demo$Counter value returnObj Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 29 ms. ts=2018-09-10 17:53:11;result=@Integer[621] ts=2018-09-10 17:53:12;result=@Integer[622] ts=2018-09-10 17:53:13;result=@Integer[623] ts=2018-09-10 17:53:14;result=@Integer[624] ts=2018-09-10 17:53:15;result=@Integer[625]
更多的功能可以查看进阶使用。
5. 退出arthas
如果只是退出当前的连接,可以用quit
或者exit
命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出arthas,可以执行shutdown
命令。