Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决
1、这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception? 2、我改的代码为什么没有执行到?难道是我没 commit?分支搞错了? 3、遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗? 4、线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现! 5、是否有一个全局视角来查看系统的运行状况? 6、有什么办法可以监控到JVM的实时运行状态?
文档使用地址: <a href='https://alibaba.github.io/arthas/'>https://alibaba.github.io/arthas/</a>
<h1>1、准备</h1> 准备一个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包,并上传至服务器
<h1>2、运行</h1> 此处我们先来监控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的相关依赖: ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190305111046214-251892070.png) ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190305112625305-1903787153.png)
<h1>3、监控</h1> watch命令:<a href='https://alibaba.github.io/arthas/watch.html'>https://alibaba.github.io/arthas/watch.html</a> ##### 3.1、监控方法的返回值 ``` watch com.springboot.controller.UserController get returnObj ``` 再访问一下get方法: ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190305113213703-324398300.png) 再回来看刚才监控的对象,返回的list对象里确实有5个对象: ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190305113302951-1696422315.png) ##### 3.2、监控异常信息 ``` watch com.springboot.controller.UserController exception "{params,throwExp}" -e -x 2 ``` 再访问一下exception方法: 再回来看刚才监控的对象: ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190305191242043-719662486.png) 所有信息一目了然,不需要改动生产的代码就可以看到调试信息
arthas命令列表 : <a href='https://alibaba.github.io/arthas/commands.html'>https://alibaba.github.io/arthas/commands.html</a>
来源:oschina
链接:https://my.oschina.net/u/4269500/blog/3626555