Spring-Loaded实现热更新

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.简介      

Spring-Loaded是基于javaAgent实现的一个代理,更多javaAgent可以参考:
java代理-javassist:http://my.oschina.net/OutOfMemory/blog/309283

官网:https://github.com/spring-projects/spring-loaded
当前的最新版:1.2.4.RELEASE

2.说明

目前最新版1.2.4.RELEASE实现了监听jar包的更新的机制,实现热更新jar包,目前官方提供的版本在linux上可以很好的运行,但在windows还存在bug,官网已经有人提出:https://github.com/spring-projects/spring-loaded/issues/145。如果想在windows上测试可以自己手动改一下源码。

3.参数

-Dspringloaded=watchJars=main.jar:other.jar
即watchJars选项,监听的多个jar之前用:分离

4.实例

以win为例,这里需要手动改一下源码,编译一下
测试目录:
Spring-Loaded实现热更新_第1张图片
lib:springloaded-1.2.4.RELEASE.jar
main-0.0.1-SNAPSHOT.jar测试的jar包,一般都是游戏逻辑jar包,测试

package org.main;

import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 * 
 */
public class App {
	public static void main(String[] args) throws InterruptedException {
		Reload reload = new Reload();
		while (true) {
			reload.load();
			
			TimeUnit.SECONDS.sleep(2);
		}
	}
}
package org.main;

public class Reload {
	public void load() {
		System.out.println("load.....");
	}
}
run.bat:


java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.1-SNAPSHOT.jar
pause
运行run.bat
D:\springloaded\win>java -javaagent:lib/springloaded-1.2.4.RELEASE.jar -noverify
 -Dspringloaded=verbose;explain;watchJars=main-0.0.1-SNAPSHOT.jar -jar main-0.0.
1-SNAPSHOT.jar
SL: [verbose mode on] Full configuration is:verbose;explain;watchJars=main-0.0.1
-SNAPSHOT.jar
SL: [explain mode on] Reporting on the decision making process within SpringLoad
ed
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor logPre
Process
信息: SpringLoaded preprocessing: classname=org/main/App classloader=Launcher$Ap
pClassLoader typeRegistry=TypeRegistry(id=1325628529,loader=sun.misc.Launcher$Ap
pClassLoader)
====remainingPrefix=====jar:file:/D:/springloaded/win/main-0.0.1-SNAPSHOT.jar===
=jarname=====main-0.0.1-SNAPSHOT.jar
2015-9-16 16:55:32 org.springsource.loaded.agent.SpringLoadedPreProcessor prePro
cess
信息: [explanation] Based on the name, type org/main/App is considered to be rel
oadable
2015-9-16 16:55:32 org.springsource.loaded.ReloadableType 
信息: New reloadable type: org.main.App (allocatedId=0) TypeRegistry(id=13256285
29,loader=sun.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.agent.Watcher addFile
信息: Now watching d:\springloaded\win\main-0.0.1-SNAPSHOT.jar
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=0)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: 
信息: New reloadable type: org.main.Reload (allocatedId=1) TypeRegistry(id=13256
28529,loader=sun.misc.Launcher$AppClassLoader)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: >TypeRegistry.getReloadableType(typeRegistryId=0,typeId=1)
2015-9-16 16:55:32 org.springsource.loaded.TypeRegistry getReloadableType
信息: 

修改jar中的类文件,将Reload中的load....改成reload....  重新打包,并覆盖当前的jar,观察日志:

2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher run
信息: Observed last modification time change for d:\springloaded\win\main-0.0.1-
SNAPSHOT.jar (lastScanTime=1442393851501)
2015-9-16 16:57:32 org.springsource.loaded.agent.Watcher 
......省略
reload.....
reload.....

热更新完成!

参考:http://www.blogjava.net/landon/archive/2015/07/01/425994.html

转载于:https://my.oschina.net/OutOfMemory/blog/506945

你可能感兴趣的:(Spring-Loaded实现热更新)