15

I want to implement plugin architecture in Spring Boot application. Let me explain my scenario. I have a main application, which starts the server, manages security, etc. The app is like the root of my final product which will include this root app and other plugins added to it.

Now, the plugins are Spring Boot application themselves, which I may add to the root app by dynamically searching for jars in the specific path or by added them to project dependency as library.

Plugins have their own configurations and are like apps running inside the main root app. Let's say if the root app runs the server, the plugin app may have all the controllers (endpoints), beans etc that provide functionality to my product.

This is the premise, now what I want to know is,

  1. How can I achieve this architecture?
  2. How will the root app communicate with the plugins?
  3. Will they have separate application contexts?
  4. How can I boot and configure child app from the root app?
  5. When the application receives the request from clients how can I route the request to specific controller inside specific plugin considering I may have many plugins.

I am confused about the concept here, and how it can work. Any sort of help is appreciated. If there is some example that anyone can provide, that will be just wonderful.

4

3 回答 3

8

这个帖子是3年前的。但是,我想为正在寻找类似情况的解决方案的人回答这个问题。看来 pf4j 是一个适合你的插件框架。除了支持原生应用外,它还有spring-pf4j,所以你可以在spring中使用它。

网址:https ://pf4j.org

于 2018-07-06T05:42:37.593 回答
5

就像Java动态加载插件中描述的那样,你有两个选项:

  1. 采用 OSGi 方式,将您的所有问题都考虑在内,但与 Spring boot 结合可能有点棘手
  2. 使用服务加载器

至少对于第二种方法,每个 jar 文件都应该实现相同的接口,您可以使用该接口来注册 jar 文件的内容(类似于 OSGi 包的启动方法)。通过这种方式,您可以分离每个 jar 文件的应用程序上下文,并仅在启动时使其可用(例如,您可以创建一个上下文层次结构,在其中将您添加的 jar 的上下文添加到根上下文中)。

您的最后一点可能是一个棘手的问题,因为您必须考虑可以有多个服务可以满足相同的请求。再次借鉴 OSGi,这些服务通常是通过一个通用接口定义的,并且实现具有类似优先级的东西,如果有多个服务,则表明应该使用哪个服务。当然,您可以定义其他方法来选择其中一种。

于 2015-10-02T06:13:02.750 回答
1

有两种可能的选择:

  1. 使用 spring-plugin 您可以实现 OSGi 类型的功能。 https://github.com/spring-projects/spring-plugin
  2. 使用 mirco-boot,它将使用 spring boot 后端和 mircoserver 前端。它还根据您的要求提供插件支持。您可以探索 https://github.com/aol/micro-server/tree/master/micro-boot
于 2017-12-12T12:11:05.913 回答