Microservice Architecture dependency

2019-06-07 05:39发布

问题:

I have read a lot about microservice architecture but there is one thing that I dont understand how to achieve and hope you can help me with this...

Lets say I have a web-api-endpoint that recieves orders that an OrderMicroservice is responsible to handle. When order is put Inventory must be updated so OrderMS publish an event to subscribers (pub/sub using for example Nats) and InventoryMS will update the inventory due to it is subscribing to current event/message....I want to have a loose coupled architecture and use asynch calls to modules/MSs thats are interested in given info.

Given scenario will work perfectly fine if you have 1 instance of InventoryMS but what happens if you have scaled the InventoryMS horizontally i.e there are 5 instances of InventoryMS and all of them subscribes to inventory.change.event and will try to update the inventory?

What kind of architecture or message pattern should I use for a scenario like this with horizonatally scalled MS's so I can have a loose coupled architecture when MSs are dependent of each other? One way is that communication internally is made by REST-calls using circuit breaker pattern but then I feel that I build a monolite of MSs with some smartness (the circuit breaker)...

Thanks for your help!

回答1:

Use Point to Point messaging model and only one consumer will receive a mesage. In pub/sub model all subscribers will be notified.
Example with ActiveMQ.



回答2:

You can still use pub/sub models but you need to set the multiple instances so that only one would get a message. the specific depends on the pub/sub mechanism

for instance in AMQP systems (like RabbitMQ) you'd publish the events on an exchange. the consumer service would bing a queue to that exchange and all the instances of the same service would read from the same queue (so only one would handle any given message)

Another example is Kafka - in Kafka all the instances of the same service would use the same consumer group - so, again, only one instance of each subscribing service would get the message

Other pub/sub systems would have similar solutions.

Point to point to specific instances is not a great solution as it would couple the instances of the different services