Search code examples
javahibernatemicroservicespackaging

packing common code within a microservice application


I am about to start work on a micro service project, with a shared postgres database, accessed by hibernate. I am interested in . what is the best way of packing up code base.

Several microservices are going to access the database which may result in one or more microservice wanting access to the same java DTO object.

I can either package all the DTO objects to common jar and give microservice access to the jar or I share the different DTO between the different Microservices which could result in multiple instances of the same DTO but will keep the individual microservice codebase pure.

My normal method of preference would be to build a common jar file to code down on the mount of code that must be maintained.

Are there any schools of through on this issue?

Along the same issues there are common constants which also need to be shared


Solution

  • Microservices do not share databases, or more generally:

    Microservices do not share a common state.

    There's three possible scenarios:

    one: You try to split up one small use case. Then you'd need only one microservice.

    two: The data management is a separate concern. Then you should create a distinct service for handling the data. That service would then be used by your other services.

    three: You're trying to manage several distinct use cases in one database. Then you'd have a dedicated database for each of your services.

    Actually, even when doing two you might still also do three, as your microservices should remain functional (responsive and at least partly useful) even when the service used by all of them is down for some reason.

    Regarding the common constants: They probably need to be redundantly packed into each Micrososervice. You can share a common library (read: maven artifact) though.