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 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

相关文章
|
2月前
|
人工智能 缓存 监控
使用LangChain4j构建Java AI智能体:让大模型学会使用工具
AI智能体是大模型技术的重要演进方向,它使模型能够主动使用工具、与环境交互,以完成复杂任务。本文详细介绍如何在Java应用中,借助LangChain4j框架构建一个具备工具使用能力的AI智能体。我们将创建一个能够进行数学计算和实时信息查询的智能体,涵盖工具定义、智能体组装、记忆管理以及Spring Boot集成等关键步骤,并展示如何通过简单的对话界面与智能体交互。
779 1
|
1月前
|
人工智能 监控 Java
Java与AI智能体:构建自主决策与工具调用的智能系统
随着AI智能体技术的快速发展,构建能够自主理解任务、制定计划并执行复杂操作的智能系统已成为新的技术前沿。本文深入探讨如何在Java生态中构建具备工具调用、记忆管理和自主决策能力的AI智能体系统。我们将完整展示从智能体架构设计、工具生态系统、记忆机制到多智能体协作的全流程,为Java开发者提供构建下一代自主智能系统的完整技术方案。
306 4
|
2月前
|
人工智能 Java API
Java AI智能体实战:使用LangChain4j构建能使用工具的AI助手
随着AI技术的发展,AI智能体(Agent)能够通过使用工具来执行复杂任务,从而大幅扩展其能力边界。本文介绍如何在Java中使用LangChain4j框架构建一个能够使用外部工具的AI智能体。我们将通过一个具体示例——一个能获取天气信息和执行数学计算的AI助手,详细讲解如何定义工具、创建智能体并处理执行流程。本文包含完整的代码示例和架构说明,帮助Java开发者快速上手AI智能体的开发。
846 8
|
6月前
|
机器学习/深度学习 消息中间件 存储
【高薪程序员必看】万字长文拆解Java并发编程!(9-2):并发工具-线程池
🌟 ​大家好,我是摘星!​ 🌟今天为大家带来的是并发编程中的强力并发工具-线程池,废话不多说让我们直接开始。
227 0
|
5月前
|
Java 数据安全/隐私保护 计算机视觉
银行转账虚拟生成器app,银行卡转账截图制作软件,java实现截图生成工具【仅供装逼娱乐用途】
本内容提供Java生成自定义图片的示例代码,涵盖基础图像创建、文本添加及保存功能,适合学习2D图形编程。包括教学示例图片生成、文本图层处理和数字水印技术实现方案。
|
5月前
|
安全 Java 编译器
JD-GUI,java反编译工具及原理: JavaDecompiler一个Java反编译器
Java Decompiler (JD-GUI) 是一款由 Pavel Kouznetsov 开发的图形化 Java 反编译工具,支持 Windows、Linux 和 Mac Os。它能将 `.class` 文件反编译为 Java 源代码,支持多文件标签浏览、高亮显示,并兼容 Java 5 及以上版本。JD-GUI 支持对整个 Jar 文件进行反编译,可跳转源码,适用于多种 JDK 和编译器。其原理基于将字节码转换为抽象语法树 (AST),再通过反编译生成代码。尽管程序可能带来安全风险,但可通过代码混淆降低可读性。最新版修复了多项识别错误并优化了内存管理。
2698 1
|
6月前
|
Arthas 监控 Java
Arthas vmoption(查看和修改 JVM里诊断相关的option)
Arthas vmoption(查看和修改 JVM里诊断相关的option)
118 16
|
6月前
|
Arthas 存储 监控
Arthas heapdump(dump java heap, 类似 jmap 命令的 heap dump 功能)
Arthas heapdump(dump java heap, 类似 jmap 命令的 heap dump 功能)
379 8
|
5月前
|
Java 数据安全/隐私保护
银行转账虚拟生成器app,银行卡转账截图制作软件,java实现截图生成工具【仅供装逼娱乐用途】
本项目提供了一套基于Java的图片处理教学方案,包含自定义图片生成、图像水印添加及合法电子凭证生成技术示例。
|
Arthas 监控 Java
Java 诊断利器 Arthas使用
Java 诊断利器 Arthas使用
3277 0