java中arthas如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
下载Arthas
wget https://alibaba.github.io/arthas/arthas-boot.jar
把下载好的arthas-boot.jar
包放到想要监测的java应用所在服务器,跟Spring Boot
应用一样,直接使用java命令运行即可。
java -jar arthas-boot.jar
注意:
第一次运行,下载慢可以使用--repo-mirror aliyun --use-http
启动后,会列出当前的java应用列表(有点像jps -l
),输出序号选择想要监测的应用即可。
启动后即进行Arthas
的命令行界面,可以使用Arthas
提供的命令来实现需要监测的功能。如下图,需要监测的java应用是示例java-monitor-example
。
如果只是退出当前的连接,可以用quit
或者exit
命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出arthas,可以执行shutdown
命令。
Arthas
的使用就是需要学会使用它提供的命令功能,主要分为几大类:
基本命令:help
、cat
、pwd
、history
,quit
等等,跟linux的命令差不多。
jvm相关:dashboard
、thread
、jvm
、sysenv
等等,主要是对JVM信息的监测,跟之前学习java命令行工具jinfo
、jmap
、jstack
等有异曲同工之妙。
class/classloader相关:sc
、sm
、jad
、dump
、classloader
等等。
monitor/watch/trace相关:monitor
、watch
、trace
、stack
等等,这些功能基本涵盖了BTrace
中所实现的功能,包括定时检测,方法参数,返回值,调用时长等。
下面对常用的几个命令进行说明,详细的命令列表请查阅官方文档。
dashboard
启动Arthas
后,-h
查看使用帮助:
$ dashboard -h
USAGE:
dashboard [-b] [-h] [-i <value>] [-n <value>]
SUMMARY:
Overview of target jvm's thread, memory, gc, vm, tomcat info.
EXAMPLES:
dashboard
dashboard -n 10
dashboard -i 2000
相当于概览,在一个界面中显示线程、内存、gc情况,vm情况和tomcat信息。如下图(官方文档的示例图):
这个概览的信息默认5秒刷新一次,对于内存变化,线程占用情况,GC次数,一目了然。使用ctrl+c
退出。
thread
还记得jstack
吗,我们需要先找出线程ID,使用它导出线程堆栈,然后使用线程ID查看。在Arthas
中就方便多了,像上面dashboard
中,已经有ID,直接使用thread id
即可。-h
查看帮助文档:
$ thread -h
USAGE:
thread [-h] [-b] [-i <value>] [-n <value>] [id]
SUMMARY:
Display thread info, thread stack
EXAMPLES:
thread
thread 51
thread -n -1
thread -n 5
thread -b
thread -i 2000
OPTIONS:
-h, --help this help
-b, --include-blocking-thread Find the thread who is holding a lock that blocks the most number of threads.
-i, --sample-interval <value> Specify the sampling interval (in ms) when calculating cpu usage.
-n, --top-n-threads <value> The number of thread(s) to show, ordered by cpu utilization, -1 to show all.
<id> Show thread stack
如上面所示的EXAMPLES
,使用thread
命令,可以找出占用CPU最高前N个线程(-n
),可以打印指定线程的运行堆栈(id
),找出当前阻塞其他线程的线程(-b
),由此来分析线程问题很方便。
jvm
jvm
命令很简单,没有参数,它输出的信息包括运行参数,类加载信息, 内存情况,系统信息,线程数信息,文件描述符等。有点像jvisualvm
的中概述
,但比它更详细。
jad
有时需要检测线上运行的应用中,新的代码是否有使用或者是否有更新到,可以把加载的类反编译出来,查看源码是否为最新的,这时jad
就很有用。-h
打印使用帮助:
$ jad -h
USAGE:
jad [-c <value>] [-h] [-E] [--source-only] class-pattern [method-name]
EXAMPLES:
jad java.lang.String
jad java.lang.String toString
jad --source-only java.lang.String
jad -c 39eb305e org/apache/log4j/Logger
jad -c 39eb305e -E org\\.apache\\.*\\.StringUtils
OPTIONS:
-c, --code <value> The hash code of the special class's classLoader
-h, --help this help
-E, --regex Enable regular expression to match (wildcard matching by default)
--source-only Output source code only
<class-pattern> Class name pattern, use either '.' or '/' as separator
<method-name> method name pattern, decompile a specific method instead of the whole class
如上面所示的EXAMPLES
,jad可以反编译类(class-pattern
),反编译类的某个方法(method-name
),如果有多个classLoader
,还可以使用-c
来选择显示哪个等。
monitor
monitor
可以定时输出方法的执行情况进行监控,包括调用次数,成功次数,失败次数,平均时长,失败率等,有点像BTrace
中的@Timer
,但是更方便。-h
查看使用帮助:
$ monitor -h
USAGE:
monitor [-c <value>] [-h] [-n <value>] [-E] class-pattern method-pattern
SUMMARY:
Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.
Examples:
monitor org.apache.commons.lang.StringUtils isBlank
monitor org.apache.commons.lang.StringUtils isBlank -c 5
monitor -E org\.apache\.commons\.lang\.StringUtils isBlank
OPTIONS:
-c, --cycle <value> The monitor interval (in seconds), 60 seconds by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching by default)
<class-pattern> Path and classname of Pattern Matching
<method-pattern> Method of Pattern Matching
如上面所示的EXAMPLES
,可以监测方法执行情况,默认是60s输出一次,可以使用-c
来修改输出间隔时间。
watch
类似于BTrace
的@OnMethod
,若想在线上的应用中把执行方法时的参数,返回值,异常信息,watch
命令就非常合适。-h
使用帮助:
$ watch -h
USAGE:
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]
SUMMARY:
Display the input/output parameter, return object, and thrown exception of specified method invocation
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params : the parameters array of method
params[0..n] : the element of parameters array
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
Examples:
watch -b org.apache.commons.lang.StringUtils isBlank params
watch -f org.apache.commons.lang.StringUtils isBlank returnObj
watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
watch -bf *StringUtils isBlank params
watch *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0] params[0].length==1
watch *StringUtils isBlank params '#cost>100'
watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]
OPTIONS:
-b, --before Watch before invocation
-e, --exception Watch after throw exception
-x, --expand <value> Expand level of object (1 by default)
-f, --finish Watch after invocation, enable by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching by default)
-M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by default)
-s, --success Watch after successful invocation
<class-pattern> The full qualified class name you want to watch
<method-pattern> The method name you want to watch
<express> the content you want to watch, written by ognl.
Examples:
params
params[0]
'params[0]+params[1]'
'{params[0], target, returnObj}'
returnObj
throwExp
target
clazz
method
如上面所示的EXAMPLES
,监测时机分别是方法执行前(-b
),方法执行结束(-f
,默认值),方法执行成功(-s
)。监测内容包括:参数(params
),返回值(returnObj
),异常(throwExp
)等。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/mianshenglee/blog/3100324