相信不少人都有这样一种感觉,本身写的代码在开发、测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,而线上调试代码又很麻烦,让人头疼得很。不过, 阿里巴巴出了一款名叫Arthas的工具,能够在线分析诊断Java代码,让人眼前一亮。❞java
Arthas(阿尔萨斯) 是阿里开源的一个Java在线分析诊断工具。git
在平常开发上线过程当中,咱们多多少少都会遇到下边这些问题,苦于没法在线调试,「只能经过老鸟的经验来硬分析bug,效率上不去还总开口问别人答疑解惑,多少有些很差意思」。程序员
wget https://alibaba.github.io/art...github
java -jar arthas-boot.jar --target-ip 0.0.0.0
复制代码web
首先想用arthas调试项目,服务器必需要有运行着的Java服务,demo-0.0.1-SNAPSHOT.jar就是我启动的测试项目,启动arthas后它会自动检测本地全部的Java服务列出来,咱们只需按照序号输入想要调试的项目便可,选1进入对应进程的arthas交互平台spring
[root@iz2zehzeir87zi8q99krk1z data]# java -jar arthas-boot.jar --target-ip 172.17.72.201
[INFO] arthas-boot version: 3.1.0
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 28679 demo-0.0.1-SNAPSHOT.jar
复制代码shell
curl -L https://alibaba.github.io/art... | sh
复制代码数组
执行上面的命令会在所在的文件中生成as.sh执行文件tomcat
启动arthas服务器
./as.sh PID #进程id 指定JAVA进程id
./as.sh -h #h来获取更多参数信息
复制代码
「要想使用arthas服务的 web console必须对外暴露本机ip」
java -jar arthas-boot.jar --target-ip 172.17.72.201
java -jar arthas-boot.jar --telnet-port 9999 --http-port -1
复制代码
./as.sh --target-ip 0.0.0.0
./as.sh --telnet-port 9999 --http-port -1
复制代码
访问arthas控制台也有两种方法
「(1)、web console 界面」
「重点说明」:--target-ip 的ip 必定要是arthas所在机器对外暴露的ip,「但若是用的是阿里云机器必需要使用私有ip启动arthas服务,但访问必须是公网IP」
「(2)、telnet方式」
telnet 10.0.2.5 8563
复制代码
访问 http://59.110.218.9:8563/ ,进入交互平台
查看当前系统的实时数据面板,例如:服务器thread信息、内存memory、GC回收等状况
$ thread -n 3
"as-command-execute-daemon" Id=57 cpuUsage=72% RUNNABLE
at sun.management.ThreadImpl.dumpThreads0(Native Method)
at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:448)
at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133)
at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:370)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Number of locked synchronizers = 1
- java.util.concurrent.ThreadPoolExecutor$Worker@a2f70c7
复制代码
「能够看到这个线程是被synchroned关键字锁致使的阻塞」 ,目前只支持找出synchronized关键字阻塞住的线程, 若是是java.util.concurrent.Lock, 目前还不支持。
Number of locked synchronizers = 1
- java.util.concurrent.ThreadPoolExecutor$Worker@a2f70c7
thread -n 3 #当前最忙的前N个线程
thread -b, ##找出当前阻塞其余线程的线程
thread -n 3 -i 1000 #间隔必定时间后展现
复制代码
「重点学习」:thread -b, ##「找出当前阻塞其余线程的线程」
$ jvm
MACHINE-NAME 28679@iz2zehzeir87zi8q99krk1z
JVM-START-TIME 2019-03-28 17:32:16
MANAGEMENT-SPEC-VERSION 1.2
SPEC-NAME Java Virtual Machine Specification
SPEC-VENDOR Oracle Corporation
SPEC-VERSION 1.8
VM-NAME Java HotSpot(TM) 64-Bit Server VM
VM-VENDOR Oracle Corporation
VM-VERSION 25.191-b12
INPUT-ARGUMENTS []
CLASS-PATH demo-0.0.1-SNAPSHOT.jar
BOOT-CLASS-PATH /usr/local/jdk/jre/lib/resources.jar:/usr/local/jdk/jre/lib/rt.jar:/usr/local/jdk/jre/lib/sunrsasign.jar:/usr/local/jdk/jre/lib/jsse.jar:/usr/local/jdk/jre/lib/jce.jar
:/usr/local/jdk/jre/lib/charsets.jar:/usr/local/jdk/jre/lib/jfr.jar:/usr/local/jdk/jre/classes
LIBRARY-PATH /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
复制代码
$ trace #类名 #方法名
复制代码
对于执行耗时相对较长的方法,调用链路耗时属性会高亮显示方便排查
参数 -j 能够过滤jdk的函数 trace -j com.example.demo.controller index2 参数 #cost 能够按执行耗时毫秒ms过滤 trace -j com.example.demo.controller index2 ’#cost >10‘
当前方法执行数据观测,能观察到的范围为:返回值、抛出异常、入参
$ trace #类名 #方法名 "{params,target,returnObj,throwExp }"
复制代码
OGNL 表达式 {params,target,returnObj,throwExp }
复制代码
throwExp:异常 params :入参(数组),单个参数params【0】 returnObj:返回值
$ watch com.example.demo.controller index2 "{params,target,returnObj}" -x 5
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 81 ms.
ts=2019-03-29 14:24:14; [cost=1000.746582ms] result=@ArrayList[
@Object[][
@String[辛志富],
],
@controller[
],
@String[index2],
]
复制代码
当前方法被调用的路径,显示当前方法被那些方法调用
public static String uuidOne() {
return uuidTwo();
}
public static String uuidTwo() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
复制代码
$ stack com.example.demo.controller uuidTwo
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 58 ms.
ts=2019-03-29 14:38:19;thread_name=http-nio-8888-exec-5;id=13;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@525b461a
@com.example.demo.controller.uuidOne()
at com.example.demo.controller.index2(controller.java:31)
at sun.reflect.GeneratedMethodAccessor36.invoke(null:-1)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
复制代码
监控类、方法的调用进行监控,调用次数、成功次数、失败次数、平均响应时长、失败率等
$ monitor -c 4 com.example.demo.controller uuidTwo
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 56 ms.
timestamp class method total success fail avg-rt(ms) fail-rate
--------------------------------------------------------------------------------------------------------
2019-03-29 14:55:40 com.example.demo.controller uuidTwo 7 7 0 0.18 0.00% 复制代码
将JVM中全部的类加载器统计出来,树状展现
$ classloader #每种classloader加载类的个树
name numberOfInstances loadedCountTotal
org.springframework.boot.loader.LaunchedURLClassLoader 1 4463
com.taobao.arthas.agent.ArthasClassloader 2 3631
BootstrapClassLoader 1 2961
java.net.FactoryURLClassLoader 1 835
sun.misc.Launcher$AppClassLoader 1 46
sun.reflect.DelegatingClassLoader 41 41
sun.misc.Launcher$ExtClassLoader 1 25
Affect(row-cnt:7) cost in 7 ms.
$ classloader -t # 类加载器间的层级关系
+-BootstrapClassLoader
+-sun.misc.Launcher$ExtClassLoader@1959f618
+-com.taobao.arthas.agent.ArthasClassloader@5fc476c6
+-com.taobao.arthas.agent.ArthasClassloader@5017e14b
+-sun.misc.Launcher$AppClassLoader@5c647e05
+-java.net.FactoryURLClassLoader@4ad317f0
+-org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418
Affect(row-cnt:7) cost in 5 ms
复制代码
手动在代码中抛异常,「不停机不从新发包的状况下,修改线上代码」
启动服务也达到咱们预期异常
「替换代码的流程:」
$ jad --source--only com.example.demo.DemoApplication > /data/DemoApplication.java
复制代码
在这里插入图片描述
修改完之后须要将类从新加载到JVM
$ sc -d *DemoApplication | grep classLoader
classLoaderHash 20ad9418 #类加载器 编号
复制代码
$ mc -c 20ad9418 /data/DemoApplication.java -d /data
Memory compiler output:
/data/com/example/demo/DemoApplication.class
复制代码
上边编译后的.class文件地址
$ redefine /data/com/example/demo/DemoApplication.class
redefine success, size: 1
复制代码
「文件替换后咱们再次访问一下程序,发现异常没有了程序已是咱们修改正确后的,class文件替换成功」
这样咱们就用arthas现实了不停机、不发包替换了生产环境的Java代码,功能确实比较强大,本文只揭开了arthas强大功能的冰山一角,后续将出更详细的文章,方便你们一块儿学习。
arthas的总体功能虽然很强大,但命令行的输入方式让我头疼不已,岁数大了记忆力真的降低严重,并且做为一个贼 TM 懒的程序员,让我去记住如此多的命令和参数,简直是要了老命。又一次由于懒让我勤快起来,我决定作个arthas命令可视化平台。
「设计初衷」:设计这个平台的初衷很简单,就是让程序员们把更多的精力放在问题的排查上,而不是记那么多枯燥无趣的命令。自己我也不是一个愿意死记硬背的人,以为脑子里仍是应该多放一些有趣、有意义的东西。可能在用惯了命令行的大佬眼里,这个功能比较鸡肋,甚至有点多余,但毕竟像我这样平凡的人更多一些,天天还陷入在重复的工做当中,工做量能减一点就多轻松一点嘛。
目前平台还在持续的开发中,因为平台是本身在维护,开发进度并不客观,平时利用一些碎片时间开发,毕竟不能耽误工做丢了饭碗嘛。无论会不会有人用,我都会一直作下去。
「github地址」 : https://github.com/chengxy-nd...
「感兴趣的小伙伴能够私信我,让咱们一块儿打造这个有趣的东西吧!」
几百本!各种技术电子书相送,「嘘~」,「免费」 送给你们