目录
基于 Arthas 实现线上代码热更新
/  

基于 Arthas 实现线上代码热更新

基于 Arthas 实现线上代码热更新

1、下载 arthas,然后解压

wget https://github.com/alibaba/arthas/releases/download/arthas-all-3.7.2/arthas-bin.zip

mkdir arthas
mv arthas-bin.zip arthas/
cd arthas
unzip arthas-bin.zip

2、单元测试 demo

public class ArthasTests {
    @Test
    public void test() {
        while (true) {
            print();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void print() {
        long now = System.currentTimeMillis();
        System.out.println("hello arthas ! ---->>> " + String.valueOf(now).substring(9));
    }
}

然后运行

控制台打印:

hello arthas ! ---->>> 1794
hello arthas ! ---->>> 2798
hello arthas ! ---->>> 3800
hello arthas ! ---->>> 4802
hello arthas ! ---->>> 5807
hello arthas ! ---->>> 6809
...

3、使用 arthas 修改代码

./as.sh
Arthas script version: 3.7.2
[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/microsoft-17.jdk/Contents/Home
Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 24466 org.jetbrains.jps.cmdline.Launcher
  [2]: 24467 com.intellij.rt.junit.JUnitStarter
  [3]: 2708
# 我这里用 IDEA 跑的单元测试,这里直接选择 2
2
# arthas 终端
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.7.2
main_class com.intellij.rt.junit.JUnitStarter
pid        24467
time       2024-02-27 11:29:59

[arthas@24467]$

# 将代码反编译到指定目录
jad --source-only cn.lacknb.test.arthas.ArthasTests > /tmp/ArthasTests.java
# 然后修改这个java文件,将打印的信息修改一下
# 需要找到加载这个类的类加载器的hash
sc -d cn.lacknb.test.arthas.ArthasTests
#  classLoaderHash   18b4aac2,这个就是
# 内存编译代码
mc -c 18b4aac2 /tmp/ArthasTests.java -d /tmp

# redefine 热更新代码
redefine /tmp/cn/lacknb/test/arthas/ArthasTests.class

redefine 执行后,这里只有方法重新调用后才会生效

比如这个:

@Test
public void test() {
  while (true) {
    System.out.println("hello world !");
  }
}

用arthas改这个,由于一直在调用,改完后也无法生效

下面这个就可以

public void test() {
  while (true) {
    printf();
  }
}
private void printf() {
  System.out.println("hello world !");
}

打印结果:

hello arthas ! ---->>> 5807
hello arthas ! ---->>> 6809
hello arthas ! ---->>> 7809
hello arthas ! ---->>> 8814
hello arthas ! ---->>> 9819
hello arthas (hacker inject~) ! ---->>> 0821
hello arthas (hacker inject~) ! ---->>> 1826
hello arthas (hacker inject~) ! ---->>> 2828
hello arthas (hacker inject~) ! ---->>> 3832
...

标题:基于 Arthas 实现线上代码热更新
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2024/02/27/1709015166175.html