Reputation: 155
This is a question that's been annoying me for a while, how does one write a series of microservices that run on on various machines at different locations without the need to hard code each services individual location?
Like say for instance I had service A which does some form of validation of a json message. Service A runs on box 1,3,5 and more instances can be brought up as demand grows.
Now say I have service B which looks to call upon service A, how would I communicate to service B where my service A resides?
Possible Solutions I've considered:
Hard coding service B with the location of a 'master' node for Service A which then delegates tasks out to all instances of service A.
Utilization of message queues? - Service B writes to a series of message queues, Service A instances read from set message queues and sends back results to service B.
SSDP - utilizing some form of simple service discovery protocol to broadcast which services are running where on a set network and keeping track of these services.
I'm quite new to this architectural style so I'm hoping I've not missed something very simple?
Upvotes: 4
Views: 1685
Reputation: 701
Use a service registry and look up the location of other services at run time. Here are some of the typical technologies used for this (there are others out there).
The service registry must exist in a known location. This location should always be a configurable property in your microservice. Never hard coded! For improved flexibility, it's pretty typical to access the registry endpoint byway of a DNS. So, your service looks for https://registry-1
instead of a specific IP address, which might change.
Depending on the communication mechanism you want in your system, a message queue will help your services communicate, but it will not help with discovery. In this approach you would still use a DNS and configurable properties to tell each microservice the location of the message queue. Individual services would then publish and subscribe messages to the queue. Microservices would never be aware of other services (no discovery) and all communication would be through messages in the queue.
Sam Newman's book on microservices goes into greater detail on these approaches and covers other areas of concern you are likely interested in.
Upvotes: 4
Reputation: 897
Generally speaking, there are 2 approaches to implementing service discovery:
Upvotes: 2