JAVA代碼熱部署,在線不停服動態更新


本地debug的時候,可以實時編譯並更新代碼,線上也可以不停服來動態更新類,即所說的java熱部署。

 
JDK代理的兩種方式:
1.premain方式是Java SE5開始就提供的代理方式,但其必須在命令行指定代理jar,並且代理類必須 在main方法前啟動,它要求開發者在應用啟動前就必須確認代理的處理邏輯和參數內容等等
2.agentmain方式是JavaSE6開始提供,它可以在應用程序的 VM啟動后再動態添加代理的方式
 
agentmain應用場景:
比如正常的生產環境下,一般不會開啟代理功能,但是在發生問題時,我們不希望停止應用就能夠動態的去修改一些類的行為,以幫助排查問題,這在應用啟動前是無法確定的
 
與Permain類似,agent方式同樣需要提供一個agent jar,並且這個jar需要滿足[可查看 附件的jar文件]:
1.在manifest中指定Agent-Class屬性,值為代理類全路徑
2.代理類需要提供public static void agentmain(String args, Instrumentation inst)或public static void agentmain(String args)方法。並且再二者同時存在時以前者優先。args和inst和premain中的一致。
 
那如何在不停服的情況下動態修改類呢??
實現代碼如下:
 Java Code 代碼動態更改類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
public  class JavaAgent {

     public  static  final Logger logger = LoggerFactory.getLogger(JavaAgent. class);

     private  static  String classesPath;
     private  static  String jarPath;
     private  static VirtualMachine vm;
     private  static  String pid;

     static {
        classesPath = JavaAgent. class.getClassLoader().getResource( "").getPath();
        logger.error( "java agent:classpath:{}", classesPath);
        jarPath = getJarPath();
        logger.error( "java agent:jarPath:{}", jarPath);

         // 當前進程pid
         String name = ManagementFactory.getRuntimeMXBean().getName();
        pid = name.split( "@")[ 0];
        logger.error( "當前進程pid:{}", pid);
    }

     /**
     * 獲取jar包路徑
     * @return
     */

     public  static  String getJarPath() {
         // StringUtils是jar文件內容
        URL url = StringUtils. class.getProtectionDomain().getCodeSource().getLocation();
         String filePath = null;
         try {
            filePath = URLDecoder.decode(url.getPath(),  "utf-8"); // 轉化為utf-8編碼
        }  catch (Exception e) {
            e.printStackTrace();
        }
         if (filePath.endsWith( ".jar")) { // 可執行jar包運行的結果里包含".jar"
             // 截取路徑中的jar包名
            filePath = filePath.substring( 0, filePath.lastIndexOf( "/") +  1);
        }

        File file =  new File(filePath);

        filePath = file.getAbsolutePath(); //得到windows下的正確路徑
         return filePath;
    }

     private  static  void init()  throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
         // 虛擬機加載
        vm = VirtualMachine.attach(pid);
        vm.loadAgent(jarPath +  "/javaagent.jar");

        Instrumentation instrumentation = JavaDynAgent.getInstrumentation();
        Preconditions.checkNotNull(instrumentation,  "initInstrumentation must not be null");
    }

     private  static  void destroy()  throws IOException {
         if (vm != null) {
            vm.detach();
        }
    }

     /**
     * 重新加載類
     *
     * @param classArr
     * @throws Exception
     */

     public  static  void javaAgent( String root,  String[] classArr)  throws ClassNotFoundException, IOException, UnmodifiableClassException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
        init();

         try {
             // 1.整理需要重定義的類
            List<ClassDefinition> classDefList =  new ArrayList<ClassDefinition>();
             for ( String className : classArr) {
                Class<?> c = Class.forName(className);
                 String classPath = (StringUtils.isNotBlank(root) ? root : classesPath) + className.replace( ".""/") +  ".class";
                logger.error( "class redefined:" + classPath);
                 byte[] bytesFromFile = Files.toByteArray( new File(classPath));
                ClassDefinition classDefinition =  new ClassDefinition(c, bytesFromFile);
                classDefList.add(classDefinition);
            }
             // 2.redefine
            JavaDynAgent.getInstrumentation().redefineClasses(classDefList.toArray( new ClassDefinition[classDefList.size()]));
        }  finally {
            destroy();
        }
    }

     public  static  void main( String[] args)  throws Exception {
        PortUtil.test();

        javaAgent(null,  new  String[] { "com.agileeagle.webgame.framework.util.PortUtil"});

        PortUtil.test();
    }
}
 
 Java Code  Instrumentation 如何獲取?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
public  class JavaDynAgent {
     private  static Instrumentation instrumentation;
     private  static Object lockObject =  new Object();

     public JavaDynAgent() {
    }

     public  static  void agentmain( String args, Instrumentation inst) {
        Object var2 = lockObject;
         synchronized(lockObject) {
             if(instrumentation == null) {
                instrumentation = inst;
                System.out.println( "0->" + inst);
            }  else {
                System.out.println( "1->" + inst);
            }

        }
    }

     public  static Instrumentation getInstrumentation() {
         return instrumentation;
    }
}
 
實現原理是:
1.綁定pid獲得虛擬機對象,然后通過虛擬機加載代理jar包,這樣就調用到 agentmain,獲取得到Instrumentation
2.基於 Instrumentation接口可以實現JDK的代理機制,從而實現對類進行動態重新定義。
 
注意:com.sun.tools.attach. VirtualMachine的jar包是 jdk下lib中的tools.jar,所以項目中要引用到這個jar包,而且因為涉及到底層虛擬機,windows和linux機器這個jar不同
 
因此, 整個流程就是:
1.項目中引用 jdk/lib/tools.jar,否則無法使用VirtualMachine類
2.項目中引用 javaagent.jar ,它提供了agentmain接口
3.代碼實現動態增加JDK代理
新的原理解析說明: Java線上解決方案1:JAVA熱更新
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM