tags:[“Java”]
Arthas 是Alibaba开源的Java诊断工具,初步试用了一下,甚是方便,在生产线上找问题应该比较方便。
Arthas可以帮助你解决什么问题?
Arthas支持JDK 6+,支持Linux/Mac/Winodws,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
远程到生产的Linux主机上,一行代码即可下载工具包。
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
执行命令后,Arthas会列出当前机器上的Java进程,输入对应的编号进入交互式控制台,这时候就可以通过各种命令的组合来对这个Java进程进行诊断了。
同时,也提供了一个Web Console,地址为: http://127.0.0.1:8563/
但是实际意义估计不大,进去后也是个交互式控制台,还不如直接在终端里操作了。
输入 dashboard ,按回车/enter,会展示当前进程的信息,按ctrl+c可以中断执行。
关于仪表盘的各项指数,可以点击链接去查看详情,这里不再赘述,通过 Ctrl + C 可以退出。
可以通过thread命令来打印指定线程的堆栈信息
$ thread 96
"DubboSaveRegistryCache-thread-1" Id=96 WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@48ff8704
at sun.misc.Unsafe.park(Native Method)
- waiting on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@48ff8704
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Affect(row-cnt:0) cost in 154 ms.
该命令支持管道,配合grep命令可以筛选关键的一些信息,如:
$ thread 96 | grep 'runWorker('
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
使用jad来反编译某个类,这个功能很棒,再也不用下载jar包下来再反编译了。
jad com.ewei.open.dubbo.http.api.account.OpenRoleApi
ClassLoader:
+-WebappClassLoader
context:
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.URLClassLoader@6ec5b763
+-java.net.URLClassLoader@6ec5b763
+-sun.misc.Launcher$AppClassLoader@305f387c
+-sun.misc.Launcher$ExtClassLoader@3b756db3
Location:
/Users/wuwenze/Development/ewei-projects/helpdesk/ewei-open/target/ewei-open/WE
B-INF/lib/ewei-module-account-web-api-0.0.1-SNAPSHOT.jar
/*
* Decompiled with CFR 0_132.
*
* Could not load the following classes:
* com.ewei.account.api.entity.Role
* io.swagger.annotations.Api
* io.swagger.annotations.ApiOperation
* javax.validation.constraints.NotNull
* org.budo.dubbo.protocol.http.authentication.AuthenticationCheck
*/
package com.ewei.open.dubbo.http.api.account;
import com.ewei.account.api.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.budo.dubbo.protocol.http.authentication.AuthenticationCheck;
@Api(value="/u89d2/u8272/u76f8/u5173/u63a5/u53e3")
public interface OpenRoleApi {
....
通过该命令,实时监控某个函数的返回值,这个相当好使了,告别打印日志吧
$ watch com.ewei.account.api.service.IUserService findByEmailOrMobilePhoneOrExternalId returnobj
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 162 ms.
ts=2019-07-26 13:01:51; [cost=13.695ms] result=@User[
serialVersionUID=@Long[6552716967614882077],
TYPE_ENGINEER=@String[engineer],
TYPE_CUSTOMER=@String[customer],
TYPE_SUSPENDED_CUSTOMER=@String[suspended_customer],
STATUS_NO_VALIDATION=@Integer[2],
STATUS_NORMAL=@Integer[1],
STATUS_DISABLE=@Integer[0],
USER_STATUS_LABELS=@HashMap[isEmpty=false;size=3],
customerCustomFields=@HashSet[isEmpty=true;size=0],
engineerRole=null,
defaultServiceDesk=null,
token=null,
browserOpenId=null,
serviceDesk=null,
tags=@HashSet[isEmpty=true;size=0],
userBadgeConfig=null,
]
耗时,返回值一目了然,甚是方便,只要系统产生调用,立马在控制台就能监控到。