Spring Boot – 在运行时或编译时有条件地加载模块

huangapple go评论70阅读模式
英文:

Spring Boot - Conditionally load module during runtime or compile-time

问题

首先,我对Spring Boot还不熟悉,所以不确定在该框架内是否可以实现类似这样的功能。

让我描述一下我的问题。

我有10个代码仓库。每个仓库都监听数据流,解析数据并更新数据库。由于可维护性问题,我计划将它们合并到一个单独的代码仓库下。这个新应用将会被泛化,某些特定于应用的配置(例如,我应连接到哪个数据流,数据库主机是什么)将在运行时获取。

从理论上讲,这将允许我维护单一的代码库,但根据配置将其部署为10个单独的服务,这正是我所需要的。然而,有一组Java类是特定于应用程序的,用于解析检索到的数据。要更好地理解这一点,请参考下面的图表。

(插入图片)

理想情况下,我仍然需要将这些类保留在同一个仓库中,但作为单独的模块。一旦加载了配置,应用程序应能够将相应的模块加载到应用程序上下文中,并初始化这些Java类。其他模块将不会被使用。

我能在Spring Boot中实现类似的功能吗?或者,即使是在构建时的解决方案,如果我能够创建可以单独部署的单独构建,也是可以接受的。

英文:

First of all, I am new to Spring Boot so I am not sure if something like this is possible within the framework.

Let me describe my problem.

I have 10 code repositories. Each repository listens to a data stream, parses the data and updates a database. Due to maintainability issues I plan on bringing it under a single code repository. This new application will be generalized, and certain app specific configurations (for example, which stream should I connect to, the database host) will be retrieved at run-time.

Theoretically, this would allow me to maintain a single code base, but deploy it as 10 separate services based on configurations which is what I need. However, there's a set of java classes that are application specific used to parse the retrieved data. To better understand this refer the diagram below.

Spring Boot – 在运行时或编译时有条件地加载模块

Ideally, I need to still maintain these classes in the same repository, but as separate modules. Once the configurations are loaded, the app should be able to load the corresponding module into the application context and initialize the Java classes. The other modules will not be used.

Can I do something like this with Spring Boot? Alternately, even a build time solution is fine if I can create separate builds which can then be deployed separately.

答案1

得分: 1

我不确定我是否理解得很好,但为什么不尝试使用 Spring Profile(https://www.baeldung.com/spring-profiles)。您可以为每个服务设置不同的配置,并且在运行时通过 spring.profiles.active 来指定使用哪个配置。

此外,类似这样的内容可能也会有用:https://www.baeldung.com/spring-reloading-properties。

英文:

Not sure did I understood it well but why don't try spring profile (https://www.baeldung.com/spring-profiles). You can set for every service different config and with spring.profiles.active in runtime say what configuration will use.

Also something like this could be useful https://www.baeldung.com/spring-reloading-properties

huangapple
  • 本文由 发表于 2020年9月16日 13:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63913939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定