线上问题排查利器-arthas的使用

Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决

1、这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
2、我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
3、遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
4、线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
5、是否有一个全局视角来查看系统的运行状况?
6、有什么办法可以监控到JVM的实时运行状态?

文档使用地址: https://alibaba.github.io/arthas/

1、准备

准备一个springboot项目,配置及代码如下:
工程结构:
1373276-20190305104545446-198262916.png

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <groupId>com.springboot.test</groupId>
    <artifactId>springboot-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

server.port=80

App.java

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

UserController.java

package com.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class UserController {

    @ResponseBody
    @RequestMapping("/get")
    public List<String> get(){
        List<String> list = new ArrayList<String>();
        for(int i = 0 ; i < 5 ; i++){
            list.add(i + "");
        }
        return list;
    }
    
    @ResponseBody
    @RequestMapping("/exception")
    public List<String> exception(){
        List<String> list = new ArrayList<String>();
        for(int i = 0 ; i < 5 ; i++){
            if(i == 3){
                throw new RuntimeException("error");
            }
            list.add(i + "");
        }
        return list;
    }
}

右键项目:Run As - maven clean
右键项目:Run As - maven install
在项目的target目录下找到刚刚打包好的jar包,并上传至服务器

2、运行

此处我们先来监控get方法里的list对象

2.1、先运行java项目(在后台运行)
java -jar springboot-test-0.0.1-SNAPSHOT.jar &
2.2、下载arthas-boot.jar,然后用java -jar的方式启动:
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar

此处要选择一个JVM进程,选择刚刚运行的java进程(1)并回车,第一次安装要下载arthas的相关依赖:
1373276-20190305111046214-251892070.png
1373276-20190305112625305-1903787153.png

3、监控

watch命令:https://alibaba.github.io/arthas/watch.html

3.1、监控方法的返回值
watch com.springboot.controller.UserController get returnObj

再访问一下get方法:
1373276-20190305113213703-324398300.png
再回来看刚才监控的对象,返回的list对象里确实有5个对象:
1373276-20190305113302951-1696422315.png

3.2、监控异常信息
watch com.springboot.controller.UserController exception "{params,throwExp}" -e -x 2

再访问一下exception方法:
再回来看刚才监控的对象:
1373276-20190305191242043-719662486.png
所有信息一目了然,不需要改动生产的代码就可以看到调试信息

arthas命令列表 : https://alibaba.github.io/arthas/commands.html

转载于:https://www.cnblogs.com/lmj612/p/10475168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值