- [Instructor] Later services
will register and unregister themselves.
But there may be cases when a service
shuts down non gracefully with no chance to unregister.
Lets make the rule
that services need to send a heartbeat
to reregister themselves, continuously.
Data service that we haven't seen for 30 seconds
is considered to be expired.
So, if we look at service registry,
just if you remember,
in register,
we already have this function that updates the timestamp
if the service was already registered.
Now we will add a new function that we call cleanup.
And in cleanup, we first get current timestamp.
So const now = math.floor.
New date divided by 1000.
And then I run through the services with object keys.
This.services
and I do a for each on them
and I get a key in my callback.
And in there,
I am trying to find out if the service is expired.
Add if this services key .timestamp + this.timeout
is lower than now,
we will delete this services key.
So let's remove it
and I also do a this log debug
and I will output removed service
with the key that was just removed.
Of course, we also have to call this function occasionally.
So let's add it to register in cap
and so I simply call it at the very beginning.
This cleanup.
And the same for get.
This cleanup.
So this should clean up the registry frequently.
To test this, let's set the timeout to just 10 seconds.
And I will start the application.
So let's head into Postman to try that out.
First we have to create a new entry.
I use 110 as version now.
And then I want to do this catch request.
Find this version, is 110.
And we see that it expired already.
Now let's try it out once more.
So I've just created it.
And let's do the catch request again.
It's still here.
And let's see what happens after a few.
Yeah, now it's gone again.
So now we have built up all functionality
for the service registry.
We can register and unregister services.
They expire automatically.
We have some load balancing built in
when we query those services.
So we are now fully complete
and we can proceed with building out
the rest of our micro service infrastructure.