- [Instructor] To use the ServiceRegistry
that we just created,
let's add an instance of the class to our application.
So I open server, service.js,
and there, right after I require express, I will add const,
Service
Registry,
equals, require,
lib,
ServiceRegistry.
And then, right after line seven,
where we create the instance of log,
I will now create an instance of the registry,
so I add const serviceRegistry,
equals,
new,
ServiceRegistry.
Note the difference in upper and lower casing here,
so the variable has
a lowercase S now,
because it's an instance of a class.
And now down there, starting with line 17,
I want to wire in this registry method we just created.
And for that, I remove the return statement we have here,
and instead add, const, and we will be using
ES6 destructuring assignments, so I add,
const, curly brackets,
and there, servicename,
serviceversion,
and serviceport,
equals,
request dot params.
This will assign everything from request params.
And now for the IP address,
this can be inferred by the request that's coming in,
so the service that's requesting now, the registration,
this request will carry the IP address of the service in it,
the IP address the service listens at can be taken
from the request headers.
On some systems,
the remote address will be in IPv6 notation,
and this changes the way how we have to connect to it.
And that's why we have to add a little check here,
a little trickery, so for that add,
const, service
ip,
equals,
some testing on req.connection,
.remoteAddress, because there we get the address
of the calling service,
and on that remote address I call includes,
because it's a string,
and I want to check for this double colon notation
of IPv6, and if I get that,
we have to assign, and I'm using backticks here again,
square brackets, and in there,
we again add,
req.connection.remoteAddress, so we are wrapping
the remote address in square brackets,
because that's the proper IPv6 notation.
In any other case, I will simply use the
remote address as we got it.
Now of course we have to call this register method
on the registry, so add
const,
serviceKey, because that's what we will get back
from this call.
And I call serviceRegistry
.register,
and there I pass in
servicename,
serviceversion,
then serviceip,
and the serviceport.
And in the end, I want to return
some result
that tells the caller that everything went right,
and it will be JSON, and there,
I simply return result, and serviceKey.
And here ESLint is complaining because the line length
is too long here, let's see what we can do here.
Let's wrap this line here,
and now ESLint is happy as well.
And here you see that ESLint is also complaining
about this next,
that it's not used anymore, so let's just remove it,
and to properly see what's going on in our application,
let's attach the debugger that is build into Node.
Setting this up is very easy with Visual Studio Code.
For that, I simply click on this bug icon here,
on the left menu bar, and there I click on this cog wheel,
and
I just have to fix that program line here,
because it should be bin/run,
because that's the start script of this application.
And after that, I can click on this play button
to start debugging, just make sure before you do that,
that the application is not already running,
and I click on Launch,
and we see
in the DEBUG CONSOLE,
that this application is running now.
Now, to do proper debugging,
I will head into the ServiceRegistry,
and up here, where we have this return statement on line 19,
I will click next to this line number on the left side
to add a so-called breakpoint.
Of course we now also have to perform
this PUT request somehow,
and this is where Postman comes into play,
so I will just start Postman.
I'm starting it for the first time here,
takes a little bit, and I'm just click on Skip signing in,
and this takes me right to this dashboard,
there I click on Request,
and let's call this request
Create
Service,
and it also wants me to create a collection,
so I click on Create Collection,
and let's call this collection Microservices,
and I save this request.
Now I want to do a PUT request,
so I select here the HTTP method,
and let's do a PUT request to http://
localhost,
on port 3000,
this is where our ServiceRegistry is listening,
and I want to call the method, the route register,
and our service will be called awesomeservice
just for the sake of testing,
and it's the version 1.0.0,
and it listens let's say on port 3001.
Now I click on Send,
and we see that even Visual Studio Code
jumps back into focus, so where we
just hit this breakpoint,
and then I click again on this debug view,
and there I see all the local variables on the stack.
And there, if we look into this,
and services,
we see
this services structure that we created, and in there,
we have this service that was just added to the structure,
so that looks good so far.
So we clearly see that everything is working well,
and we just can resume the execution of this application,
so click on this arrow button here.
Let's remove the breakpoint also, we don't need it anymore,
and let's look back into Postman,
what was the actual result,
and we see get the service key back as expected.
With that, we now have a basic registry
that can register services.
Next we will create a route to also remove entries.