spring boot入门之——热部署

场景:

  • 本地调试(频繁的启动/停止服务器)

  • 线上发布(每次都需要启动/停止服务器)


优点:

  • 无论本地还是线上,都适用

  • 无需重启服务器,提高开发、调试效率;提升发布、运维效率,降低运维成本

BaiduShurufa_2018-6-17_14-0-33.jpg

java实现热部署有哪几种方式?

  • 通过配置tomcat,直接把项目放在webapps里。

  • 在tomcat\conf\server.xml中的<host></host>中添加<context debug="0" docBase="" path="" privileged="true" reloadable="true"/>标签

  • 在tomcat\conf\Catalina\localhost中添加xml,关键属性<context docBase="" reloadable="true" />


BaiduShurufa_2018-6-17_14-0-33.jpg

实现一个Java类热加载的实例

//自定义类加载器,需实现findClass方法(核心类)
public class MyClassLoader extends ClassLoader {
	
	//定义加载的路径
	private String classPath;
	
	public MyClassLoader(String classPath) {
		//调用父类的加载器
		super(ClassLoader.getSystemClassLoader());
		this.classPath = classPath;
	}

	//1.重新findClass方法
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		byte[] b = this.loadData(name);
		return this.defineClass(name, b, 0, b.length);
	}

	//加载class文件中的内容
	private byte[] loadData(String name) {
		//.替换为//表示
		name = name.replaceAll(".", "//");
		try {
			FileInputStream is = new FileInputStream(new File(classPath + name + ".class"));
			ByteArrayOutputStream aos = new ByteArrayOutputStream();
			int b = 0;
			while((b = is.read())!=-1){
				aos.write(b);
			}
			is.close();
			aos.close();
			return aos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}




猜你喜欢

转载自blog.51cto.com/mazongfei/2141149