Spring Boot 如何熱加載Jar實(shí)現(xiàn)動(dòng)態(tài)插件?
本文轉(zhuǎn)載自微信公眾號(hào)「陶陶技術(shù)筆記」,作者zlt2000。轉(zhuǎn)載本文請(qǐng)聯(lián)系陶陶技術(shù)筆記公眾號(hào)。
一、背景
動(dòng)態(tài)插件化編程是一件很酷的事情,能實(shí)現(xiàn)業(yè)務(wù)功能的 「解耦」 便于維護(hù),另外也可以提升 「可擴(kuò)展性」 隨時(shí)可以在不停服務(wù)器的情況下擴(kuò)展功能,也具有非常好的 「開(kāi)放性」 除了自己的研發(fā)人員可以開(kāi)發(fā)功能之外,也能接納第三方開(kāi)發(fā)商按照規(guī)范開(kāi)發(fā)的插件。
常見(jiàn)的動(dòng)態(tài)插件的實(shí)現(xiàn)方式有 SPI、OSGI 等方案,由于脫離了 Spring IOC 的管理在插件中無(wú)法注入主程序的 Bean 對(duì)象,例如主程序中已經(jīng)集成了 Redis 但是在插件中無(wú)法使用。
本文主要介紹在 Spring Boot 工程中熱加載 jar 包并注冊(cè)成為 Bean 對(duì)象的一種實(shí)現(xiàn)思路,在動(dòng)態(tài)擴(kuò)展功能的同時(shí)支持在插件中注入主程序的 Bean 實(shí)現(xiàn)功能更強(qiáng)大的插件。
二、熱加載 jar 包
通過(guò)指定的鏈接或者路徑動(dòng)態(tài)加載 jar 包,可以使用 URLClassLoader 的 addURL 方法來(lái)實(shí)現(xiàn),樣例代碼如下:
「ClassLoaderUtil 類」
- public class ClassLoaderUtil {
 - public static ClassLoader getClassLoader(String url) {
 - try {
 - Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
 - if (!method.isAccessible()) {
 - method.setAccessible(true);
 - }
 - URLClassLoader classLoader = new URLClassLoader(new URL[]{}, ClassLoader.getSystemClassLoader());
 - method.invoke(classLoader, new URL(url));
 - return classLoader;
 - } catch (Exception e) {
 - log.error("getClassLoader-error", e);
 - return null;
 - }
 - }
 - }
 
其中在創(chuàng)建 URLClassLoader 時(shí),指定當(dāng)前系統(tǒng)的 ClassLoader 為父類加載器 ClassLoader.getSystemClassLoader() 這步比較關(guān)鍵,用于打通主程序與插件之間的 ClassLoader ,解決把插件注冊(cè)進(jìn) IOC 時(shí)的各種 ClassNotFoundException 問(wèn)題。
三、動(dòng)態(tài)注冊(cè) Bean
將插件 jar 中加載的實(shí)現(xiàn)類注冊(cè)到 Spring 的 IOC 中,同時(shí)也會(huì)將 IOC 中已有的 Bean 注入進(jìn)插件中;分別在程序啟動(dòng)時(shí)和運(yùn)行時(shí)兩種場(chǎng)景下的實(shí)現(xiàn)方式。
3.1. 啟動(dòng)時(shí)注冊(cè)
使用 ImportBeanDefinitionRegistrar 實(shí)現(xiàn)在 Spring Boot 啟動(dòng)時(shí)動(dòng)態(tài)注冊(cè)插件的 Bean,樣例代碼如下:「PluginImportBeanDefinitionRegistrar 類」
- public class PluginImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
 - private final String targetUrl = "file:/D:/SpringBootPluginTest/plugins/plugin-impl-0.0.1-SNAPSHOT.jar";
 - private final String pluginClass = "com.plugin.impl.PluginImpl";
 - @SneakyThrows
 - @Override
 - public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
 - ClassLoader classLoader = ClassLoaderUtil.getClassLoader(targetUrl);
 - Class<?> clazz = classLoader.loadClass(pluginClass);
 - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
 - BeanDefinition beanDefinition = builder.getBeanDefinition();
 - registry.registerBeanDefinition(clazz.getName(), beanDefinition);
 - }
 - }
 
3.2. 運(yùn)行時(shí)注冊(cè)
程序運(yùn)行時(shí)動(dòng)態(tài)注冊(cè)插件的 Bean 通過(guò)使用 ApplicationContext 對(duì)象來(lái)實(shí)現(xiàn),樣例代碼如下:
- @GetMapping("/reload")
 - public Object reload() throws ClassNotFoundException {
 - ClassLoader classLoader = ClassLoaderUtil.getClassLoader(targetUrl);
 - Class<?> clazz = classLoader.loadClass(pluginClass);
 - springUtil.registerBean(clazz.getName(), clazz);
 - PluginInterface plugin = (PluginInterface)springUtil.getBean(clazz.getName());
 - return plugin.sayHello("test reload");
 - }
 
「SpringUtil 類」
- @Component
 - public class SpringUtil implements ApplicationContextAware {
 - private DefaultListableBeanFactory defaultListableBeanFactory;
 - private ApplicationContext applicationContext;
 - @Override
 - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 - this.applicationContext = applicationContext;
 - ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
 - this.defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
 - }
 - public void registerBean(String beanName, Class<?> clazz) {
 - BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
 - defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getRawBeanDefinition());
 - }
 - public Object getBean(String name) {
 - return applicationContext.getBean(name);
 - }
 - }
 
四、總結(jié)
本文介紹的插件化實(shí)現(xiàn)思路通過(guò) 「共用 ClassLoader」 和 「動(dòng)態(tài)注冊(cè) Bean」 的方式,打通了插件與主程序之間的類加載器和 Spring 容器,使得可以非常方便的實(shí)現(xiàn)插件與插件之間和插件與主程序之間的 「類交互」,例如在插件中注入主程序的 Redis、DataSource、調(diào)用遠(yuǎn)程 Dubbo 接口等等。
但是由于沒(méi)有對(duì)插件之間的 ClassLoader 進(jìn)行 「隔離」 也可能會(huì)存在如類沖突、版本沖突等問(wèn)題;并且由于 ClassLoader 中的 Class 對(duì)象無(wú)法銷毀,所以除非修改類名或者類路徑,不然插件中已加載到 ClassLoader 的類是沒(méi)辦法動(dòng)態(tài)修改的。
所以本方案比較適合插件數(shù)據(jù)量不會(huì)太多、具有較好的開(kāi)發(fā)規(guī)范、插件經(jīng)過(guò)測(cè)試后才能上線或發(fā)布的場(chǎng)景。
五、完整 demo
https://github.com/zlt2000/springs-boot-plugin-test















 
 
 













 
 
 
 