基于arthas热更新class脚本

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 FrameController.class -K $1 -iv $2

echo "************************************************* 5. Install arthas ******************************************************************"
specifyJavaHome=
arthas_start_cmd=''

if [[ ${specifyJavaHome} == '' ]]
then
    curl -L https://arthas.aliyun.com/install.sh | sh
    arthas_start_cmd='./as.sh'
else
    curl -O https://arthas.aliyun.com/arthas-boot.jar
    arthas_start_cmd=${specifyJavaHome}" -jar arthas-boot.jar"
fi

selectJavaProcessName=%[selectJavaProcessName]

if [[ ${selectJavaProcessName} != '' ]]
then
    arthas_start_cmd=${arthas_start_cmd}" --select "${selectJavaProcessName}
fi

echo "************************************************* 6. Create a pipeline ***************************************************************"
rm -f tmp_in
mknod tmp_in p
exec 8<> tmp_in
${arthas_start_cmd} <&8 &

echo "********************************************* 7. Choose the java process *************************************************************"
sleep 1s
echo "
" >> tmp_in

echo "*********************************************** 8. Redefine the class ****************************************************************"
sleep 3s
echo "retransform $(pwd)/FrameController.class > $(pwd)/arthas-hot-swap-result" >> tmp_in
sleep 4s
echo "quit" >> tmp_in
sleep 2s

swapResult=$(cat $(pwd)/arthas-hot-swap-result | grep "success")
echo $swapResult
if [[ $swapResult != "" ]]
then
echo '
****************************************** 9. The following files were successfully hot deployed *************************** ****************
*****
***** FrameController.class
*****
********************************************************************************************************************************************
'
else
echo '
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9. Failed to hot deployed the following files %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
%%%%% FrameController.class
%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
cat $(pwd)/arthas-hot-swap-result
fi

3.2. 自定义脚本

按照这个流程,自定义写了一个脚本,去掉了上传文件服务器步骤,只需要把许替换的文件上传至服务器指定目录,启动脚本时指定项目包名称,即可完成指定项目多个class文件热替换

#热刷新class指定路径配置
updateClassPath="/tmp/replace"
#服务器上arthas路径
arthasPath="/home/cmp/.arthas/lib/3.6.7/arthas"
#入参 项目包名称,支持模糊匹配
projectName=$1

rm -f tmp_in
mknod tmp_in p
exec 8<> tmp_in
sh ${arthasPath}/as.sh --select $projectName <&8 &
echo "********************************************* Arthas HotSwap The Java Class *************************************************************"
sleep 1s
echo "
" >> tmp_in
sleep 3s
echo "开热加载......"
#遍历目录中的所有class文件
for file in ${updateClassPath}/*
do
    echo ${file}
    #如果file后缀是class 执行替换
    if [ "${file##*.}" = "class" ]; then
        #开始启动arthas项目
        echo "热加载${file}"
        echo "retransform ${file}" >> tmp_in
    fi
done
echo "quit" >> tmp_in

posted @   God-slayer  阅读(1546)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示