Let's understand why service discovery is required. Iff you look into
the traditional distributed system what happens over here is you have a client
and the client in turn will connect to multiple servers right so in this case
we know the location of the servers and mostly it is a static IP address right
so if it is a static IP address we can have the static IP address configured in
the property file and the client can access the property file and it can't
connect to the different services right so there is a no problem in this right
so in case of micro service architecture there is a little bit of complexity in
discovering the service let us understand what the complexity is right
so in case of micro service architecture you have a client the client in turn
goes and connect two different servers right the servers in turn will have
multiple services right so now if it is since it is a micro service distributed
system what happens here is you have a mechanism to autoscaler or order scale
it down right so in this case what happens is I Peters of the system gets
keep on changing right so it is not a static IP trust right if it is not a
static IP address how will the client know the IP address so that is how we
are going to introduce a new component called service registry right so service
registry is nothing but it is a database which has all the available instance
which has been registered and it provides to EPA's primary Onis the
management EP may the server's can go and register the instances into that and
the second thing is the client can query that API and figure out what IP address
or what instance is available for the client to use it correct and also there
are other important features within the server registry service registry let's
see one by one so service registry should be fault tolerant correct so
let's say you have one service registry and if it fails in this case what
happens is the client will not be able to discover the service at the same time
you know you have to make sure the service registry is highly available all
right how do you do that you can have a service registry deployed into the kind
of deployed into the clustered environment so that you know if it one
service registry goes down you can have a mechanism to find out or another
service registry and make use of it right so that is we have to be very
careful when use the service registry right so now on top of that any service
registering as all the information client can sorry all the servers or the
services can go and register into the service registry
right so once the service registry has that information client can go and query
with the query EPA and once it's notes the instance it can directly go and
connect to the different services based on the output from the query parameter
based on the output from the query string correct let's also talk about the
discovery patterns right so we have two discovery patterns to identify or to
discover the services one is client-side discovery pattern and other one is the
server-side discovery pattern in terms of client-side service pattern what
happens is you have a client the client in turn connects to different servers or
different services right the services goes and registers or itself registers
within the service registry and the client can use the query to get the
different instance and it can make use of the different services correct that
is client-side discovery pattern whereas in terms of service I discovery pattern
the client has a very minimal role so client in turn connects to the load
balancer and the load balancer in turn connects to the different servers or
different services right so services in turn go and register to the self
registry right so here if you look into that the client is not going to make an
actual connection to the connection to the service registry what happens here
is load balancer is going to query the service registry and once the load
balancer has that information about the different instances it can go and
connect to the different services like so here a load balancer plays an
important role in the server-side discovery pattern right so all your
logic algorithm has to go into the load balancer level to discover the services
or to balance the different servers correct that is what we call it as a
server-side discovery pattern right so now let's see how to register the
services or instances to the service registry so there are two types of
registering mechanism let's see one by one the first one is the self
registration in this what happens is you will have the servers or the services
running in the different locations and they go and register with the service
registry by themself so that is called self registration right the second type
of registration is called third-party registration in this what happens is you
have a service registry and you have a server
running across the globe right here we are introducing a new component called
register so register what it does is it goes and connects to the different
service registry and it registers different service instance right so it
uses the methods like registered heartbeat and register etcetera earlier
it was a self registration where this server itself takes care of registering
itself but here you are using a third-party component called register to
do the registration operations right so now it also does one more operation
where it keeps on pings the different servers across the globe and it makes
sure it is really healthy and it keeps on running right so let us understand
like you know if the server or service is not running it has to unregister
right so that is why it keeps on doing the heartbeat mechanism to make sure the
server is running or not so if it is not running it immediately goes in and
registers that right so one important difference if you look between the self
registration in the third party registration is in case of self
registration you have to write a code in a service to make sure you just connect
to the service registry and go and register the corresponding service to
the service registry right whereas in third party with third party
registration you don't want to write a code you can't make use of the third
party components or third third party services to register your instance or
services into the service registry right one more interesting thing over here is
if you look into the overall service discovery mechanism the service registry
is a component over here right so now there are certain cases like if you want
to get an environment into AWS and if you keep on spinning the new instances
automatically they provide a service registry where your new instances get
added into the service registry so you don't have an extra work right now let's
say you have a mechanism where you have to build your own service registry then
you can use the software components like components like Netflix Eureka right so
you can install and you need a little bit of work to make sure that Eureka
instance is deployed at the cluster and it gets added into the different
services or you can use a different ap is provided by service registry to
register your services right that is another way of doing it right