Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microservices shared objects

Currently we are building a microservice oriented application from our monolith.

The issue we are facing is how to send and receive objects between services while keeping the services independent.

Problem: Say we have an EventService and a NotificationService. The EventService generates an Event, serializes it (json/java?) and publishes the serialized object on an ActiveMQ topic. The NotificationService, which is listening to that specific topic, receives the serialized object and has to deserialize it and therefore needs to know the Event-Class.

What is a good approach to solve the problem of sharing the Event-Class between the two services? Should I introduce a new Project which contains only the Event-Class? The Problem I see here is that this could lead to two scenarios:

  1. The shared project gets bloated up and will contain more classes than each service including it will need.
  2. I will have an extra project for every two services that share some type of objects which results in a massive overhead with a growing number of services.
like image 882
ayguenh92 Avatar asked Mar 21 '26 00:03

ayguenh92


1 Answers

Follow this rule of thumb:

For enterprise applications and complex objects, create a 3'rd project (an API project), and share it among your services as a dependency.

For simple and self-descriptive objects, use 'copies' of the same objects in each one of your services; note that this is powerful since the POJOs don't need to be identical;

I just shared sample code here

like image 77
Naor Bar Avatar answered Mar 22 '26 13:03

Naor Bar