Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share java models between microservices in microservice architecture

I am designing the architecture of my new app.I chose microservice architecture.In my architecture I noticed that I have models that are used by diffrent microservices. I want to know if there is a way to share models code between microservices instaed of writing them in each microservice.

By the way I am using the spring boot framework for my app.

like image 814
fatma Avatar asked Mar 28 '19 09:03

fatma


People also ask

How can 2 microservices share common models between each other?

The whole point of microservices is that they can change and scale independently. Sharing those models will force those services to iterate together, and will enforce strong coupling (bad). To deal with shared domains in a microservice architecture, keep you binding to a minimum.

How do you communicate between microservices in Java?

The synchronous call is the simplest way to communicate two services. It also bonds them together, since the calling microservice needs to wait for a response from remote. This kind of coupling can sometimes be prevented by using asynchronous communication.

How do you share resources between microservices?

Using messaging to exchange lightweight data structures, often via a message broker that manages sessions and data queues. Via a shared data store, where the service might not communicate directly, but share a common source of information. Exchanging RESTful data, similar to the way they communicate with clients.


2 Answers

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.

like image 196
Karol Dowbecki Avatar answered Oct 06 '22 08:10

Karol Dowbecki


In a Microservices architecture, each one is absolutely independent of the others and it must hide the details of the internal implementation.

If you share the model you are coupling microservices and lose one of the greatest advantages in which each team can develop its microservice without restrictions and the need of knowing how evolve others microservices. Remember that you can even use different languages in each one, this would be difficult if you start to couple microservices.

https://softwareengineering.stackexchange.com/questions/290922/shared-domain-model-between-different-microservices

like image 24
Mebin Joe Avatar answered Oct 06 '22 06:10

Mebin Joe