通过arthas vmtool 调用线上正在运行的service方法

简介: 通过arthas vmtool 调用线上正在运行的service方法

通过arthas vmtool 调用线上正在运行的service方法

场景

场景 具体描述
业务上有某个缓存需要删除,但是没有写删除 key 的远程接口 通过arthas执行 service 方法,删除缓存 key

1.前期准备

1.1下载arthas

官网地址

https://arthas.gitee.io/doc/quick-start.html

下载运行

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

image-20231030140415201

1.2 idea安装arthas插件

image-20231030140525882

1.3 写一个普通sum方法

image-20231030140646776

启动这个应用

image-20231030140739533

1.4 启动arthas并attach上helloworld进程

image-20231030141055423

选择1

image-20231030141132535

2. 实际操作

例如我们需要远程调用下这个sum方法,但是controller没用写调用sum方法接口,重新发版有风险且太慢了,于是可以利用arthas直接远程调用sum

image-20231030141624474

上面我们已经启动了arthas且attach上了helloworld进程

2.1 获取UserService的classLoaderHash

此时需要先获取UserService的classLoaderHash 用于后续我们指定访问这个方法

sc -d com.example.helloworld.service.UserService

image-20231030142131239

classLoaderHash 18b4aac2

2.2 通过idea arthas插件获取执行方法的命令

image-20231030142310922

image-20231030142452384

复制出来是这个样子

vmtool -x 3 --action getInstances --className com.example.helloworld.service.UserService --express 'instances[0].sum(new com.example.helloworld.controller.User())' -c 18b4aac2

参数解释

-x 3返回参数展开形式的,默认1,设置3,方便观察返回结果

-c xxx指定classLoaderHash

2.3 完善下,加上传递参数,例如传递年龄为3

vmtool -x 3 --action getInstances --className com.example.helloworld.service.UserService --express 'instances[0].sum(new com.example.helloworld.controller.User("zhangsan",3))' -c 18b4aac2

image-20231030143002222

image-20231030143039793

目录
相关文章
|
7月前
|
Arthas 运维 监控
Arthas stack (输出当前方法被调用的调用路径)
Arthas stack (输出当前方法被调用的调用路径)
373 37
|
6月前
|
Arthas 监控 Java
Arthas vmtool(从 jvm 里查询对象,执行 forceGc)
Arthas vmtool(从 jvm 里查询对象,执行 forceGc)
413 16
|
7月前
|
Arthas 监控 Java
Arthas trace (方法内部调用路径,并输出方法路径上的每个节点上耗时)
Arthas trace (方法内部调用路径,并输出方法路径上的每个节点上耗时)
967 10
|
7月前
|
Arthas 监控 Java
Arthas watch (方法执行数据观测)
Arthas watch (方法执行数据观测)
707 12
|
7月前
|
Arthas 监控 Java
Arthas sm(查看已加载类的方法信息 )
Arthas sm(查看已加载类的方法信息 )
147 6
|
7月前
|
Arthas 监控 前端开发
Arthas tt(方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)
Arthas tt(方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)
177 7
|
7月前
|
Arthas 运维 监控
Arthas monitor(方法执行监控)
Arthas monitor(方法执行监控)
540 0
|
12月前
|
Arthas 监控 安全
arthas如何跟踪某个方法?并查看方法的入参和出参?
arthas如何跟踪某个方法?并查看方法的入参和出参?
2103 6
|
Arthas 监控 Java
JVM知识体系学习七:了解JVM常用命令行参数、GC日志详解、调优三大方面(JVM规划和预调优、优化JVM环境、JVM运行出现的各种问题)、Arthas
这篇文章全面介绍了JVM的命令行参数、GC日志分析以及性能调优的各个方面,包括监控工具使用和实际案例分析。
1246 3
|
Arthas 监控 Java
Java 诊断利器 Arthas使用
Java 诊断利器 Arthas使用
3343 0