Arthas之热部署

背景

  • 项目源代码
public class HotDeployDemo {
    public static void main(String[] arrstring) throws InterruptedException {
        for (int i = 0; i >= 0; ++i) {
            new Thread(new Runnable(){

                @Override
                public void run() {
                    for (int i = 0; i >= 0; ++i) {
                        try {
                            Thread.sleep(5000L);
                            System.out.println("线程名: " + Thread.currentThread().getName() + ", j: " + i);
                            continue;
                        }
                        catch (InterruptedException interruptedException) {
                            interruptedException.printStackTrace();
                        }
                    }
                }
            }, "Thread_" + i).start();
            Thread.sleep(30000L);
        }
    }
}
  • 执行结果
    在这里插入图片描述

热部署

  1. Arthas环境初始化

https://arthas.aliyun.com/doc/download.html

// 启动方式
java -jar arthas-boot.jar
  1. 选择对应的项目序号
    在这里插入图片描述

  2. 使用jad命令反编译得到新的源文件

 jad --source-only HotDeployDemo > ./2/HotDeployDemo.java
  1. 修改新的源文件
public class HotDeployDemo {
    public static void main(String[] arrstring) throws InterruptedException {
        for (int i = 0; i >= 0; ++i) {
            new Thread(new Runnable(){

                @Override
                public void run() {
                    for (int i = 0; i >= 0; ++i) {
                        try {
                            Thread.sleep(5000L);
                            // 将打印的变量i变为j,当新的线程执行时显示的变量就会变为j
                            System.out.println("线程名: " + Thread.currentThread().getName() + ", j: " + i);
                            continue;
                        }
                        catch (InterruptedException interruptedException) {
                            interruptedException.printStackTrace();
                        }
                    }
                }
            }, "Thread_" + i).start();
            Thread.sleep(30000L);
        }
    }
}

  1. 获得原来的类加载器
sc -d HotDeployDemo
  1. 编译新的源文件
// -d参数表示制定目标字节码文件的位置
mc -classLoaderClass sun.misc.Launcher$AppClassLoader ./2/HotDeployDemo.java -d ./2/
  1. 将原来的字节码文件替换成新的字节码文件
redefine 2/HotDeployDemo$1.class
  1. 执行结果
    在这里插入图片描述
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值