更新时间:2019年07月26日 11时16分25秒 来源:黑马程序员论坛
Arthas 是 阿里巴巴最近开源出来的一个针对 java 的工具,主要是针对 java 的问题进行诊断! 一、概述 这个工具可以协助你做下面这些事情: 这个类是从哪个 jar 包加载而来的? 为什么会报各种类相关的 Exception? 线上遇到问题无法 debug 好蛋疼,难道只能反复通过增加 System.out 或通过加日志再重新发布吗? 线上的代码为什么没有执行到这里?是由于代码没有 commit?还是搞错了分支? 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现! 是否有一个全局视角来查看系统的运行状况? 有什么办法可以监控到JVM的实时运行状态? 二、安装方式 1.1 window 安装方式 下载地址:http://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22com.taobao.arthas%22%20AND%20a%3A%22arthas-packaging%22 下载完成后,解压缩,如下图所示:: ![]() 在Download栏下载最新的 bin.zip 包,解压后在bin目录有 as.bat。此脚本暂时只接受一个参数 pid,即只能诊断本机上的 Java 进程。 启动命令为: as.bat <pid> 注:我在 window 10 上面启动的时候遇到如下问题, D:\download\arthas-packaging-3.0.4-bin>telnet 'telnet' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 解决办法为:“控制面板” ——> “启动或关闭Windows功能” ——> 勾选 “Telnet 功能” ![]() 1.2 Linux 安装方式 安装Arthas: curl -L https://alibaba.github.io/arthas/install.sh | sh 启动Arthas: ./as.sh 成功启动后,会看到如下界面。 ![]() 三、常用命令 3.1 基础命令 help——查看命令帮助信息 cls——清空当前屏幕区域 session——查看当前会话的信息 reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类 version——输出当前目标 Java 进程所加载的 Arthas 版本号 quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响 shutdown——关闭 Arthas 服务端,所有 Arthas 客户端全部退出 keymap——Arthas快捷键列表及自定义快捷键 jvm相关 dashboard——当前系统的实时数据面板 thread——查看当前 JVM 的线程堆栈信息 jvm——查看当前 JVM 的信息 sysprop——查看和修改JVM的系统属性 New! getstatic——查看类的静态属性 class/classloader相关 sc——查看JVM已加载的类信息 sm——查看已加载类的方法信息 dump——dump 已加载类的 byte code 到特定目录 redefine——加载外部的.class文件,redefine到JVM里 jad——反编译指定已加载类的源码 classloader——查看classloader的继承树,urls,类加载信息,使用classloader去getResource monitor/watch/trace相关 请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行 shutdown 或将增强过的类执行 reset 命令。 monitor——方法执行监控 watch——方法执行数据观测 trace——方法内部调用路径,并输出方法路径上的每个节点上耗时 stack——输出当前方法被调用的调用路径 tt——方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测 options options——查看或设置Arthas全局开关 管道 Arthas支持使用管道对上述命令的结果进行进一步的处理,如sm org.apache.log4j.Logger | grep grep——搜索满足条件的结果 plaintext——将命令的结果去除颜色 wc——按行统计输出结果 Web Console 通过websocket连接Arthas。 Web Console 其他特性 异步命令支持 执行结果存日志 批处理的支持 ognl表达式的用法说明 3.2 使用示例 首先,在窗口中,输入 help 查看一下所有提供的可用命令(他的通信本质是通过 telnet 协议来通信的),如下图: Attach success. Connecting to arthas server... current timestamp is 1537266148 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.4 pid: 25206 timestamp: 1537266148841 $ help NAME DESCRIPTION help Display Arthas Help keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version shutdown Shut down Arthas server and exit the console session Display current session information sysprop Display, and change the system properties. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) $ 这里主要说一下 watch ,这个命令对变量进行数据监测。 ========================================================================== 首先贴上我的测试代码: package com.oct.tail; import java.util.UUID; /** * @Author Ryan * @Date 2018/9/18 9:58 * @desc */ public class OtherTestCase { /** * * @return */ public static String uuid(){ return UUID.randomUUID().toString().replaceAll("-", ""); } public static void main(String[] args) { while(true){ System.out.println("uuid = " + uuid()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } 如下我做一个示例 ( 本人是基于 Window 10 , JDK 8 环境, Linux 雷同),(对于 watch 命令我假装不知道如何使用,立即输入 watch help 来看看会有什么玩意儿): ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.4 pid: 11924 timestamp: 1537326702039 $ watch -help USAGE: watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class- pattern method-pattern express [condition-express] SUMMARY: Display the input/output parameter, return object, and thrown exception of specif ied method invocation The express may be one of the following expression (evaluated dynamically): target : the object clazz : the object's class method : the constructor or method params[0..n] : the parameters of method returnObj : the returned object of method throwExp : the throw exception of method isReturn : the method ended by return isThrow : the method ended by throwing exception #cost : the execution time in ms of method invocation Examples: watch -Eb org\.apache\.commons\.lang\.StringUtils isBlank params[0] watch -b org.apache.commons.lang.StringUtils isBlank params[0] watch -f org.apache.commons.lang.StringUtils isBlank returnObj watch -bf *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] params[0].length==1 watch *StringUtils isBlank '#cost>100' WIKI: https://alibaba.github.io/arthas/watch OPTIONS: -b, --before Watch before invocation -e, --exception Watch after throw exception -x, --expand <value> Expand level of object (1 by default) -f, --finish Watch after invocation, enable by default -h, --help this help -n, --limits <value> Threshold of execution times -E, --regex Enable regular expression to match (wildcard matching b y default) -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1 024 by default) -s, --success Watch after successful invocation <class-pattern> The full qualified class name you want to watch <method-pattern> The method name you want to watch <express> the content you want to watch, written by ognl. Examples: params[0] 'params[0]+params[1]' returnObj throwExp target clazz method <condition-express> Conditional expression in ognl style, for example: TRUE : 1==1 TRUE : true FALSE : false TRUE : 'params.length>=0' FALSE : 1==2 $ 在这里,我们针对方法 uuid() 返回值进行监测。监测结果如下: $ $ $ watch -f com.oct.tail.OtherTestCase uuid returnObj Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 18 ms. ts=2018-09-19 11:13:48;result=@String[26c80eb505664dbcb14f8d810fb4811c] ts=2018-09-19 11:13:49;result=@String[fc03c43864f94372b646ce6253d90646] ts=2018-09-19 11:13:50;result=@String[55ff41e0d66347c2bc75ab8ff4ffda4e] ts=2018-09-19 11:13:51;result=@String[c504388c0aa74458a41a1b3a77c3d536] ts=2018-09-19 11:13:52;result=@String[18d59c09ffde4c7aab15feb88b3e433f] ts=2018-09-19 11:13:53;result=@String[c19dd8c1e5f8442696c8f886e81e74d5] ts=2018-09-19 11:13:54;result=@String[d37a74aa502f4897aa1ed84dc69b83d8] ts=2018-09-19 11:13:55;result=@String[cc11753b6f424c1e9a6a1ab36f334349] ts=2018-09-19 11:13:56;result=@String[75a9b3c0bed4426d9363168912f16d74] ts=2018-09-19 11:13:57;result=@String[f13022118e5a4115800a6eacc480e6a8] 简直方便的死去活来!太感动了。 总结,这个工具不止这里介绍的功能,还有其他的用法及命令,详细信息, 请移步官网(https://alibaba.github.io/arthas/sm.html) 转自简书 [color=rgba(0, 0, 0, 0.85)]Ryan-瑞恩[color=rgba(0, 0, 0, 0.87)] |
推荐了解热门学科
java培训 | Python人工智能 | Web前端培训 | PHP培训 |
区块链培训 | 影视制作培训 | C++培训 | 产品经理培训 |
UI设计培训 | 新媒体培训 | 产品经理培训 | Linux运维 |
大数据培训 | 智能机器人软件开发 |
传智播客是一家致力于培养高素质软件开发人才的科技公司,“黑马程序员”是传智播客旗下高端IT教育品牌。自“黑马程序员”成立以来,教学研发团队一直致力于打造精品课程资源,不断在产、学、研3个层面创新自己的执教理念与教学方针,并集中“黑马程序员”的优势力量,针对性地出版了计算机系列教材50多册,制作教学视频数+套,发表各类技术文章数百篇。
传智播客从未停止思考
传智播客副总裁毕向东在2019IT培训行业变革大会提到,“传智播客意识到企业的用人需求已经从初级程序员升级到中高级程序员,具备多领域、多行业项目经验的人才成为企业用人的首选。”
中级程序员和初级程序员的差别在哪里?
项目经验。毕向东表示,“中级程序员和初级程序员最大的差别在于中级程序员比初级程序员多了三四年的工作经验,从而多出了更多的项目经验。“为此,传智播客研究院引进曾在知名IT企业如阿里、IBM就职的高级技术专家,集中研发面向中高级程序员的课程,用以满足企业用人需求,尽快补全IT行业所需的人才缺口。
何为中高级程序员课程?
传智播客进行了定义。中高级程序员课程,是在当前主流的初级程序员课程的基础上,增加多领域多行业的含金量项目,从技术的广度和深度上进行拓展。“我们希望用5年的时间,打造上百个高含金量的项目,覆盖主流的32个行业。”传智播客课程研发总监于洋表示。
黑马程序员热门视频教程【点击播放】
Python入门教程完整版(懂中文就能学会) | 零起点打开Java世界的大门 |
C++| 匠心之作 从0到1入门学编程 | PHP|零基础入门开发者编程核心技术 |
Web前端入门教程_Web前端html+css+JavaScript | 软件测试入门到精通 |