1. 概述
基于arthas工具的retransform命令热替换class,此方法用于在不重启项目的前提下,替换更新的class,如关键类添加打印日志,排查线上问题
但arthas工具的命令稍显复杂,有那个时间还不如重新部署,因此需要一个可以快速替换的工具来进行替换
2. 方法一:ArthasHotSwap插件
使用idea插件 ArthasHotSwap插件,安装后,选中类名数据右键点击“swap this class”,会复制一个脚本,然后粘贴到项目启动的服务器上,可完成替换,具体网上均有教程,此处不多赘述。
3. 方法二:自定义shell脚本
我本人也使用过这个插件,但是试过几次均没有成功因此模仿ArthasHotSwap插件的流程,写了一个自定义脚本。
3.1. ArthasHotSwap 插件流程
大致流程如下:
1.将本地需替换的class文件,编码成base64文本文件,并上传至文件服务器(可能是OSS)
2.提供一个默认执行脚本,给用户复制
3.用户拿到复制脚本后,在服务器上执行
4.脚本下载base64文本文件,并解析成class文件
5.启动arthas命令,如果用户没有安装,脚本会自动安装,默认路径 /home/用户名xxx/.arthas/
6.执行arthas中的retransform 命令,将解析出来的class文件替换至运行的jvm中
插件脚本源码:
点击查看代码
#Copyright (c) 2020, 2021, xxxtai. All rights reserved.
#!/usr/bin/env bash
echo "
/\$\$\$\$\$\$ /\$\$ /\$\$
/\$\$__ \$\$ | \$\$ | \$\$
| \$\$ \ \$\$ /\$\$\$\$\$\$ /\$\$\$\$\$\$ | \$\$\$\$\$\$\$ /\$\$\$\$\$\$ /\$\$\$\$\$\$\$
| \$\$\$\$\$\$\$\$ /\$\$__ \$\$|_ \$\$_/ | \$\$__ \$\$ |____ \$\$ /\$\$_____/
| \$\$__ \$\$| \$\$ \__/ | \$\$ | \$\$ \ \$\$ /\$\$\$\$\$\$\$| \$\$\$\$\$\$
| \$\$ | \$\$| \$\$ | \$\$ /\$\$| \$\$ | \$\$ /\$\$__ \$\$ \____ \$\$
| \$\$ | \$\$| \$\$ | \$\$\$\$/| \$\$ | \$\$| \$\$\$\$\$\$\$ /\$\$\$\$\$\$\$/
|__/ |__/|__/ \___/ |__/ |__/ \_______/|_______/
/\$\$ /\$\$ /\$\$ /\$\$\$\$\$\$
| \$\$ | \$\$ | \$\$ /\$\$__ \$\$
| \$\$ | \$\$ /\$\$\$\$\$\$ /\$\$\$\$\$\$ | \$\$ \__/ /\$\$ /\$\$ /\$\$ /\$\$\$\$\$\$ /\$\$\$\$\$\$
| \$\$\$\$\$\$\$\$ /\$\$__ \$\$|_ \$\$_/ | \$\$\$\$\$\$ | \$\$ | \$\$ | \$\$ |____ \$\$ /\$\$__ \$\$
| \$\$__ \$\$| \$\$ \ \$\$ | \$\$ \____ \$\$| \$\$ | \$\$ | \$\$ /\$\$\$\$\$\$\$| \$\$ \ \$\$
| \$\$ | \$\$| \$\$ | \$\$ | \$\$ /\$\$ /\$\$ \ \$\$| \$\$ | \$\$ | \$\$ /\$\$__ \$\$| \$\$ | \$\$
| \$\$ | \$\$| \$\$\$\$\$\$/ | \$\$\$\$/ | \$\$\$\$\$\$/| \$\$\$\$\$/\$\$\$\$/| \$\$\$\$\$\$\$| \$\$\$\$\$\$\$/
|__/ |__/ \______/ \___/ \______/ \_____/\___/ \_______/| \$\$____/
| \$\$
| \$\$
|__/
"
echo "************************************************** 1. Prepare the workspace **********************************************************"
if [ ! -d "./arthas-hot-swap" ]
then
mkdir ./arthas-hot-swap
echo "******* mkdir ./arthas-hot-swap success"
else
rm -rf ./arthas-hot-swap
mkdir ./arthas-hot-swap
echo "******* ./arthas-hot-swap exists, delete the directory first, and then create a new one"
fi
cd ./arthas-hot-swap
rm -f $(pwd)/arthas-hot-swap-result
echo "**************************************************** 2. install openssl **************************************************************"
openssl version
if [[ $? -eq 0 ]]; then
echo "******* openssl has been installed successfully "
else
echo "******* openssl is not installed, and installation were start next "
sudo yum install openssl openssl-devel
fi
echo "********************************************* 3. Download the encrypted file *********************************************************"
curl http://xxxtai-arthas-hot-swap.oss-cn-beijing.aliyuncs.com/public/WHObQxW0HIDDniGwtf0QetenxurLDcwehvqFZPmVx6I=x >> encrypt-FrameController.txt
echo "************************************************* 4. Encrypt the file ****************************************************************"
openssl enc -aes-128-cbc -a -d -in encrypt-FrameController.txt -out Fra