资讯专栏INFORMATION COLUMN

Java诊断工具Arthas

happyfish / 2874人阅读

摘要:诊断工具简介是阿里开源的一个线上诊断工具,发现阿里还是挺喜欢开源一些技术的,造福人类。昨天试用了一下,发现真是强大,解决了我工作两年的很多困扰,有点相见恨晚的感觉。每秒统计方法的调用次数,成功失败次数和平均响应时间等。具体使用请见。

Java诊断工具Arthas 1. Arthas简介

Arthas是阿里开源的一个线上java诊断工具,发现阿里还是挺喜欢开源一些技术的,造福人类。昨天试用了一下,发现真是强大,解决了我工作两年的很多困扰,有点相见恨晚的感觉。

根据官网的说明,它能解决下面的问题:

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?

我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?

遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?

线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!

是否有一个全局视角来查看系统的运行状况?

有什么办法可以监控到JVM的实时运行状态?

Arthas采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

2. Arthas安装

在Linux下直接执行curl -L https://alibaba.github.io/arthas/install.sh | sh,然后执行./as.sh就可以进入到Arthas的命令行界面。

3. Arthas试用

安装完后试用了它的监控相关的功能,以后调试就不那么麻烦了。

# 进入命令行界面,刚进入的时候会显示所有的java进程,选择对应的数字然后回车就可以进入到对应的进程中
./as.sh
Arthas script version: 3.0.4.1
Found existing java process, please choose one and hit RETURN.
  [1]: 27073 org.apache.rocketmq.namesrv.NamesrvStartup
* [2]: 2379 org.apache.catalina.startup.Bootstrap
2
Calculating attach execution time...
Attaching to 2379 using version 3.0.5...

real    0m0.249s
user    0m0.208s
sys    0m0.025s
Attach success.
telnet connecting to arthas server... current timestamp is 1545738424
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is "^]".
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O   |  .--. ""--.  .--"|  "--"  | /  O   "   .-"                          
|  .-.  ||  "--"."   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |      |  |   |  |  |  ||  | |  |.-"    |                         
`--" `--"`--" "--"   `--"   `--"  `--"`--" `--"`-----"                          
                                                                                

wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 2379
timestamp: 1545738424462

$
3.1 monitor命令

monitor可以对某个类的某个方法进行周期性监控,来输出方法的平均响应时间,成功失败次数,调用次数等。

# 每3秒统计queryCatalog方法的调用次数,成功失败次数和平均响应时间等。
$ monitor -c 3 com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 82 ms.


 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:41  com.timeline.services.CatalogService  queryCatalog  3      3        0     2.46        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:44  com.timeline.services.CatalogService  queryCatalog  0      0        0     0.00        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:47  com.timeline.services.CatalogService  queryCatalog  0      0        0     0.00        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:50  com.timeline.services.CatalogService  queryCatalog  2      2        0     2.08        0.00%   

具体使用请见:https://alibaba.github.io/art...。

3.2 watch命令

watch可以监控某个方法的出入参,异常信息等。

# 观察方法的出入参
# -x表示遍历深度,可以调整来打印具体的参数和结果内容,默认值是1。
$watch com.timeline.services.CatalogService queryCatalog "{params,returnObj}" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 73 ms.
ts=2018-12-25 19:49:55;result=@ArrayList[
    @Object[][isEmpty=true;size=0],
    @ArrayList[
        @CatalogVO[CatalogVO(id=59, name=wangjun, location=beijing, time=1537256454000, description=desccc)],
    ],
]
# 可以看到入参是空的,出参是一个list

具体使用请见:https://alibaba.github.io/art...。

3.3 trace命令

trace可以输出方法内部调用关系,并打印方法内部每个调用的耗时。

# 打印queryCatalog方法内每个调用的耗时
$ trace com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 101 ms.
`---ts=2018-12-25 20:00:25;thread_name=http-nio-8080-exec-8;id=1b;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.ParallelWebappClassLoader@4d1110a3
    `---[2.896979ms] com.timeline.services.CatalogService:queryCatalog()
        +---[2.605774ms] com.timeline.dao.CatalogDao:queryCatalog()
        +---[0.0048ms] java.util.ArrayList:()
        +---[min=0.001154ms,max=0.00634ms,total=0.007494ms,count=2] java.util.List:size()
        +---[0.004428ms] java.util.List:get()
        +---[0.005115ms] com.timeline.entity.Catalog:getId()
        +---[0.003107ms] com.timeline.entity.Catalog:getName()
        +---[0.0026ms] com.timeline.entity.Catalog:getLocation()
        +---[0.002573ms] com.timeline.entity.Catalog:getTime()
        +---[0.005901ms] com.timeline.common.Utils:timestamp2Long()
        +---[0.002543ms] com.timeline.entity.Catalog:getDescription()
        +---[0.005133ms] com.timeline.vo.CatalogVO:()
        `---[0.004174ms] java.util.List:add()

具体使用请见:https://alibaba.github.io/art...。

3.4 stack命令

stack可以输出方法的整个调用路径。

# 打印queryCatalog方法的整个调用路径(不抛出堆栈也能看到调用链啦!)
$ stack com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 82 ms.
ts=2018-12-25 20:02:56;thread_name=http-nio-8080-exec-9;id=1c;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.ParallelWebappClassLoader@4d1110a3
    @com.timeline.controllers.CalalogController.queryCatalog()
        at sun.reflect.GeneratedMethodAccessor46.invoke(null:-1)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
        

具体使用请见:https://alibaba.github.io/art...。

3.5 tt命令

记录方法每次的调用出入参和异常信息,并且完整保留调用现场,可以再次模拟调用。

# 记录每次调用queryCatalog的情况,可以根据其他命令打印出入餐和复现现场
$ tt -t com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 128 ms.


 INDEX       TIMESTAMP                    COST(ms)      IS-RET      IS-EXP     OBJECT                CLASS                                       METHOD                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1008        2018-12-25 20:04:46          4.747987      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1009        2018-12-25 20:04:47          2.270835      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1010        2018-12-25 20:04:47          1.434482      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1011        2018-12-25 20:04:48          1.82891       true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1012        2018-12-25 20:04:49          2.27361       true        false      0x2577a8a4            CatalogService                              queryCatalog

具体使用请见:https://alibaba.github.io/art...。

当然除此之外还有很丰富的其他功能,比如JVM,ClassLoader相关的命令。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72180.html

相关文章

  • 推荐一款阿里开源的 Java 诊断工具,好用到爆!

    摘要:是什么鬼是一款阿里巴巴开源的线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题。诊断使用的是命令行交互模式,支持,操作系统,命令还支持使用键对各种信息的自动补全,诊断起来非常利索。显示当前系统的实时数据面板,按即可退出。 Arthas是什么鬼? Arthas是一款阿里巴巴开源的 Java 线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题。 Arthas诊断使用的是...

    Rango 评论0 收藏0
  • 爱上Java诊断利器之Arthas

    摘要:是什么摘自的介绍大意为是阿里开源的一个诊断工具,可以帮助开发人员在不修改代码或重启服务器的情况下快速定位线上问题。听起来确实是我们的程序员的一大福利。参考资料使用手册 1. Arthas是什么? 摘自Arthas的Github介绍: Arthas is a Java Diagnostic tool open sourced by Alibaba. Arthas allows deve...

    Hancock_Xu 评论0 收藏0
  • 阿里在线诊断工具Arthas调整日志等级记录

    摘要:阿里的开源诊断工具可以在线调整等级查找当前类的用获取单独设置的全局设置 阿里的开源Java诊断工具Arthas可以在线调整log等级 查找当前类的classloader hashcode sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash 用OGNL获取logger ognl -c 1be6f...

    lansheng228 评论0 收藏0
  • Arthas使用指南

    摘要:注意这个统计也会产生一定的开销这个接口本身开销比较大,因此会看到的线程占用一定的百分比,为了降低统计自身的开销带来的影响,可以把采样间隔拉长一些,比如毫秒。 Arthas 能为你做什么? 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception? 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了? 遇到问题无法在预发 debug 一下,难道只能通过加...

    darkerXi 评论0 收藏0
  • 使用Arthas 获取Spring ApplicationContext还原问题现场

    摘要:背景最近来了个实习僧小弟,安排他实现对目标网站连通性检测的小功能简单讲就是将下边的脚本换成代码来实现百度平台状态不正常,请注意功能实现使用开始执行定时任务,检测百度网站连通性请求百度成功,返回报文请求异常百度执行检测百度网站连通 背景 最近来了个实习僧小弟,安排他实现对目标网站 连通性检测的小功能,简单讲就是将下边的shell 脚本换成Java 代码来实现 #!/bin/bash UR...

    xeblog 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<