Search code examples
javaspringmicroservices

Sharing code between multiple micro-services


I am developing three micro-services

  1. View offers
  2. Add offers(for administrator)
  3. Opt offers

View offers is used to show offers to customer and Add offer used by service admins to link offer to customer and if offer is linked to customer, view offer will return that offer in response. Opt offers is used by customer to avail the offer.

While designing, I realized that all 3 services share same functionality, but also have their own logic.

Should I use shared code while developing these API or each API must have its own code base?


Solution

  • If you are using maven then you can use Maven Modules. This basically means you can extract the service code into separate module and include that module as a pom dependency in all 3 microservices.

    Here's an example of how to do it using Spring. If you are not using Spring then you can have a look at another example here.