Architecture for communication between two microservices

青春壹個敷衍的年華 提交于 2020-01-06 16:18:20

问题


First of all, I'm using JHipster 4.0.x for my project. I'm using microservices architecture.

For this example, I've got one gateway, one microservice for "Store" and a second one for "Skill". I want to centralise all Stores in one database and Skills in a second one.

Each one is a data repository that will not evolve at the same speed. On the other hand, they will be the central point of my infrastructure in order to update, via an ESB, the other software.

Jhipster is great for that, I've got easily my CRUD for each service.

The tricky point is a Store is defined by a Skill. The simplest way would be to say that I only do one service with "Skill" and "Store". But I can't do that because "Skill" must also be a central point for other data.

I imagined this Entity's architecture

[SKILL]

[STORE]----many-to-one----[LINK_WITH_OTHER_ENTITIES]

(with *.json generate by jhipster):

  • on Skill Service :

    • an entity Skill

    { "fluentMethods": true, "relationships": [], "fields": [ { "fieldName": "code", "fieldType": "String" }, { "fieldName": "libelle", "fieldType": "String" } ], "changelogDate": "20161201084915", "dto": "no", "service": "no", "entityTableName": "filiere_metier", "pagination": "no", "microserviceName": "metiers", "searchEngine": "elasticsearch" }

  • on Store Service :

    • an entity Store

    { "fluentMethods": true, "relationships": [ { "relationshipName": "categorie_metier", "otherEntityName": "pont_msvc", "relationshipType": "many-to-one", "otherEntityField": "id" } ], "fields": [ { "fieldName": "code", "fieldType": "String" }, { "fieldName": "lib", "fieldType": "String" } ], "changelogDate": "20161125141916", "dto": "no", "service": "no", "entityTableName": "store", "pagination": "no", "microserviceName": "store", "searchEngine": "elasticsearch" }

    • an Entity to make a link with Skill

    { "fluentMethods": true, "relationships": [], "fields": [ { "fieldName": "idext", "fieldType": "String" }, { "fieldName": "msvcName", "fieldType": "MicroServices", "fieldValues": "gw,metier" }, { "fieldName": "msvcEntityName", "fieldType": "String" } ], "changelogDate": "20161208100401", "dto": "no", "service": "no", "entityTableName": "pont_msvc", "pagination": "no", "microserviceName": "store", "searchEngine": "elasticsearch" }

Then When I'll CRUD on Store I'll use CRUD from Skill too, thanks this article but this point is an another story.

What do you think? Is this the right way?


回答1:


There is no the right way, as it depends on your needs. In your mentioned article (i'm the author, thanks for the complement!), I described the general approach, which has a better work flow described here, which makes it easier to implement but doesn't change the fact, you do more CRUD calls for one request as you increase your service communication chain.

So, what might be wrong with that? While this is the most consistent approach (you always get the very recent state of the data), you got a lack of availability, as your store service is coupled to the skill service. If this fails and there is no advanced cache setup (such as with hystrix), you have 2 services failing if skill service crashes. Additionally, request will produce a bigger network overhead.

Another approach is called event-sourcing, where your skill service informs in a messaging channel about changes of skill entities, so all consuming services can calculate the current state by applying that changelogs. While this leads to less network overhead and guaranteed availability, your data is "eventually consistent".

For this you could take apache kafka (which is also supported by JHipster) and switch to event based entity communication. This approach is harder to implement, as there are no pre-built functions as they exist for REST-based and secure communication



来源:https://stackoverflow.com/questions/42449160/architecture-for-communication-between-two-microservices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!