不停机还能替换代码?6年的 Java程序员表示不可思议

新闻 开发工具
相信很多人都有这样一种感受,自己写的代码在开发、测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,而线上调试代码又很麻烦,让人头疼得很。

 [[324165]]

相信很多人都有这样一种感受,自己写的代码在开发、测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,而线上调试代码又很麻烦,让人头疼得很。不过, 阿里巴巴出了一款名叫Arthas的工具,可以在线分析诊断Java代码,让人眼前一亮。

Arthas 是什么?

Arthas(阿尔萨斯) 是阿里开源的一个Java在线分析诊断工具。

Arthas 能解决啥问题?

在日常开发上线过程中,我们多多少少都会遇到下边这些问题,苦于无法在线调试,「只能通过老鸟的经验来硬分析bug,效率上不去还总开口问别人答疑解惑,多少有些不好意思」

  • 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  • 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  • 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  • 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  • 是否有一个全局视角来查看系统的运行状况?
  • 有什么办法可以监控到JVM的实时运行状态?
  • 线上代码有错误,不想重新发布?那能不能改class文件替换一下?

Arthas两种安装、启动方式

1、jar包启动

  1. wget https://alibaba.github.io/arthas/arthas-boot.jar 
  2.  
  3. java -jar arthas-boot.jar --target-ip 0.0.0.0 

首先想用arthas调试项目,服务器必须要有运行着的Java服务,demo-0.0.1-SNAPSHOT.jar就是我启动的测试项目,启动arthas后它会自动检测本地所有的Java服务列出来,我们只需按照序号输入想要调试的项目即可,选1进入对应进程的arthas交互平台

  1. [root@iz2zehzeir87zi8q99krk1z data]# java -jar arthas-boot.jar --target-ip 172.17.72.201 
  2. [INFO] arthas-boot version: 3.1.0 
  3. [INFO] Found existing java process, please choose one and hit RETURN. 
  4. * [1]: 28679 demo-0.0.1-SNAPSHOT.jar 

2、在线安装

  1. curl -L https://alibaba.github.io/arthas/install.sh | sh 

执行上面的命令会在所在的文件中生成as.sh执行文件

不停机还能替换代码?6年的 Java程序员表示不可思议

启动arthas

  1. ./as.sh PID #进程id 指定JAVA进程id 
  2. ./as.sh -h #h来获取更多参数信息 
  3.  
  4. 3、远程连接: 

「要想使用arthas服务的 web console必须对外暴露本机ip」

  1. java -jar arthas-boot.jar --target-ip 172.17.72.201 
  2. java -jar arthas-boot.jar --telnet-port 9999 --http-port -1 

  1. ./as.sh --target-ip 0.0.0.0 
  2. ./as.sh --telnet-port 9999 --http-port -1 

访问arthas控制台也有两种方法

「(1)、web console 界面」

「重点说明」:--target-ip 的ip 一定要是arthas所在机器对外暴露的ip,「但如果用的是阿里云机器必须要使用私有ip启动arthas服务,但访问必须是公网IP」

不停机还能替换代码?6年的 Java程序员表示不可思议

「(2)、telnet方式」

  1. telnet 10.0.2.5 8563 
不停机还能替换代码?6年的 Java程序员表示不可思议

访问 http://59.110.218.9:8563/ ,进入交互平台

不停机还能替换代码?6年的 Java程序员表示不可思议

Arthas 命令使用

1、Dashboard 命令

查看当前系统的实时数据面板,例如:服务器thread信息、内存memory、GC回收等情况

不停机还能替换代码?6年的 Java程序员表示不可思议

2、Thread(线程监控)

  1. $ thread -n 3 
  2. "as-command-execute-daemon" Id=57 cpuUsage=72% RUNNABLE 
  3. at sun.management.ThreadImpl.dumpThreads0(Native Method) 
  4. at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:448
  5. at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133
  6. at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79
  7. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82
  8. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18
  9. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111
  10. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108
  11. at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:370
  12. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149
  13. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624
  14. at java.lang.Thread.run(Thread.java:748
  15.  
  16. Number of locked synchronizers = 1  
  17. - java.util.concurrent.ThreadPoolExecutor$Worker@a2f70c7 

「可以看到这个线程是被synchroned关键字导致的阻塞」 ,目前只支持找出synchronized关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持。

  1. Number of locked synchronizers = 1 
  2. - java.util.concurrent.ThreadPoolExecutor$Worker@a2f70c7 
  3. thread -n 3 #当前最忙的前N个线程 
  4. thread -b, ##找出当前阻塞其他线程的线程 
  5. thread -n 3 -i 1000 #间隔一定时间后展示 

「重点学习」:thread -b, ##「找出当前阻塞其他线程的线程」

3、JVM (jvm实时运行状态,内存使用情况等)

  1. $ jvm 
  2. RUNTIME  
  3. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  4. MACHINE-NAME 28679@iz2zehzeir87zi8q99krk1z  
  5. JVM-START-TIME 2019-03-28 17:32:16  
  6. MANAGEMENT-SPEC-VERSION 1.2  
  7. SPEC-NAME Java Virtual Machine Specification  
  8. SPEC-VENDOR Oracle Corporation  
  9. SPEC-VERSION 1.8  
  10. VM-NAME Java HotSpot(TM) 64-Bit Server VM  
  11. VM-VENDOR Oracle Corporation  
  12. VM-VERSION 25.191-b12  
  13. INPUT-ARGUMENTS []  
  14. CLASS-PATH demo-0.0.1-SNAPSHOT.jar  
  15. 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  
  16. :/usr/local/jdk/jre/lib/charsets.jar:/usr/local/jdk/jre/lib/jfr.jar:/usr/local/jdk/jre/classes  
  17. LIBRARY-PATH /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib  

4、trace (当前方法内部调用路径,路径上每个节点的耗时)

  1. $ trace #类名 #方法名  
不停机还能替换代码?6年的 Java程序员表示不可思议

对于执行耗时相对较长的方法,调用链路耗时属性会高亮显示方便排查

不停机还能替换代码?6年的 Java程序员表示不可思议

参数 -j 可以过滤jdk的函数 trace -j com.example.demo.controller index2 参数 #cost 可以按执行耗时毫秒ms过滤 trace -j com.example.demo.controller index2 ’#cost >10‘

5、watch

当前方法执行数据观测,能观察到的范围为:返回值、抛出异常、入参

  1. $ trace #类名 #方法名 "{params,target,returnObj,throwExp }"  
  1. OGNL 表达式 {params,target,returnObj,throwExp } 

throwExp:异常 params :入参(数组),单个参数params【0】 returnObj:返回值

  1. $ watch com.example.demo.controller index2 "{params,target,returnObj}" -x 5 
  2. Press Q or Ctrl+C to abort. 
  3. Affect(class-cnt:1 , method-cnt:1) cost in 81 ms. 
  4. ts=2019-03-29 14:24:14; [cost=1000.746582ms] result=@ArrayList
  5. @Object[][ 
  6. @String[辛志富], 
  7. ], 
  8. @controller
  9. ], 
  10. @String[index2], 

6、stack

当前方法被调用的路径,显示当前方法被那些方法调用

  1. public static String uuidOne() { 
  2. return uuidTwo(); 
  3. public static String uuidTwo() { 
  4. return UUID.randomUUID().toString().replaceAll("-"""); 
  1. $ stack com.example.demo.controller uuidTwo 
  2. Press Q or Ctrl+C to abort. 
  3. Affect(class-cnt:1 , method-cnt:1) cost in 58 ms. 
  4. 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 
  5. @com.example.demo.controller.uuidOne() 
  6. at com.example.demo.controller.index2(controller.java:31
  7. at sun.reflect.GeneratedMethodAccessor36.invoke(null:-1
  8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43

7、monitor 命令

监控类、方法的调用进行监控,调用次数、成功次数、失败次数、平均响应时长、失败率等

  1. $ monitor -c 4 com.example.demo.controller uuidTwo 
  2. Press Q or Ctrl+C to abort. 
  3. Affect(class-cnt:1 , method-cnt:1) cost in 56 ms. 
  4. timestamp class method total success fail avg-rt(ms) fail-rate  
  5. --------------------------------------------------------------------------------------------------------  
  6. 2019-03-29 14:55:40 com.example.demo.controller uuidTwo 7 7 0 0.18 0.00%  

8、classloader 命令

将JVM中所有的类加载器统计出来,树状展示

  1. $ classloader #每种classloader加载类的个树 
  2. name numberOfInstances loadedCountTotal  
  3. org.springframework.boot.loader.LaunchedURLClassLoader 1 4463  
  4. com.taobao.arthas.agent.ArthasClassloader 2 3631  
  5. BootstrapClassLoader 1 2961  
  6. java.net.FactoryURLClassLoader 1 835  
  7. sun.misc.Launcher$AppClassLoader 1 46  
  8. sun.reflect.DelegatingClassLoader 41 41  
  9. sun.misc.Launcher$ExtClassLoader 1 25  
  10. Affect(row-cnt:7) cost in 7 ms. 
  11. $ classloader -t # 类加载器间的层级关系 
  12. +-BootstrapClassLoader  
  13. +-sun.misc.Launcher$ExtClassLoader@1959f618  
  14. +-com.taobao.arthas.agent.ArthasClassloader@5fc476c6  
  15. +-com.taobao.arthas.agent.ArthasClassloader@5017e14b  
  16. +-sun.misc.Launcher$AppClassLoader@5c647e05  
  17. +-java.net.FactoryURLClassLoader@4ad317f0  
  18. +-org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418  
  19. Affect(row-cnt:7) cost in 5 ms 

线上代码热更新(动态修改上线项目代码)

手动在代码中抛异常,「不停机不重新发包的情况下,修改线上代码」

不停机还能替换代码?6年的 Java程序员表示不可思议

启动服务也达到我们预期异常

「替换代码的流程:」

1、jad命令 将需要更改的文件先进行反编译,保存下来 ,编译器修改

  1. $ jad --source--only com.example.demo.DemoApplication > /data/DemoApplication.java 
不停机还能替换代码?6年的 Java程序员表示不可思议

在这里插入图片描述

修改完以后需要将类重新加载到JVM

2、SC命令 查找当前类是哪个classLoader加载的

  1. $ sc -d *DemoApplication | grep classLoader 
  2. classLoaderHash 20ad9418 #类加载器 编号    

3、MC命令 用指定的classloader重新将类在内存中编译

  1. $ mc -c 20ad9418 /data/DemoApplication.java -d /data  
  2. Memory compiler output: 
  3. /data/com/example/demo/DemoApplication.class 

4、redefine命令 将编译后的类加载到JVM

上边编译后的.class文件地址

  1. $ redefine /data/com/example/demo/DemoApplication.class  
  2. redefine success, size: 1 

「文件替换后我们再次访问一下程序,发现异常没有了程序已经是我们修改正确后的,class文件替换成功」

不停机还能替换代码?6年的 Java程序员表示不可思议

总结

这样我们就用arthas现实了不停机、不发包替换了生产环境的Java代码,功能确实比较强大,本文只揭开了arthas强大功能的冰山一角,后续将出更详细的文章,方便大家一起学习。

越懒越勤快

arthas的整体功能虽然很强大,但命令行的输入方式让我头疼不已,岁数大了记忆力真的下降严重,而且作为一个贼 TM 懒的程序员,让我去记住如此多的命令和参数,简直是要了老命。又一次因为懒让我勤快起来,我决定做个arthas命令可视化平台。

不停机还能替换代码?6年的 Java程序员表示不可思议

「设计初衷」:设计这个平台的初衷很简单,就是让程序员们把更多的精力放在问题的排查上,而不是记那么多枯燥无趣的命令。本身我也不是一个愿意死记硬背的人,觉得脑子里还是应该多放一些有趣、有意义的东西。可能在用惯了命令行的大佬眼里,这个功能比较鸡肋,甚至有点多余,但毕竟像我这样平凡的人更多一些,每天还陷入在重复的工作当中,工作量能减一点就多轻松一点嘛。

责任编辑:张燕妮 来源: 今日头条
相关推荐

2011-02-23 08:50:22

C#.NETdynamic

2017-03-21 08:52:20

神经网络声誉

2013-10-10 13:07:25

方物

2010-07-15 16:21:03

不可思议的服务器

2021-11-10 06:38:01

Python链式操作

2014-01-14 10:33:42

开源硬件开源

2020-07-02 15:40:11

Spring BootJar包Java

2023-04-06 09:44:00

ChatGPT行业质量

2011-07-18 13:35:14

HTML 5

2022-01-24 15:57:34

Python返回功能代码

2013-07-31 15:06:58

未来的WebWebGLWeb

2014-07-26 22:18:51

2017-11-08 14:07:45

数据库MySQL慢查分析

2012-02-13 11:01:27

N9Android 4.0

2016-07-06 11:56:52

思科汉堡光纤骨干网

2012-05-16 17:28:32

智能手机

2024-04-07 00:00:00

亿级数据ES

2017-12-19 14:15:27

程序员愿望加班

2017-12-20 15:42:13

2018-07-10 09:18:36

人工智能智能出行大数据
点赞
收藏

51CTO技术栈公众号