美文网首页
Java 诊断工具之 Arthas

Java 诊断工具之 Arthas

作者: Tinyspot | 来源:发表于2023-06-20 17:21 被阅读0次

    1. 安装

    1.1 方式一

    步骤一:下载 arthas-boot.jar,然后用java -jar的方式启动

    curl -O https://arthas.aliyun.com/arthas-boot.jar
    

    步骤二:选择应用 java 进程

    $ $ java -jar arthas-boot.jar
    * [1]: 35542
      [2]: 71560 math-game.jar
    

    1.2 方式二:使用 as.sh

    在 Linux/Unix/Mac 等平台,
    curl -L https://arthas.aliyun.com/install.sh | sh

    2. 常用命令

    2.1 watch

    • express 观察表达式,默认值:{params, target, returnObj}
    • [x:] 指定输出结果的属性遍历深度,默认为 1,最大值是 4
    • 小技巧:若超过 4 层,可指定参数属性(params[0].name})
    • -n 2,表示只执行两次

    示例:

    @Override
    public String greeting(User user, String env) {
        System.out.println("greeting run..." + JSON.toJSONString(user) + env);
        return JSON.toJSONString(user) + "; " + env;
    }
    

    通用方式:watch com.example.class.method '{params,returnObj,throwExp}' -n 5 -x 3

    [arthas@47310]$ watch com.example.concrete.starter.service.impl.GreetServiceImpl greeting '{params,returnObj,throwExp}'  -n 5  -x 3 
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 169 ms, listenerId: 1
    method=com.example.concrete.starter.service.impl.GreetServiceImpl.greeting location=AtExit
    ts=2023-06-21 16:56:17; [cost=1.690072ms] result=@ArrayList[
        @Object[][
            @User[
                serialVersionUID=@Long[5196056007908280999],
                name=@String[Tinyspot],
                age=@Integer[20],
                status=null,
                salary=null,
                total=null,
                extErrorCode=null,
            ],
            @String[local],
        ],
        @String[{"age":20,"name":"Tinyspot"}; local],
        null,
    ]
    

    指定参数 params[0]

    [arthas@47310]$ watch com.example.concrete.starter.service.impl.GreetServiceImpl greeting '{params[0],returnObj,throwExp}'  -n 5  -x 3 
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 47 ms, listenerId: 6
    method=com.example.concrete.starter.service.impl.GreetServiceImpl.greeting location=AtExit
    ts=2023-06-21 17:05:26; [cost=0.315678ms] result=@ArrayList[
        @User[
            serialVersionUID=@Long[5196056007908280999],
            name=@String[Tinyspot],
            age=@Integer[20],
            status=null,
            salary=null,
            total=null,
            extErrorCode=null,
        ],
        @String[{"age":20,"name":"Tinyspot"}; local],
        null,
    ]
    
    [arthas@47310]$ watch com.example.concrete.starter.service.impl.GreetServiceImpl greeting '{params[1],returnObj,throwExp}'  -n 5  -x 3 
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 47 ms, listenerId: 7
    method=com.example.concrete.starter.service.impl.GreetServiceImpl.greeting location=AtExit
    ts=2023-06-21 17:06:26; [cost=0.279525ms] result=@ArrayList[
        @String[local],
        @String[{"age":20,"name":"Tinyspot"}; local],
        null,
    ]
    

    指定参数里的属性

    [arthas@47310]$ watch com.example.concrete.starter.service.impl.GreetServiceImpl greeting '{params[0].name,returnObj,throwExp}'  -n 5  -x 3
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 49 ms, listenerId: 9
    method=com.example.concrete.starter.service.impl.GreetServiceImpl.greeting location=AtExit
    ts=2023-06-21 17:13:36; [cost=0.216618ms] result=@ArrayList[
        @String[Tinyspot],
        @String[{"age":20,"name":"Tinyspot"}; local],
        null,
    ]
    
    [arthas@47310]$ watch com.example.concrete.starter.service.impl.GreetServiceImpl greeting '{params[0].{#this.name},returnObj,throwExp}'  -n 5  -x 3 
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 49 ms, listenerId: 8
    method=com.example.concrete.starter.service.impl.GreetServiceImpl.greeting location=AtExit
    ts=2023-06-21 17:09:51; [cost=0.287647ms] result=@ArrayList[
        @ArrayList[
            @String[Tinyspot],
        ],
        @String[{"age":20,"name":"Tinyspot"}; local],
        null,
    ]
    

    2.2 trace

    作用:方法内部调用路径,并输出方法路径上的每个节点上耗时

    示例:

    [arthas@47310]$ trace com.example.concrete.starter.service.impl.GreetServiceImpl greeting  -n 5 --skipJDKMethod false 
    Press Q or Ctrl+C to abort.
    Affect(class count: 1 , method count: 1) cost in 201 ms, listenerId: 10
    `---ts=2023-06-21 17:17:33;thread_name=http-nio-8080-exec-4;id=5b;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@7f79edee
        `---[0.785942ms] com.example.concrete.starter.service.impl.GreetServiceImpl:greeting()
            +---[3.13% 0.024568ms ] java.lang.StringBuilder:<init>() #17
            +---[5.86% min=0.0039ms,max=0.031258ms,total=0.046033ms,count=3] java.lang.StringBuilder:append() #17
            +---[14.19% 0.111553ms ] com.alibaba.fastjson.JSON:toJSONString() #17
            +---[1.53% 0.011997ms ] java.lang.StringBuilder:toString() #17
            +---[19.77% 0.155415ms ] java.io.PrintStream:println() #17
            +---[0.74% 0.005834ms ] java.lang.StringBuilder:<init>() #18
            +---[5.28% 0.041536ms ] com.alibaba.fastjson.JSON:toJSONString() #18
            +---[1.92% min=0.003606ms,max=0.005787ms,total=0.015053ms,count=3] java.lang.StringBuilder:append() #18
            `---[0.55% 0.00435ms ] java.lang.StringBuilder:toString() #18
    
    

    2.3 反编译文件 - jad

    jad com.example.concrete.xxxServiceImpl

    3. IDEA 插件

    arthas idea

    4. Spring Boot 集成 Arthas

    <dependency>
        <groupId>com.taobao.arthas</groupId>
        <artifactId>arthas-spring-boot-starter</artifactId>
        <version>${arthas.version}</version>
    </dependency>
    

    相关文章

      网友评论

          本文标题:Java 诊断工具之 Arthas

          本文链接:https://www.haomeiwen.com/subject/xrayydtx.html