Arthas 是什么
Arthas是一款阿里巴巴开源的 Java 线上诊断工具。Arthas支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab
自动补全功能,进一步方便进行问题的定位和诊断。
官网地址**:**https://arthas.aliyun.com/doc/
github地址**:**https://github.com/alibaba/arthas
Arthas能干什么
大家有没有在生产 或者测试环境中遇到下列问题。
- 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
- 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态?
- 怎么快速定位应用的热点,生成火焰图?
- 怎样直接从JVM内查找某个类的实例?
- 怎样定位慢调用方法。
在官网上说Arthas能帮忙处理以上问题,并没有写明怎么处理,下面我们通过介绍几个命令来介绍下,以上问题通过Arthas怎么去定位处理。
Arthas快速入门
下载Arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
运行Arthas
java -jar arthas-boot.jar
运行该命令后 会显示java 进程,选择需要排查的进程。
我们运行以下测试代码来看看arthas的使用
package com.example.arthas;
public class ArthasTest {
public static void main(String[] args) throws InterruptedException {
while(true){
put("test");
}
}
private static String put(String name) throws InterruptedException {
Thread.sleep(1000L);
System.out.println(name);
return "hello "+name;
}
}
选择我们测试代码运行的进程[3]22756
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 1200 org.jetbrains.jps.cmdline.Launcher
[2]: 9120
[3]: 22756 com.example.arthas.ArthasTest
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
通过sc命令查找类是哪个jar包加载的
[arthas@22756]$ sc -d com.example.arthas.ArthasTest
class-info com.example.arthas.ArthasTest
code-source /D:/ideaproject/jsoupservice/target/classes/
name com.example.arthas.ArthasTest
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name ArthasTest
modifier public
annotation
interfaces
super-class +-java.lang.Object
class-loader +-sun.misc.Launcher$AppClassLoader@18b4aac2
+-sun.misc.Launcher$ExtClassLoader@593cfc78
classLoaderHash 18b4aac2
Affect(row-cnt:1) cost in 77 ms.
其中code-source 信息表明类加载路径,如果是jar包中加载出来的 会显示jar包信息,如下
code-source file:/home/trade/u3/u3-mnager/u-mnager-1.jar!/BOOT-INF/lib/uc-do-1.0-SNAPSHOT.jar!/
对于依赖冲突这类问题,我们可以通过该命令来进行排查。
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
我们通过jad命令来反编译查看源码信息
[arthas@22756]$ jad com.example.arthas.ArthasTest
ClassLoader:
+-sun.misc.Launcher$AppClassLoader@18b4aac2
+-sun.misc.Launcher$ExtClassLoader@593cfc78
Location:
/D:/ideaproject/jsoupservice/target/classes/
/*
* Decompiled with CFR.
*/
package com.example.arthas;
public class ArthasTest {
public static void main(String[] args) throws InterruptedException {
while (true) {
/* 7*/ ArthasTest.put("test");
}
}