【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
Centos安装Arthas的安装与启动
curl -L https://arthas.gitee.io/install.sh | sh
启动as前要先配置好JAVA_HOME
JAVA_HOME=/usr/java/jdk1.8.0_131
export JAVA_HOME
找到as的安装目录执行
#target-ip 为指定绑定的IP,如果不指定IP,arthas只listen 127.0.0.1,所以如果想从远程连接,则可以使用 --target-ip参数指定listen的IP
[root@i-7ovp6mxk opt]# ./as.sh --target-ip 192.192.17.14
如果指定了IP就可以在其它机器上执行以下命令就可以连接上arthas
gaopandeMacBook-Pro:~ gaopan$ telnet 192.192.18.67 3658
Trying 192.192.18.67...
Connected to 192.192.18.67.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.7
pid 3196
time 2019-12-11 16:29:19
[arthas@3196]$
Arthas的使用(只列出了一些常用的命令)
- sc -查看JVM已加载的类信息,“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息
class-pattern支持全限定名,如com.taobao.test.AAA,也支持com/taobao/test/AAA这样的格式,这样,我们从异常堆栈里面把类名拷贝过来的时候,不需要在手动把/替换为.啦。sc 默认开启了子类匹配功能,也就是说所有当前类的子类也会被搜索出来,想要精确的匹配,请打开options disable-sub-class true开关
示例:
#查找对应的类
[arthas@3196]$ sc com.chinayanghe.smp.common.filter.ELKLogFilter
com.chinayanghe.smp.common.filter.ELKLogFilter
Affect(row-cnt:1) cost in 219 ms.
[arthas@3196]$ sc com/chinayanghe/smp/common/filter/ELKLogFilter
com.chinayanghe.smp.common.filter.ELKLogFilter
Affect(row-cnt:1) cost in 40 ms.
#查看类的详细信息 -d参数为查看类的详细信息
[arthas@3196]$ sc -d com.chinayanghe.smp.common.filter.ELKLogFilter
class-info com.chinayanghe.smp.common.filter.ELKLogFilter
code-source file:/opt/war/micro-service-smp.jar!/BOOT-INF/classes!/
name com.chinayanghe.smp.common.filter.ELKLogFilter
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name ELKLogFilter
modifier public
annotation
interfaces
super-class +-ch.qos.logback.core.filter.Filter
+-ch.qos.logback.core.spi.ContextAwareBase
+-java.lang.Object
class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@1be6f5c3
+-sun.misc.Launcher$AppClassLoader@55f96302
+-sun.misc.Launcher$ExtClassLoader@73d06910
classLoaderHash 1be6f5c3
Affect(row-cnt:1) cost in 49 ms.
#查看类的详细信息和字段信息 -f查看类的字段信息
[arthas@3196]$ sc -d -f com.chinayanghe.smp.common.filter.ELKLogFilter
class-info com.chinayanghe.smp.common.filter.ELKLogFilter
code-source file:/opt/war/micro-service-smp.jar!/BOOT-INF/classes!/
name com.chinayanghe.smp.common.filter.ELKLogFilter
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name ELKLogFilter
modifier public
annotation
interfaces
super-class +-ch.qos.logback.core.filter.Filter
+-ch.qos.logback.core.spi.ContextAwareBase
+-java.lang.Object
class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@1be6f5c3
+-sun.misc.Launcher$AppClassLoader@55f96302
+-sun.misc.Launcher$ExtClassLoader@73d06910
classLoaderHash 1be6f5c3
fields name ELKLOGGERNAME
type java.lang.String
modifier final,public,static
value com.chinayanghe.smp.common.handler.ElkLoggerHandler
Affect(row-cnt:1) cost in 35 ms.
- sm -查看已加载类的方法信息.“Search-Method” 的简写,这个命令能搜索出所有已经加载了 Class 信息的方法信息。
#查看类的方法列表
[arthas@3196]$ sm com.chinayanghe.smp.common.filter.ELKLogFilter
com.chinayanghe.smp.common.filter.ELKLogFilter <init>()V
com.chinayanghe.smp.common.filter.ELKLogFilter decide(Ljava/lang/Object;)Lch/qos/logback/core/spi/FilterReply;
com.chinayanghe.smp.common.filter.ELKLogFilter decide(Lch/qos/logback/classic/spi/ILoggingEvent;)Lch/qos/logback/core/spi/FilterReply;
Affect(row-cnt:3) cost in 35 ms
#查看某个方法的详细信息
[arthas@3196]$ sm -d com.chinayanghe.smp.common.filter.ELKLogFilter decide
declaring-class com.chinayanghe.smp.common.filter.ELKLogFilter
method-name decide
modifier public,volatile
annotation
parameters java.lang.Object
return ch.qos.logback.core.spi.FilterReply
exceptions
classLoaderHash 1be6f5c3
declaring-class com.chinayanghe.smp.common.filter.ELKLogFilter
method-name decide
modifier public
annotation
parameters ch.qos.logback.classic.spi.ILoggingEvent
return ch.qos.logback.core.spi.FilterReply
exceptions
classLoaderHash 1be6f5c3
- jad -反编译指定已加载类的源码
[arthas@3196]$ jad com.chinayanghe.smp.common.filter.ELKLogFilter
ClassLoader:
+-org.springframework.boot.loader.LaunchedURLClassLoader@1be6f5c3
+-sun.misc.Launcher$AppClassLoader@55f96302
+-sun.misc.Launcher$ExtClassLoader@73d06910
Location:
file:/opt/war/micro-service-smp.jar!/BOOT-INF/classes!/
/*
* Decompiled with CFR.
*
* Could not load the following classes:
* ch.qos.logback.classic.spi.ILoggingEvent
* ch.qos.logback.core.filter.Filter
* ch.qos.logback.core.spi.FilterReply
* com.chinayanghe.smp.common.handler.ElkLoggerHandler
*/
package com.chinayanghe.smp.common.filter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.chinayanghe.smp.common.handler.ElkLoggerHandler;
public class ELKLogFilter
extends Filter<ILoggingEvent> {
public static final String ELKLOGGERNAME = ElkLoggerHandler.class.getName();
public FilterReply decide(ILoggingEvent event) {
return FilterReply.DENY;
}
}
Affect(row-cnt:1) cost in 1127 ms.
- redefine - 加载外部的
.class
文件,redefine jvm已加载的类。redefine后的原来的类不能恢复,redefine有可能失败(比如增加了新的field或新加了method)
[arthas@3196]$ redefine /opt/ResourceUpdateIntercept.class
redefine success, size: 1
- watch - 方法执行数据观测,让你能方便的观察到指定方法的调用情况。能观察到的范围为:
返回值
、抛出异常
、入参
,通过编写 OGNL 表达式进行对应变量的查看。
#观察方法的入参与返回值 -x表示遍历深度,可以调整来打印具体的参数和结果内容,默认值是1。
$ watch demo.ClassGame methodName "{params,returnObj}" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 44 ms.
ts=2018-12-03 19:16:51; [cost=1.280502ms] result=@ArrayList[
@Object[][
@Integer[535629513],
],
@ArrayList[
@Integer[3],
@Integer[19],
@Integer[191],
@Integer[49199],
],
]
#观察异常信息的例子 -e表示抛出异常时才触发 express中,表示异常信息的变量是throwExp
$ watch demo.ClassGame methodName "{params[0],throwExp}" -e -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 62 ms.
ts=2018-12-03 19:38:00; [cost=1.414993ms] result=@ArrayList[
@Integer[-1120397038],
java.lang.IllegalArgumentException: number is: -1120397038, need >= 2
at demo.MathGame.primeFactors(MathGame.java:46)
at demo.MathGame.run(MathGame.java:24)
at demo.MathGame.main(MathGame.java:16)
,
]
#如果想查看方法运行前后,当前对象中的属性,可以使用target关键字,代表当前对象
$ watch demo.MathGame primeFactors 'target'
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 52 ms.
ts=2018-12-03 19:41:52; [cost=0.477882ms] result=@MathGame[
random=@Random[java.util.Random@522b408a],
illegalArgumentCount=@Integer[13355],
]
#使用target.field_name访问当前对象的某个属性
iceImpl.illegalArgumentCount, visit /root/logs/arthas/arthas.log for more details.
[arthas@3196]$ watch com.chinayanghe.smp.service.ModelConfigService getModelConfigByRole 'target.jedisUtil'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 173 ms.
ts=2019-12-11 16:59:54; [cost=185.897642ms] result=@JedisUtil[
log=@Logger[Logger[com.chinayanghe.smp.util.JedisUtil]],
jedisPool=@JedisPool[redis.clients.jedis.JedisPool@48f4713c],
]
来源:oschina
链接:https://my.oschina.net/u/3242075/blog/3141820