最佳实践:使用Maven和Spring Boot进行多API项目开发

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

Best practices with multi API project using Maven and Spring Boot

问题

我正在处理一个项目,需要开发两个连接到同一个数据库的独立API。

管理API

这是一个内部的、非公开的API,其中包含管理多个资源的端点。当然,这些资源是持久化在数据库中的。

公共API

另一个是公共API,其中包含列出上述某些资源的端点,并执行不在数据库中持久化的操作。

直到现在,我通常会使用Spring Initilizr创建一个Maven Spring Boot项目,带上所需的依赖,然后开始开发... 正如你所想象的,这将导致一个没有任何分离的大型单体项目,随着时间的推移不必要地增加技术债务。

对于这种情况,什么是最佳解决方案?

类似于这样的结构:

  • projectname-data:数据层。包含数据库模型和接口
  • projectname-management-api:管理API,依赖于projectname-data
  • projectname-public-api:公共API,依赖于projectname-data
英文:

I am working on a project that requires the development of two separate APIs that connect to the same database.

Management API

This is an internal, non-public API that contains endpoints to manage multiple resources. Of course, these resources are persisted in the database.

Public API

The other is a public API that contains endpoints to list some of the above resources and execute operations that do not persist in the database.

Up until now, I would just create a Maven Spring Boot project using Spring Initilizr with the dependencies needed and just start developing... As you can imagine this would result in having a big monolithic project with no separation whatsoever, increasing the technical debt overtime unnecessarily.

What would be the best solution for this case?

Something like this:

  • projectname-data: The data layer. Contains DB models and interfaces
  • projectname-management-api: The Management API, that uses projectname-data as a dependency
  • projectname-public-api: The Public API, that uses projectname-data as a dependency

答案1

得分: 1

除非你真的需要,否则我不会从一开始就将服务划分为更多的服务。
只需拥有单独的控制器,并且将不同的访问/控制要求应用于每个控制器,这样你就可以开始了。

会让你开始考虑拆分它们的事情...根据你所解释的,基本上是扩展。如果公共API的扩展能力比管理API要强得多,你可能会开始考虑使其更轻,并且不会随之扩展管理API。与访问相关的严格限制也可能是一个问题。

但是,除非是这种情况并且你从一开始就清楚了,否则不要为了这么简单的事情从一开始就付出拥有多个服务的代价。除非你很匆忙,一旦你有了适当的基础,将来就应该有时间和更清晰的视角来决定如何划分它们。

“额外的服务”并不是免费赠送给你的。

就是我的看法 最佳实践:使用Maven和Spring Boot进行多API项目开发
干杯!
附言:我们拥有大量的此类服务、内部工具,到目前为止,没有必要去划分它们。我在其他地方咨询过,我们从一开始就开始划分,但不是针对内部工具,然后我们将不直接共享相同的数据库。

英文:

Unless you really need it, I wouldn't start dividing the service in more services from the very beginning.
Simply having a separate controller and different access/control requirements applied to each controller should get you started.

Things that would make you start thinking about splitting them... from what you explained, basically scaling. If the public API has to be able to scale much more than the management one, you might start thinking about making it lighter and not scaling the management along with it. Strong restrictions related to access might be an issue as well.

But unless that's the case and you have it clear from the very beginning, don't pay the price of having multiple services from the very beginning for such a simple thing. Unless you are in a nut rush, once you have the proper foundations, you should have time and a cleaner view about how to divide them in the future.

"Extra services" is not something you are given for free.

That's my 2ec ;).
Cheers!
PD: We have plenty of such services, internal tools, and so far, no need to divide any of them. I've consulted in other places where we started dividing from the very beginning but not for internal tools, and then we would not share the same DB directly.

huangapple
  • 本文由 发表于 2020年4月7日 19:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61079109.html
匿名

发表评论

匿名网友

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

确定