众所周知ava是静态的强类型语言,一旦编译好Class文件后,那程序就会按照编译好的Class文件执行,变量、 类型、逻辑将都无法更改。如果需要进行逻辑修改,我们必须要重新编码、编译、替换原始Class,并重新启动程序,这对于我们一般无状态服务是没有问题的,但是对于基础数据服务却是非常的麻烦。
以Solr Cloud为例,如果我们需要更新自定义的Solr分词器,在没有热部署的情况下,我们必须替换Solr集群的依赖Solr plugin jar,并重启整个集群。这样的话,就会非常的耗时,也存在风险。目前热部署里面比较成熟的框架ASM,我们今天暂时不讨论这个方向,为了研究Solr的热部署原理,我们从比较简单的ClassLoader出发。
所谓热部署就是要在程序运行的时候,我们需要替换其在内存里的Class文件(或者说Class对象),阅读java.lang.Classloader源码可以知道,Class对象由加载此Class文件的ClassLoader持有。如果想要替换内存中的Class对象,我们就必须知道Class是如何从磁盘加载至内存中的。
从loadClass方法可以知道,获取Class对象的过程:
- /**
- * Loads the class with the specified binary name. The
- * default implementation of this method searches for classes in the
- * following order:
- *
- *
- *
- *
Invoke {@link #findLoadedClass(String)} to check if the class
- * has already been loaded.
- *
- *
Invoke the {@link #loadClass(String) loadClass} method
- * on the parent class loader. If the parent is null the class
- * loader built-in to the virtual machine is used, instead.
- *
- *