Java 诊断工具 Arthas 常见命令(超详细实战教程)(一)

简介: Java 诊断工具 Arthas 常见命令(超详细实战教程)(一)

基本概念

云杏这么多微服务,当然需要一个诊断利器来排查问题。

Arthas 是阿里开源的挚爱工具,关注开发者开发者。查问题,独立重启跟踪 Java 代码;实时 JVM 在线监控。Arthas 6+,Linux/Mac/Windows,支持 JDK 支持 Java 模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

微信图片_20220908123804.png官方定义为应用诊断利器,从此Javagithub获得了29.4K个star。

可以用来查看线程值,内存,GC异常和运行时,查看入参返回/显示快速应用的提示,生成图等功能,帮助排查难问题。本文主要讲述常见的使用命令。

常见命令

启动arthas-demo(案例程序)

执行如下命令下载arthas-demo.jar,再用java -jar命令启动程序:

wget https://arthas.aliyun.com/arthas-demo.jar;
java -jar arthas-demo.jar

启动arthas-boot(诊断工具程序)

执行如下命令下载arthas-boot.jar,再用java -jar命令启动:

wget https://arthas.aliyun.com/arthas-boot.jar;
java -jar arthas-boot.jar

arthas是Arthas boot的程序,用户会启动-启动所有的Java进程后,可以选择诊断需要的进程。

微信图片_20220908123824.png

选择要诊断的Java程序,我这里输入1,再按回车键(回车)。

附上成功之后,会打印Arthas LOGO。

微信图片_20220908123907.png

输入帮助可以获取到阿尔萨斯相关命令帮助信息。

[arthas@1266]$ help
 NAME         DESCRIPTION                                                                             
 help         Display Arthas Help                                                                     
 auth         Authenticates the current session                                                       
 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 me 
              thod invocation                                                                         
 tt           Time Tunnel                                                                             
 jvm          Display the target JVM information                                                      
 memory       Display jvm memory info.                                                                
 perfcounter  Display the perf counter information.                                                   
 ognl         Execute ognl expression.                                                                
 mc           Memory compiler, compiles java files into bytecode and class files in memory.           
 redefine     Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)              
 retransform  Retransform classes. @see Instrumentation#retransformClasses(Class...)                  
 dashboard    Overview of target jvm's thread, memory, gc, vm, tomcat info.                           
 dump         Dump class byte array from JVM                                                          
 heapdump     Heap dump                                                                               
 options      View and change various Arthas options                                                  
 cls          Clear the screen                                                                        
 reset        Reset all the enhanced classes                                                          
 version      Display Arthas version                                                                  
 session      Display current session information                                                     
 sysprop      Display, and change the system properties.                                              
 sysenv       Display the system env.                                                                 
 vmoption     Display, and update the vm diagnostic options.                                          
 logger       Print logger info, and update the logger level                                          
 history      Display command history                                                                 
 cat          Concatenate and print files                                                             
 base64       Encode and decode using Base64 representation                                           
 echo         write arguments to the standard output                                                  
 pwd          Return working directory name                                                           
 mbean        Display the mbean information                                                           
 grep         grep command for pipes.                                                                 
 tee          tee command for pipes.                                                                  
 profiler     Async Profiler. https://github.com/jvm-profiling-tools/async-profiler                   
 vmtool       jvm tool                                                                                
 stop         Stop/Shutdown Arthas server and exit the console.

与linux同样规则的命令此处不再赘述。如:history,cat,echo,pwd,grep。

系统的实时数据面板仪表板命令

微信图片_20220908123925.png

仪表板可以命令查看当前系统的实时数据面板。可以查看到 CPU、内存、GC、运行环境等信息。

输入 q 或者 Ctrl+C 可以退出仪表板命令。

打印线程ID的栈线程

thread 1 命令会打印线程ID 1的栈。用thread 1 | grep 'main(' 查找到主类。

微信图片_20220908123955.png

查找JVM中已加载的类 sc/sm

可以通过sc命令来查找JVM里已经加载的打印类,通过-d参数,可以出类的具体信息,很方便查找类的加载问题。

[arthas@1266]$ sc -d *MathGame
 class-info        demo.MathGame                                                                                             
 code-source       /home/shell/arthas-demo.jar                                                                               
 name              demo.MathGame                                                                                             
 isInterface       false                                                                                                     
 isAnnotation      false                                                                                                     
 isEnum            false                                                                                                     
 isAnonymousClass  false                                                                                                     
 isArray           false                                                                                                     
 isLocalClass      false                                                                                                     
 isMemberClass     false                                                                                                     
 isPrimitive       false                                                                                                     
 isSynthetic       false                                                                                                     
 simple-name       MathGame                                                                                                  
 modifier          public                                                                                                    
 annotation                                                                                                                  
 interfaces                                                                                                                  
 super-class       +-java.lang.Object                                                                                        
 class-loader      +-sun.misc.Launcher$AppClassLoader@1b6d3586                                                               
                     +-sun.misc.Launcher$ExtClassLoader@107df6e5                                                             
 classLoaderHash   1b6d3586                                                                                                  
Affect(row-cnt:1) cost in 50 ms.

sc支持通配,比如搜索所有的StringUtils:

sc *StringUtils

查找UserController的ClassLoader

[arthas@1266]$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
 classLoaderHash   19469ea2

sm命令类似:查找类的具体函数。

sm java.math.RoundingMode

通过 -d 参数可以打印函数的具体属性:

sm -d java.math.RoundingMode

查找特定的函数,例如查找构造函数:

sm java.math.RoundingMode <init>

反编译代码 jad命令

jad demo.MathGame

微信图片_20220908124018.png

通过--source-only参数可以只打印出在反编译的源代码:

jad --source-only com.example.demo.arthas.user.UserController

动态执行代码 ognl 命令

在Arthas里,有一个ognl命令,可以动态执行代码。这个有点秀啊😯😯😯

调用静态函数
ognl '@java.lang.System@out.println("hello ognl")'
获取静态类的物理场

UserController类中的logger获取字段:

ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader @com.example.demo.arthas.user.UserController@logger

通过-x参数控制返回值的展开层数。

ognl --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader -x 2 @com.example.demo.arthas.user.UserController@logger
多行表达式,给与临时变量执行,返回一个列表
ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'

查看函数的参数/异常信息 watch 命令/返回值

watch demo.MathGame primeFactors returnObj

微信图片_20220908124057.png

查看JVM信息 sysprop sysenv jvm仪表板

系统道具
  • sysprop :打印所有的System Properties信息。
  • 指定密钥:sysprop user.dir
  • 通过grep过滤:sysprop | grep user
  • 设置新的价值:sysprop testKey testValue
系统环境

sysenv 命令可以类似地获取到环境变量。和 sysprop 命令。

虚拟机

jvm 命令会打印出 JVM 的各种详细信息。

仪表板

仪表板可以命令查看当前系统的实时数据面板。

重置类重置命令

通过reset命令可以还原类,将Arthas过码的类全部关闭,Arthas增强服务端时会重置所有过的类。Arthas在等命令的时候watch/trace,其实是应用的字节修改,插入的增强代码。显式执行重置命令,可以清除掉这些增强代码。

reset 指定类:

reset demo.MathGame

所有还原类:

reset

查看当前会话信息会话

微信图片_20220908124317.png

tee 命令

传统的 tee 命令类似用于读取标准输入的数据,并将其内容输出成文件。

tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

查看当前Arthas版本

[arthas@1710]$ version
3.6.2

退出阿尔萨斯

输入退出或退出命令可以退出阿尔萨斯当前会话。执行命令停止彻底退出阿尔萨斯。

📌PS:所有命令都可以通过 -h 参数查看帮助信息。

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

相关文章
|
10天前
|
IDE Oracle Java
java基础教程(1)-Java概述和相关名词解释
【4月更文挑战第1天】Java是1995年Sun Microsystems发布的高级编程语言,以其跨平台特性著名。它介于编译型和解释型语言之间,通过JVM实现“一次编写,到处运行”。Java有SE、EE和ME三个版本,分别针对标准、企业及嵌入式应用。JVM是Java虚拟机,确保代码在不同平台无需重编译。JRE是运行环境,而JDK包含开发工具。要安装Java开发环境,可从Oracle官网下载JDK,设置JAVA_HOME环境变量并添加到PATH。
|
10天前
|
Arthas 监控 Java
Java 诊断利器 Arthas使用
Java 诊断利器 Arthas使用
|
3天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
24 0
|
3天前
|
存储 Java
Java基础教程(7)-Java中的面向对象和类
【4月更文挑战第7天】Java是面向对象编程(OOP)语言,强调将事务抽象成对象。面向对象与面向过程的区别在于,前者通过对象间的交互解决问题,后者按步骤顺序执行。类是对象的模板,对象是类的实例。创建类使用`class`关键字,对象通过`new`运算符动态分配内存。方法包括构造函数和一般方法,构造函数用于对象初始化,一般方法处理逻辑。方法可以有0个或多个参数,可变参数用`类型...`定义。`this`关键字用于访问当前对象的属性。
|
5天前
|
Java 索引
Java基础教程(6)-Java中的流程控制语句
【4月更文挑战第6天】Java流程控制包括选择(if, switch)、重复(while, do-while, for)和跳转(break, continue, return)语句。选择语句根据条件执行不同路径,if和switch用于单条件和多条件分支。重复语句用于循环,如for循环的初始化、条件和迭代部分,以及while和do-while循环。跳转语句中,break用于立即退出循环,continue结束当前循环迭代,return则从方法中返回。此外,Java的for each循环简化了数组或集合的遍历,但不能控制遍历顺序或索引。
|
6天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十九)-java+ selenium自动化测试-元素定位大法之By css下卷(详细教程)
【4月更文挑战第11天】按计划今天宏哥继续讲解css的定位元素的方法。但是今天最后一种宏哥介绍给大家,了解就可以了,因为实际中很少用。
27 2
|
7天前
|
存储 Java 编译器
Java基础教程(4)-Java中的操作符
【4月更文挑战第4天】Java中的String是常用类,字符串是不可变对象,用双引号表示。String对象在编译期长度受限于65535,运行期不超过Int范围。字符串方法如length()、substring()、replace()、equals()等提供了多种操作。可变字符串可使用StringBuffer或StringBuilder。String对象通过字符串池优化内存,池中已有相同内容字符串则返回其引用。
|
7天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十八)-java+ selenium自动化测试-元素定位大法之By css中卷(详细教程)
【4月更文挑战第10天】本文主要介绍了CSS定位元素的几种方法,包括ID属性值定位、其他属性值定位和使用属性值的一部分定位。作者提供了示例代码,展示了如何使用这些方法在Java+Selenium自动化测试中定位网页元素。通过CSS选择器,可以更精确地找到页面上的特定元素,如输入框、按钮等,并进行相应的操作,如输入文本、点击等。文章还提供了实际运行代码后的控制台输出和浏览器动作的示例。
36 0
|
8天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十七)-java+ selenium自动化测试-元素定位大法之By css上卷(详细教程)
【4月更文挑战第9天】本文介绍了CSS定位方式的使用,包括它的优势和8种常用的定位方法。CSS定位相比XPath定位更快、更稳定。文章通过示例详细讲解了如何使用CSS定位元素,包括通过id、name、class name、tag name、link text、partial link text以及XPath进行定位。还提供了Java代码示例来演示如何在自动化测试中使用这些定位方法。
38 1
|
10天前
|
Java API 开发者
Java 8新特性之函数式编程实战
【4月更文挑战第9天】本文将深入探讨Java 8的新特性之一——函数式编程,通过实例演示如何运用Lambda表达式、Stream API等技术,提高代码的简洁性和执行效率。