Reputation: 1300
I've two micro services in a Spring Boot micro service architecture. Let's say...
Both services contains a domain model, of course in the end in separate JARs.
HotSpot Domain Model (HotSpot Service)
@Entity
public class HotSpot {
@Id
private Long id;
}
Attachment Domain Model (Attachment Service)
@Entity
public class Attachment {
@Id
private Long id;
}
If you create a new hot spot, it should be possible to add an additional attachment, like a describing image. So there should exist some kind of association/mapping between a hot spot entity and it's attachment. In a monolithic application, this would be implemented by annotating with JPA annotation @OneToOne
or something like this.
How can I achieve this in a micro service architecture? Both classes are in separate JAR/projects! I thought about storing just the identifier as Long
independently from JPA.
Any "better"/other ideas?
Upvotes: 2
Views: 581
Reputation: 1224
We have a semi-generic service (like the attachment service in your example) for Tagging. You could use a similar structure to what we use, something like this:
// Contains types of "things" that can accept attachments
// Probably unnecessary if you aren't multi-tenant
public class EntityType
{
public int Id;
public Guid TenantId;
public string Name;
}
// Contains a list of Entities available to Attach to
public class Entity
{
public int Id;
public int EntityTypeId;
public string EntityId; // The unique Identifier for this Instance of Entity
}
// Contains actual attachment values (URI's) assigned to an Entity instance
public class EntityAttachment
{
public int Id;
public int EntityId;
public string Attachment;
}
Even though the specific need is a little different, something like this might work well for you to build a generic Attachments
service that lets you attach to almost anything but still keep the data well normalized and fast to query.
We build the EntityType
and Entity
datasets on the fly - if someone wants to add a tag to a type or ID we don't have, we add it silently.
Upvotes: 0
Reputation: 79
First I would ask how are you dividing your micro services? It doesn't make sense to separate resources that are depending on each other. In your example I'm not sure if an Attachment can exists without a HotSpot. If it's not possible it definitively make no sense to have this two entities in two separate micro services.
Assumed your resources should be separated in two micro services, you need for each resource an URI. If resource A is related to resource B you could just store the URI of B in A, and if your mircroservices should be RESTful, provides a link in A to B with the right relation. Any automatic system like a OneToOne JPA relation doesn't exists.
Upvotes: 2