Solon

solon.hotplug

v2.7.5 </> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.hotplug</artifactId>
</dependency>

1、描述

基础扩展插件,提供业务插件的 '热插拔' 和 '热管理' 支持。(常规情况,使用普通的体外扩展机制E-Spi即可)。

所谓'热':即更新扩展包后不需要重启主程序,通过接口或界面进行管理;但开发也会有一些新制约。

需要让热插拔的扩展包,尽量领域独立,尽量不与别人交互,要让一些资源能“拔”掉。

建议结合 DamiBus 一起使用,它能帮助解耦。

2、热插拔

这是基础接口,但一般不直接使用。而使用管理接口。

public class DemoApp {
    public static void main(String[] args) {
        Solon.start(Test5App.class, args);

        File jarFile = new File("/xxx/xxx.jar");

        //加载插件并启动
        PluginPackage jarPlugin = PluginManager.loadJar(jarFile).start();
        
        //卸载插件
        PluginManager.unloadJar(jarPlugin);
    }
}

3、热管理示例

  • 配置管理的插件
solon.hotplug:
  add1: "/x/x/x.jar" #格式 name: jarfile
  add2: "/x/x/x2.jar"

也可以通过代码添加待管理插件(还可以,通过数据库进行管理;进而平台化)

PluginManager.add("add1", "/x/x/x.jar");
PluginManager.add("add2", "/x/x/x2.jar");
//PluginManager.remove("add2");//移除插件
  • 管理插件
//PluginManager.load("add2"); //加载插件
//PluginManager.start("add2"); //启动插件(未加载的话,自动加载)
//PluginManager.stop("add2"); //停止插件
//PluginManager.unload("add2"); //卸载插件(未停止的话,自动停止)

public class App {
    public static void main(String[] args) {
        Solon.start(App.class, args, app -> {
            //启动插件
            app.get("start", ctx -> {
                PluginManager.start("add1");
                ctx.output("OK");
            });

            //停止插件
            app.get("stop", ctx -> {
                PluginManager.stop("add1");
                ctx.output("OK");
            });
        });
    }
}

4、注意事项

  • 插件包名需独立性(避免描扫时扫到别人)
    • 例主程包为:xxxxxx.main
    • 插件1包为:xxx.add1
    • 插件2包为:xxx.add2
  • 依赖包的放置
    • 一般公共的放到主程序包(可以让插件包,更小)
    • 如果需要隔离的放到插件包
  • 如何获取主程序资源 ?????????
    • 通过 Solon.context().getBean() 获取主程序的Bean
    • 通过 Solon.cfg() 获取主程序的配置

5、插件代码示例

相对于普通的插件,要在 preStop 或 stop 时移除注册的相关资源。这很重要!

public class Plugin1Impl implements Plugin {
    AppContext context;
    StaticRepository staticRepository;

    @Override
    public void start(AppContext context) {
        this.context = context;

        //扫描自己的组件
        this.context.beanScan(Plugin1Impl.class);

        //添加自己的静态文件
        staticRepository = new ClassPathStaticRepository(context.getClassLoader(), "plugin1_static");
        StaticMappings.add("/", staticRepository);
    }

    @Override
    public void stop() throws Throwable {  
        //移除http处理。//用前缀,方便移除
        Solon.app().router().remove("/user");

        //移除定时任务
        JobManager.remove("job1");

        //移除事件订阅
        context.beanForeach(bw -> {
            if (bw.raw() instanceof EventListener) {
                EventBus.unsubscribe(bw.raw());
            }
        });

        //移除静态文件仓库
        StaticMappings.remove(staticRepository);
    }
}

6、具体的演示项目