opensolon / solon

🔥 Java "new" application development Framework: Restrained, concise, EFFICIENT, OPEN, ECOLOGICAL!! 300% higher concurrency 50% memory savings Startup is 10 times faster. Packing 90% smaller; Compatible with java8 ~ java22.
https://solon.noear.org
Apache License 2.0
2.22k stars 221 forks source link

希望插件按需加载 #266

Closed juqkai closed 1 month ago

juqkai commented 3 months ago

背景 现在有一个项目,是一个普通的java程序,但希望能够管理各个服务对象。这个程序有一个子服务,这个子服务提供web能力,但是不一定会程序启动时就把web服务启动起来,而是根据业务情况才把web开启。

现状 经过研究发现,现在的web服务能力是基于插件实现的,一共有两种插件模式,一种是进程启动后会扫描所有的jar,然后启动所有的插件。另外一种是热加载。 但是,我的web jar包是直接在pom中做的依赖,因此会在程序启动时第一时间就会加载与运行。 而使用热加载的话,我需要把依赖单独给扣出来。

解决建议

  1. 我发现主程序里面的插件的启动其实与热加载非常类似,感觉是可以把 PluginManager 做为插件的唯一管理入口。
  2. PluginManager 中使用了 Map 管理所有插件,对于主程序启动时,应该可以把插件的名称做为 KEY 进行管理
  3. 有key了 PluginManager 中就可以添加开机启动的过滤器,也可以对任意插件做按需启停

临时解决方案

  1. 设置 Solon.app().enableHttp(false)
  2. 需要按需求启停web插件时,遍历cfg().plugs()找到web插件,然后再启停
noear commented 3 months ago

目前 jdkhttp, jlhttp, smarthttp 适配插件里,带了一个 HttpServer 封装。以 smarthttp 为例:

控制 http 端口启停(极少有需求会用到)

  1. 关掉 http 启用(就是关掉,自动启动)
@SolonMain
public class SeverDemo {
    public static void main(String[] args) {
        Solon.start(SeverDemo.class, args, app -> {
            app.enableHttp(false);
        });
    }
}
  1. 使用 SmHttpServer 类(细节看类里的接口)
SmHttpServer server = new SmHttpServer();

//启动
server.start(null, 8080);

//停止
server.stop();

希望能帮助到你:)

noear commented 3 months ago

另外 “热加载”,很复杂很麻烦。。。没法跟普通的插件,并提。