一、背景
很久之前就有一个想法:如果部署在线上的代码在遇到类似空指针异常时能自动修复就好了。前段时间准备尝试落实这个想法。那就先从基础做起:先在线改代码吧!
本来我是在学习字节码的,偶尔搜了一下资料,发现Arthas这个工具,能够实现代码热修改。
我仿佛发现了新大陆,于是添加收藏。这时我发现,我的收藏夹里很早就收藏了这个网页。看来有些路终究是要走的。
废话不说,试试吧!
二、开始
1、启动springboot项目
2、运行arthas,并选择目标进程
我这里选择2
java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.6.0
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
- [1]: 6784 org.jetbrains.jps.cmdline.Launcher
[2]: 6787 com.shuimutong.learn.arthos.helloweb.HelloWebApplication
3、使用sc命令查找要修改的类
我这里准备在StringUtil里加上一个打印,输入查找命令如下:
[arthas@6787]$ sc -d *util.StringUtil
class-info com.shuimutong.learn.arthos.helloweb.util.StringUtil
code-source /Users/zhengxingao/Documents/develop/arthoslearn/helloweb/target/classes/
name com.shuimutong.learn.arthos.helloweb.util.StringUtil
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false