How to share java models between microservices in microservice architecture

You can create a separate project with common models, create a jar of this project and add dependency of this jar in other microservices.

But I have a practical experience, its a nightmare to maintain this common project, because for every change you have to create a new version and update the build scripts of all the microservices.

In my opinion we should not share models among the microservices.


You should only be sharing models that define the API of your micro-service e.g. Protobuff .proto files or the Java classes generated from them.

This is normally done by creating either a separate project or converting your micro-service projects into a multi-module projects where one of the modules is a thin API module with interface definition.

There is nothing wrong in sharing code between micro-services but you have to be careful. Share too much internal implementation details and you end up with a distributed monolith instead of micro-services.