基于arthas热更新class脚本(arthas classloader)

  本篇文章为你整理了基于arthas热更新class脚本(arthas classloader)的详细内容,包含有arthas热部署 arthas classloader arthas热替换 基于arthas热更新class脚本,希望能帮助你了解 基于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" ]

   mkdir ./arthas-hot-swap

   echo "******* mkdir ./arthas-hot-swap success"

   rm -rf ./arthas-hot-swap

   mkdir ./arthas-hot-swap

   echo "******* ./arthas-hot-swap exists, delete the directory first, and then create a new one"

  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 "

   echo "******* openssl is not installed, and installation were start next "

   sudo yum install openssl openssl-devel

  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} == ]]

   curl -L https://arthas.aliyun.com/install.sh sh

   arthas_start_cmd=./as.sh

   curl -O https://arthas.aliyun.com/arthas-boot.jar

   arthas_start_cmd=${specifyJavaHome}" -jar arthas-boot.jar"

  selectJavaProcessName=%[selectJavaProcessName]

  if [[ ${selectJavaProcessName} != ]]

   arthas_start_cmd=${arthas_start_cmd}" --select "${selectJavaProcessName}

  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 != "" ]]

  echo

  ****************************************** 9. The following files were successfully hot deployed *************************** ****************

  *****

  ***** FrameController.class

  *****

  ********************************************************************************************************************************************

  echo

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 9. Failed to hot deployed the following files %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  %%%%%

  %%%%% FrameController.class

  %%%%%

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  cat $(pwd)/arthas-hot-swap-result

  

 

  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}/*

   echo ${file}

   #如果file后缀是class 执行替换

   if [ "${file##*.}" = "class" ]; then

   #开始启动arthas项目

   echo "热加载${file}"

   echo "retransform ${file}" tmp_in

  echo "quit" tmp_in

  

 

  以上就是基于arthas热更新class脚本(arthas classloader)的详细内容,想要了解更多 基于arthas热更新class脚本的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: