线上问题排查利器-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项目,配置及代码如下: 工程结构: ![](https://img2018.cnblogs.com/blog/1373276/201903/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的相关依赖:

3、监控

watch命令:https://alibaba.github.io/arthas/watch.html ##### 3.1、监控方法的返回值
watch com.springboot.controller.UserController get returnObj

再访问一下get方法:

再回来看刚才监控的对象,返回的list对象里确实有5个对象:

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

再访问一下exception方法:
再回来看刚才监控的对象:

所有信息一目了然,不需要改动生产的代码就可以看到调试信息

3.3、监控慢接口

可以监控到方法每一行代码的调用时长

trace demo.MathGame run '#cost > 10'

https://arthas.aliyun.com/doc/trace.html#包含-jdk-的函数

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

posted @ 2019-03-05 19:16  风中的蜗牛  阅读(783)  评论(0编辑  收藏  举报