1 问题背景
前面阿里开源的Java诊断工具——Arthas对Arthas做了简要的概述,以及阐述了SpringBoot应用、Arthas、Arthas Tunnel Server三者之间的关系。现在用本地电脑做一个Arthas小实践——Arthas整合SpringBoot。
2 前言
本博文将在本地电脑搭一个简单的SpringBoot应用并整合
arthas-spring-boot-starter
,以jar包方式启动一个Arthas Tunnel Server,监控一个方法的返回结果。
3 SpringBoot应用整合Arthas
引入依赖(2021-10-13,目前最新版本号为
3.5.4
):
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>${arthas.version}</version>
</dependency>
配置arthas必要的信息:
arthas.agent-id=hsehdfsfghhwertyfad
arthas.tunnel-server=ws://localhost:7777/ws
解释上面配置:
- arthas.agent-id可以任意填写,Arthas Tunnel Server端是根据
arthas.agent-id
以及arthas.tunnel-server
来识别Clinet端的。 - 默认情况下,
arthas tunnel server
的web端口是8080
,arthas agent
连接的端口是7777
。 localhost
是arthas启动所在的机子的IP地址。
启动SpringBoot应用
4 启动Arthas Tunnel Server
前往github下载arthas tunnel server的jar包,使用如下命令启动:
java -jar arthas-tunnel-server-3.5.4-fatjar.jar
浏览器输入
localhost:8080
访问arthas tunnel server
,输入springboot
应用配置的arthas.agent-id
以及arthas.tunnel-server
,输入完点击connect
即可。如下所示:
效果如下图所示:
5 使用watch命令监控方法的返回值
待监控的方法如下,该方法只是简单加法将两个数相加:
@RestController
@Slf4j
@RequestMapping("/discount")
public class CalculateController {
@Autowired
private CalculateService calculateService;
@GetMapping("/calculate")
public BigDecimal calculateDiscountAmount(BigDecimal amount1, BigDecimal amount2) {
BigDecimal discountAmount = calculateService.calculate(amount1, amount2);
return discountAmount;
}
}
在
arthas tunnel server
控制台输入如下命令进行监控方法的返回值:
# 搜索CalculateController类的全类名
sc *CalculateController
# 监控CalculateController类中的calculateDiscountAmount方法
watch com.ganzalang.gmall.arthasdemo.controller.CalculateController calculateDiscountAmount
注:如果一个类中存在相同的方法名,入参却不同,则参考watch/monitor/trace 等判断重载函数/同名函数 #434
进入监控状态,效果如下:
根据
CalculateController
的calculateDiscountAmount()
的请求参数以及请求方式,在浏览器发送一个请求,笔者发送如下:
http://localhost:8100/discount/calculate?amount1=4&amount2=6
结果如下: