arthas的应用

arthas是一款弱小的Java诊断工具,能够帮忙开发者疾速定位和解决Java应用程序中的问题。本文将介绍arthas的根本应用办法,包含装置、启动、根本命令和高级性能。

一、装置arthas

1.1 Windows零碎

在Windows零碎中,能够通过以下步骤装置arthas:

  • 拜访arthas的GitHub发布页面:https://github.com/alibaba/arthas/releases
  • 下载最新版本的arthas(以arthas-boot.jar为例),并将其解压到一个目录中,例如:C:arthas
  • 将arthas的bin目录增加到零碎的PATH环境变量中,例如:C:\arthas\bin
  • 关上命令提示符,输出arthas --version,如果显示arthas的版本信息,则示意装置胜利。

1.2 Linux零碎

在Linux零碎中,能够通过以下步骤装置arthas:

  • 拜访arthas的GitHub发布页面:https://github.com/alibaba/arthas/releases
  • 下载最新版本的arthas(以arthas-boot.jar为例),并将其解压到一个目录中,例如:/opt/arthas
  • 将arthas的bin目录增加到零碎的PATH环境变量中,例如:/opt/arthas/bin
  • 关上终端,输出arthas --version,如果显示arthas的版本信息,则示意装置胜利。

二、启动arthas

2.1 在线模式

在线模式下,arthas会附加到指标Java过程上,实时监控和诊断Java应用程序。启动arthas的命令如下:

java -jar arthas-boot.jar <指标过程ID>

其中,<指标过程ID>是指标Java过程的ID。能够通过jps命令查看以后运行的Java过程及其ID。

2.2 离线模式

离线模式下,须要先导出指标Java过程的内存快照(core文件),而后应用arthas剖析该快照。启动arthas的命令如下:

java -jar arthas-boot.jar --core <core文件门路>

其中,<core文件门路>是指标Java过程的内存快照文件门路。

三、根本命令

3.1 help命令

help命令能够查看arthas反对的所有命令及其简要阐明。输出help后,会显示如下输入:

Usage: help [command] [options] -h | --help | --version | --description | --groups | --classloaders | --threads | --dependencies | --dashboard | --interceptors | --sc | --jad | --mc | --redefine | --watch | --option | --websocket | --telnet | --ssh | --local | --tunnel | --selectJvm | --selectClassLoader | --selectInstace | --attach | --detach | --quit | --close | --reset | --reload | --kill | --suspend | --resume | --interrupt | --interactive | --batch | --sampler [options] | --sample <pattern> [options] | --monitor <pattern> [options] | --trace <pattern> [options] | --stack <pattern> [options] | --tt <pattern> [options] | --info <pattern> [options] | --methods [options] | --params [options] | --bp <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options] | --interceptors [options] | --sc <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options] | --interceptors [options] | --sc <pattern> [options] | --jad <pattern> [options] | --mc <pattern> [options] | --redefine <pattern> [options] | --watch <pattern> [options] | --option <pattern> [options] | --dashboard [options]br -b, -eBreakpoint, -nNew breakpoint, -cClear breakpoint, -lList breakpoints, -aAll breakpoints, -iInfo breakpoint, -xDisable breakpoint, -dDelete breakpoint, -hHelp, -vVersion, -tThread, -oOption, -fForce, -qQuick mode, -mMethod trace filter, -pPattern match, -rRange match, -uUser defined range match, -gGroup match, -LLine number match, -NNext line match, -CClass match, -EException match, -SSource match, -FField match, -DDebugger command match, -GGroup name match, -HHot swap match, -MMethod modifier match, -IInstance ID match, -RRemote IP match, -TThread CPU time threshold (ms), -WWaiting for matching thread to suspend, -XSkip matched method in stack trace, -YForce matched method in stack trace, -ZEnable class redefinition, -UUse agent built-in properties and variables as default values when parsing option arguments, -KKeep alive interval (ms), -PPlugin class name pattern match, -AAgent address match, -BBatch mode flag, -CConnect address and port of target JVM or agent process, -DConnect address and port of local JVM or agent process if tunnel is enabled, -EError message template for batch mode output, -FFile path pattern match for batch mode input and output files, -GGroup name pattern match for batch mode input and output files, -HHelp message template for batch mode output, IInterval between batch mode commands (ms), JNumber of times to repeat batch mode command execution, KKeep alive interval (ms), LLog level for batch mode output messages, MMessage template for batch mode output messages with placeholders for error codes and error messages from failed commands or scripts in batch mode output file(s), NNumber of lines to skip at the beginning of batch mode input file(s), OOutput file path for batch mode output messages and error messages from failed commands or scripts in batch mode output file(s), PPlugin class name pattern match for batch mode input and output files, QQuick mode flag for batch mode commands execution, RRemote IP pattern match for batch mode input and output files, SSource pattern match for batch mode input and output files, TTarget JVM or agent process ID or address pattern match for batch mode commands execution and input/output files selection if tunnel is enabled or not specified respectively

3.2 watch参数的根本用法

watch参数的根本用法非常简单,只须要在arthas命令行中输出watch关键字,前面跟上要监控的办法名即可。例如,咱们想要监控com.example.demo.service.UserService类的addUser办法,能够输出以下命令:

watch com.example.demo.service.UserService addUser

执行该命令后,arthas会实时显示addUser办法的调用状况,包含入参、出参、异样等信息。如果须要退出watch模式,能够按Ctrl+C组合键。

watch参数的高级用法

除了根本用法外,watch参数还有一些高级用法,能够帮忙咱们更灵便地监控办法。以下是一些罕用的高级用法:

1. 指定类和办法

咱们能够应用通配符*来指定类和办法。例如,咱们想要监控所有以com.example.demo.service.结尾的服务类的所有办法,能够输出以下命令:

watch com.example.demo.service.* *

2. 指定返回值类型

咱们能够应用-x参数来指定返回值的类型。例如,咱们想要监控com.example.demo.service.UserService类的getUserById办法,只关怀返回值中的idname字段,能够输出以下命令:

watch -x "{id: id, name: name}" com.example.demo.service.UserService getUserById

3. 指定条件表达式

咱们能够应用-b参数来指定条件表达式。例如,咱们想要监控com.example.demo.service.UserService类的updateUser办法,当更新胜利时才显示信息,能够输出以下命令:

watch -b "result == true" com.example.demo.service.UserService updateUser

4. 指定输入格局

咱们能够应用-o参数来指定输入格局。例如,咱们想要以JSON格局输入监控后果,能够输出以下命令:

watch -o json com.example.demo.service.UserService addUser

5. 指定日志级别

咱们能够应用-l参数来指定日志级别。例如,咱们想要以debug级别的日志输入监控后果,能够输出以下命令:

watch -l debug com.example.demo.service.UserService addUser