下载 arthas-boot.jar
bash
curl -O https://arthas.aliyun.com/arthas-boot.jar
一、现在你还没进 Arthas(很重要)
你刚才只是 ls 和 pwd,
还没有真正 attach JVM。
👉 现在请执行这一条命令:
bash
java -jar arthas-boot.jar
二、你接下来一定会看到这个界面(正常)
类似这样 👇
Found existing java process, please choose one and hit RETURN.
* [1]: 23145 demo20250304-0.0.1-SNAPSHOT.jar
👉 你要做的事:
-
看清楚是你这个 jar:
demo20250304-0.0.1-SNAPSHOT.jar -
输入前面的数字,比如:
text
1
然后 回车
三、看到这个,说明你成功进 Arthas 了 ✅
Welcome to Arthas!
并且命令行前缀会变成:
text
[arthas@23145]$
到这一步,你已经成功 attach 正在运行的 JVM
后面所有操作,都是 不重启、直接改内存
四、定位 BookController(第一条 Arthas 命令)
在 Arthas 控制台里,输入👇:
bash
sc *BookController*
正常你会看到类似:
com.example.demo.controller.BookController
👉 把这个完整包名记住
假设是:
com.example.demo.controller.BookController
五、确认类信息(非常重要,不跳)
bash
sc -d com.example.demo.controller.BookController
你主要看两点:
classLoaderHashlocation(一般是 BOOT-INF/classes)
只要能正常显示,就可以 redefine
六、现在开始真正热替换(核心一步)
⚠️ 注意:
这条命令里的路径,是 Linux 绝对路径
你的 class 在:
/root/1230/BookController.class
👉 在 Arthas 控制台中执行:
bash
redefine /root/1230/BookController.class
七、成功标志(你要看到这个)
redefine success, size: 1
🎉 恭喜你,此时:
- ❌ 没有重启 jar
- ❌ 没有改磁盘 jar
- ✅ BookController 的新逻辑已在 JVM 内存中生效
八、立刻验证(必须做)
方式一:直接调接口
bash
curl http://localhost:8080/你BookController的接口
方式二:看日志(强烈推荐)
如果你在新代码里加了类似:
java
log.info("=== arthas hotfix ===");
那你现在一请求接口,就应该能看到。
九、如果 redefine 报错(别慌,对号入座)
❌ 报 VerifyError
👉 你改了方法签名 / 字段 / 注解
❌ 报 Unsupported major.minor
👉 class 编译 JDK 和线上不一致
❌ 提示找不到 class
👉 说明这个 Controller 还没被加载(极少)
十、非常重要的一句话(老工程师经验)
Arthas 热修 = 临时止血
一定要记得:后续正常打包 + 重启发布
否则:
- JVM 一重启
- 你的修改就 全部消失
shell
[root@VM-12-7-centos 1230]# java -jar arthas-boot.jar
[INFO] JAVA_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/jre
[INFO] arthas-boot version: 4.1.4
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 20038 demo20250304-0.0.1-SNAPSHOT.jar
1
[INFO] arthas home: /root/.arthas/lib/4.1.4/arthas
[INFO] Try to attach process 20038
Picked up JAVA_TOOL_OPTIONS:
[INFO] Attach process 20038 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 4.1.4
main_class demo20250304-0.0.1-SNAPSHOT.jar
pid 20038
start_time 2025-12-31 00:14:37.452
current_time 2025-12-31 00:15:05.480
[arthas@20038]$ redefine /root/1230/BookController.class
redefine success, size: 1, classes:
com.example.demo20250304.controller.BookController
[arthas@20038]$