Arthas使用指南:安装与常用命令(trace、watch)详解
<blockquote><p><strong>Arthas</strong> 是阿里开源的Java诊断工具,能在不重启应用的情况下实现线上问题排查、性能监控和动态代码热更新。本文将详细介绍其核心功能与使用技巧。</p>
</blockquote>
<hr>
<h3 id="一快速安装与启动">一、快速安装与启动</h3>
<pre><code class="language-bash"># 下载最新版Arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
# 启动并选择目标JVM进程
java -jar arthas-boot.jar
</code></pre>
<p>启动后会显示当前机器上的Java进程列表,输入序号即可附加到目标进程进行诊断。</p>
<hr>
<h3 id="二性能瓶颈定位trace命令">二、性能瓶颈定位:trace命令</h3>
<p><strong>命令格式</strong>:</p>
<pre><code class="language-bash">trace [全限定类名] [方法名] '#cost>阈值' -n 监控次数
</code></pre>
<p><strong>核心功能</strong>:<br>
追踪方法内部调用链,精确到<strong>每一行代码的耗时</strong>,快速定位性能瓶颈。</p>
<p><strong>使用示例</strong>:</p>
<pre><code class="language-bash"># 监控saveMedicalDiag方法,仅显示耗时>10ms的调用,最多捕获5次
trace com.example.ClinicService saveMedicalDiag '#cost>10' -n 5
</code></pre>
<p><strong>输出解读</strong>:</p>
<pre><code>`---ts=2025-07-30 12:00:00;thread_name=http-nio-8080-exec-1;id=1e;is_daemon=true;priority=5;
`--- com.example.ClinicService:saveMedicalDiag()
+--- com.example.DBUtil:getConnection()
+--- com.example.DAO:insert()# ⚠️ 主要耗时点
`--- com.example.Logger:writeLog()
</code></pre>
<p><strong>注意事项</strong>:</p>
<ol>
<li>默认忽略<code>java.*</code>包下的调用,需逐层排查或使用正则监控多层调用</li>
<li>正则监控示例:<code>trace -E com.service.*|com.dao.* save*</code></li>
</ol>
<blockquote>
<p>📘 https://arthas.aliyun.com/doc/trace.html</p>
</blockquote>
<hr>
<h3 id="三运行时数据观测watch命令">三、运行时数据观测:watch命令</h3>
<p><strong>命令格式</strong>:</p>
<pre><code class="language-bash">watch [类名] [方法名] '{观察表达式}' [参数]
</code></pre>
<h4 id="核心功能">核心功能</h4>
<table>
<thead>
<tr>
<th><strong>观察表达式</strong></th>
<th><strong>作用</strong></th>
<th><strong>示例</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>params</code></td>
<td>捕获方法入参</td>
<td><code>{params.userName}</code></td>
</tr>
<tr>
<td><code>target</code></td>
<td>获取当前对象属性</td>
<td><code>target.connectionPoolSize</code></td>
</tr>
<tr>
<td><code>returnObj</code></td>
<td>获取返回值</td>
<td><code>returnObj.status</code></td>
</tr>
<tr>
<td><code>throwExp</code></td>
<td>捕获异常信息</td>
<td><code>throwExp.getMessage()</code></td>
</tr>
</tbody>
</table>
<h4 id="常用参数">常用参数</h4>
<table>
<thead>
<tr>
<th><strong>参数</strong></th>
<th><strong>作用</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-b</code></td>
<td>方法调用前触发</td>
</tr>
<tr>
<td><code>-e</code></td>
<td>异常时触发</td>
</tr>
<tr>
<td><code>-s</code></td>
<td>方法返回后触发</td>
</tr>
<tr>
<td><code>-f</code></td>
<td>方法结束后触发</td>
</tr>
<tr>
<td><code>-x 3</code></td>
<td>设置对象遍历深度</td>
</tr>
<tr>
<td><code>-n 10</code></td>
<td>仅监控前10次调用</td>
</tr>
<tr>
<td><code>--exclude-class-pattern *Controller</code></td>
<td>排除特定类</td>
</tr>
</tbody>
</table>
<h4 id="实用场景示例">实用场景示例</h4>
<p><strong>1. 监控异常时的入参</strong>:</p>
<pre><code class="language-bash">watch com.example.OrderService createOrder '{params, throwExp}' -e -x 3
</code></pre>
<p>输出:</p>
<pre><code>ts=2025-07-30 14:30:00; result=ArrayList[
@Object[][# 入参
@OrderRequest,# ⚠️ 空值导致异常
],
ctd.persistence.DAOException: 产品ID不能为空!# 异常信息
]
</code></pre>
<p><strong>2. 监控返回值属性</strong>:</p>
<pre><code class="language-bash">watch com.example.UserService getUser '{returnObj.age, returnObj.name}' -s
</code></pre>
<p><strong>3. 跟踪对象状态变化</strong>:</p>
<pre><code class="language-bash">watch com.example.CacheService refresh 'target.cacheSize' -f
</code></pre>
<blockquote>
<p>📘 https://arthas.aliyun.com/doc/watch.html</p>
</blockquote>
<hr>
<h3 id="四arthas核心优势">四、Arthas核心优势</h3>
<ol>
<li>
<p><strong>无侵入诊断</strong><br>
无需修改代码或重启服务,直接接入线上JVM进程</p>
</li>
<li>
<p><strong>精准定位</strong><br>
<code>trace</code>精确到代码行级耗时,<code>watch</code>实时捕获运行时数据</p>
</li>
<li>
<p><strong>动态热更新</strong><br>
支持在线修改字节码(https://arthas.aliyun.com/doc/redefine.html)</p>
</li>
<li>
<p><strong>全链路支持</strong><br>
集成火焰图、线程分析、内存监控等全方位工具</p>
</li>
</ol>
<hr>
<h3 id="五使用建议">五、使用建议</h3>
<ol>
<li>
<p><strong>生产环境慎用</strong><br>
优先在测试环境验证命令,避免<code>-f</code>参数导致大量日志输出</p>
</li>
<li>
<p><strong>组合使用命令</strong><br>
先用<code>trace</code>定位瓶颈方法,再用<code>watch</code>分析具体参数</p>
</li>
<li>
<p><strong>正则表达式优化</strong><br>
使用<code>-E</code>参数批量监控多个类:<code>watch -E 'com.service.*|com.dao.*' *</code></p>
</li>
</ol>
<blockquote>
<p>通过Arthas官方提供的https://arthas.aliyun.com/doc/web-console.html可图形化操作,提升诊断效率。</p>
</blockquote>
<p>掌握Arthas的核心命令,能让Java应用的线上问题排查效率提升数倍,真正实现“无需重启的诊断艺术”。</p>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:dashery,转载请注明原文链接:https://www.cnblogs.com/ydswin/p/19013845</p><br><br>
来源:https://www.cnblogs.com/ydswin/p/19013845
頁:
[1]