To manage all services registered, we need a data structure that keeps track of them and also removes services that seem to be not available anymore. The service registry class will provide this.
- [Narrator] Finally we will now implement the logic that provides the service registry. For that we will create a class that keeps track of all services. It should have methods for adding and removing them. Additionally we want to make sure that a service gets removed if we don't hear from it for a certain time. This is why we made sure that our time service announces itself every 15 seconds. To get started, create a file called service registrey dot GS in server. So I click right on server, new file and name it service registry dot GS.
On top of this file we again add use strict. Now we add the class that's ESX syntax so we add class service registry and first we will implement a constructor. So we add constructor. It gets no arguments and we will keep track of available services in an array and we will intiate this now.
So add this dot underscore this marks this array as private services, equals and then square brackets. And we also want to set the time out here. This will be the time out in seconds after which we will remove the service from the list. So we add this dot underscore time out equals 30. Now for adding a new service we will create a new method.
For registering a new service we will now add a new method. We will also use this for updating the service when it announces itself again to tell us that its still alive. So we add a function, or method add and it gets and intent, and ip and a port. And this is important now, we want to neatly identify a service, for that we create a key that provides us with that. So we add a new constant key and it should consist of the intent plus the ip plus import.
Next we want to check if this service is already registered. So I add if it is not this underscore services key. So it doesn't exist so we want to initiate the object. So I add this services and square brackets key equals and empty object. What we are actually creating here is a so called hash map which allows us to easily access in any element in an array as long as we have the key for it.
So now we will add a time stamp. And we will use this time stamp to find out if a service is still alive or if it has failed to announce itself for a certain amount of time. So add this, underscore services and square brackets ket dot time stamp equals math dot floor new date divided by a thousand because this will be the unique time stamp in milliseconds.
Then we want to store the ip. This dot underscore services key dot ip equals the ip that was passed in this function. And equally we need a port a copy this here now, the services key, port equals port and we also want to store the intent in the service so this service key intent equals intent.
For now we will also add some debugging output that tells us that everything went right. You can remove it afterward if this gets too verbus. So add console dog log and in brackets added service for intent dollar curly brackets intent on dollar curvy brackets ip colon dollar curly brackets port.
And we can now return to this function from here. And by returning so creating this shortcut we don't have to create a else branch now here, but handle the case when the service actually exists. So we just have to update the time stamp and for that I just copy over this line from here and add here and we also want to add a log line here and I will simply copy this over and rename this tool updated service instead of added.
Next we want a method that removes the service that is quite easy. So I add a message remove this gets an intent and an ip and a port again. And we simply have to construct a key here again. So I do a const key equals intent plus ip plus port and we can simply delete this element from the array by simply adding delete this underscore services key.
Now we need a method that returns us a service for a given intent, so we add a method called get, and it gets an intent this argument. And lets simply traverse the array of services and find out if we find a matching one. So lets create a for loop for let key in this dot underscore services so for every service we want to check if this dot underscore services key dot intent equals the intent we want to have then we will simply return this dot underscore services key.
Implemented like that it will simply return the first service that matches an intent. If you want to do some loop balancing you'd have to add some randomness here like collecting all services for intent and then randomly selecting one of it. Also lets handle the case that we didn't find a matching intent. And we will simply return. Next we need some housekeeping method that does the clean up so it should look for services that are actually expired and remove them from the list in some kind of periodic manner.
For that we will add a method cleanup. Its a private method so I'm adding a underscore here because it should only be called from within this class. Cleanup we will store the current time stamp that's const now equals math dot floor and your date divided by 1000 and we will traverse the services here again for let key in this underscore services.
Lets check if service is expired so its add if this underscore services key dot time stamp plus the time out we declared before this dot underscore timeout is lower than now. And this means we haven't heard from this service for 30 seconds.
We want to delete this dot underscore services key and maybe also on top of that, so before this delete statement lets add some log message again. So we add console dot log and in back ticks remove service for intent dollar curly brackets this dot underscore services key dot intent.
Now we also want to actually call cleanup so we will use it in add and we will call it right here so after adding a new service here were going to do a cleanup to remove all services that may by still in out list of services and also here we will add it as well and also in out get method. Because before we reply with a service we want to make sure that our list is cleaned up.
Now lets not forget to also export this class in the end. So we add module dot exports equals service registry. And next we need to add this service registry to the route we created previously. So lets open service dot GS. And on top of it we first require the class const service registry equals require and we have to path here to service registry.
And now we have to instantiate it. So we add const service registry and note the lower case here, the lower case s, equals new and here we use the upper case because its the last name service registry and parentheses. And now all that is left is to add this method here on line 15 we add service registry dot add the service intent the service ip and the service pod.
Now lets do a quick test, make sure the time service is still running. Lets check that, here's my time service, and its still running but it has a problem connection with RS because RS is not running. But now we will start RS by typing node bin run dot JS. And here we already have the log message about the added service. So this is the time service that just announced itself to RS and we have the log message coming from the server registry that it was just added and here we have already the update as well.
After explaining some basics about Node.js and microservices, Daniel shows you how to sketch out the planned architecture for your application and get the boilerplate code, modules, and credentials in place. Next, he shows how to create a bot user in Slack, connect to Slack, and post messages. He also shows you how to get your bot to process variations in text by creating logic that delegates the processing of intent to dedicated modules. Lastly, he shows how to register additional services and he covers how to use monitoring to identify architectural or performance issues.
- Using Slack APIs
- Sketching out a Slack bot architecture
- Setting up a project and choosing modules
- Creating and naming your bot
- Connecting to Slack
- Setting up and using natural language processing
- Routing by intents
- Implementing geocoding and time calculation
- Adding and monitoring services