Best practice- share Spring-boot Service and Repo layer code between applications

Viewed 140

Need some best practice recommendations to a classic requirement around modularising Springboot application based on layers.

Some background info:

  • Small- medium size Spring boot project with less than 10 developers
  • 2 different Spring-boot applications and shared Service, Repo layer and also shared models
  • Bit too late to go with micro service approach with full Model/ Controller/ Service/ Repo per API.
  • Currently there is just one web application exposing the APIs for a frontend application.
  • Requirement is to add new set of APIs which are used for B2B integration, so the request/ response formats will be quite different to the already available APIs. i.e. /webapi/v1/orders for frontend client and /b2b/v1/orders will need to return different response format.
  • The Service and Repository layer along with the models need to be shared among the 2 applications, so 3 modules identified as similar to how it's explained in https://stackoverflow.com/a/50352532/907032
-- Main app   
   -- Webapi (Got dependency to common, jar packaging)
   -- b2b (Got dependency to common, jar packaging)
   -- common (jar packaging)
  • The two applications need to be deployed separately and also separated from CICD perspective not to build all the sub modules every time (A change to b2b controller should not affect common/ Webapi)
  • A change to common module which is only required to the latest b2b module, preferably should not trigger a build and deploy of webapi. i.e. webapi uses common-1.01 and b2b module uses common-1.02. Understood the new version common-1.02 should not break any feature from common-1.01 but just trying to save unnecessary build & deploy for that module until required if that makes sense.

The challenge

  • Should the modules defined in the same Repo or 3 different Repos?
  • All the talks about mono vs multi repo is about whether to keep all different projects in same or not, but here as you can see these are modules which are kind of related to each other.
  • If we define these as sub-modules in same Repo, how versioning of the common module handled? If it's always triggering a build of all three sub modules, do we even have any advantage of modularising the code?
1 Answers

As per your description, the module named "common" is not not that comon to the other two. I'd go with the multi-modudle way by doing so:

  • first break that common module in three: common, utils-webapi, utils-b2b

The first will strictly contains the thing both webapi and b2b need at the same version. Utils-webapi will be dedicated strictly to the things in api. Same goes for utils-b2b

  • B2b depends on utils-b2b with depends on common. Webapi depends on utils-webapi with depends on common

Versionning of common module is always consistant, only utils-X module version change from the X module perspective

CI is thus independant for each build.

Note: You can go further and simply consider utils-webapi utils-b2b and get rid of common. At the cost of some deduped code.

Related