Service Discovery for Microservices in Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to create the service Discovery but before that I will talk a little bit about the service Discovery if you're into microservices architecture you may have heard of service Discovery in microservices each program or server is running on a separate server each server has different IP address and um it is not s able to hard code the IP address in your code uh because they may change especially if you're launching the servers using any automated software in this case it's not possible to know the IP address to solve this problem we can use service discovery which keep track of each server IP address in this diagram here the big circle is the microservices we having here and Service A B C already in the microservices and using his own IP address and let's say in order to complete the task service a you need to contact service B service B contact service service C and service C need to contact service X to complete it but service X is not in the system yet and service X want to join when service X joining to the microservices the other services don't know about the IP address of this service what need to be done is service X need to register itself with a service Discovery say what IP address is holding and what service name it is and then this record into the service Discovery cache service a can just request the name of service which is service x to the service Discovery and then get back the IP import of the service then can connect directly to the service X to complete the task each of these Services when they join the microservices and register with the service Discovery need to check whether it's still up and running or crashing for that we need to get back to each service and see whether is accessible for this we need to contact each of these Services once a while to know um the service is up and running or not if the service is accessible record will remain in the cache otherwise we're going to be deleted the other services do not contact the wrong IP which service is already crashed here let's say the user order uh request the order want to purchase a product and the order first going to hit the order service the order service need to check two things first maybe more than two things but for now for Simplicity I just keep it simple for two things it's going to check this uh user have enough uh credit inside the account to purchase or not it's going to contact the user service so this is a checking the first checking the second checking is that you're going to check the product is available so for example if there is one remaining of the product and two user at the same time time order the same product first user will going to get the product then the quantity will be zero out of stock but the second user is not be able to purchase the product after these two checks validate order service is going to update the product quantity and then update the user so updating the user is that so the product need to add to purchase product of the user for the first thing I created here is the RPC server and the TCP listener to receive the request from other services about getting the IP address of another series next I need to do is to create the methods for this um r P see each service is going to register with the service Discovery it need to be validate so sometimes they forget to pass some IP name or end points for all these things so before getting to the problem later on we need to validate all those data so here we're going to validate three things uh one is the the name um of the service which other services need to look for based on this name is just a simple string name and then the second thing is going to be the IP and the port of the uh service and the third at the last is the health check end point so here I use one of the library or the auxilary library that I created uh earlier for myself so I just basically generate a random string um and uh based on the number that we give here Bas on the quity that we give here so five means five digit uh random string and then uh we need to have a cash package here to add the data so the cash package actually is the package that is going to handle the redish so here I create the very first um code for connecting to the Reddit in here also I'm going to use ex library that I created myself for reading the environment variables and then getting the red client r to connect and lastly we're going to check before connecting it so here we need to run the chrone job to check uh once while the health check so I want to have the function for this to create a new Chrome job based on the data that I provide which is endpoint and the health endpoint is there health check endpoint is there and then uh I want to Define how long um will be the Gap so for example here I mentioned like uh 5c it can be 1 second or whatever you decide but I think 5 Second for uh this video should be okay usually it's going to be based on how much traffic you will have on that server so if that service is going to be the high traffic so maybe you need to check it like millisecond or something like that or 1 second but for now I think five should be fine for okay now I need to create the uh radius on the dock curve okay so basically I create the very simple radius uh Docker file and um reing the password and pressing it with the environment variable and let's see if it runs okay apparently it's working let me check um okay there this is up and running okay good so right now we don't have anything because there's nothing registered in the cach yet but uh this is showing us that there it is up and working is working on the docker properly for for for so now I need to create a Docker for the service Discovery itself okay now the discovery and the radius Docker are running good so now next is to create the uh order service okay now first thing we need to do in the order service is to register with the service discovery for okay so basically here um I'm going to create some credentials for this service and then uh call the server Discovery and then it's going to request for registration and then pass the details of the service and then we return the generated U ID from the the service Discovery and just for for the information we're going to uh display the uh U ID of the registered service or we have a problem here because I haven't uh passed IP and Port so it's going to be empty so to do so on the Linux which is my main operating system I know that we can get the IP address of the server or the running computer is this I haven't tried this on the micros but uh this is how it work and then uh why I'm doing this because if we put one 27001 it's not going to work because we're running it on the docker container and the docker container is not using uh that IP okay now we have another eror and the problem is that AWS is not a commment awk is the correct command so yes now it's correct and I'm getting it so when we register it we're going to have that order here as you can see we have the service name and the generated U ID for this service and if we check again it's not going to be there because after 5 seconds it's going to call this service to see whether is alive working or not and obviously not because is just register and just exit the program okay now I use the latest um Go version so the new Go version have a very good um routing system previously when I was uh starting another uh Series in 2023 the gorilla marks was discontinued and archived actually and then I forced to create my own routing system but then uh recently just uh few months ago few weeks ago the Go version 122 came out and then uh they already implemented uh this routing and it's very convenient actually so as you can see I don't use any third party for the routing and the HTTP server here so once we have this we're going to try to check again it's going to stay alive and uh stay connected to the service Discovery as you can see still we have the order service with the U ID here we can check again and it's still there so now since we have all this uh working together we need to create another service which is the product service so for the Simplicity uh in the product service I'm not going to create any database so I'm going to just hard code the products inside the code okay now what I need to do is to create a HTTP server for the end points of uh getting the product um detail based on the ID as well as updating the product and also we need the last one which is the health checking for the service Discovery so in this Handler I need to get the product based on the provided ID on the URL and then after that uh we Marshall it to Json and then respond it back okay now I need to update it after the purchase already completed I need to deduct the quantity from the product all right so in in this um I just collect the same ID that provided on the URL P URL parameter find it from the map that we have here deduct the quantity and then place it back so the next time the quantity will reduce so this one I just like mimicking how you interact with the database so you read from the database update the database and that's all so now I need to check is it working or not I did not get this as registered so that means if something is not working here properly and the problem is that because I'm opening the wrong port and see whether we are registered properly yes so the product is healthy now we can check that the product is here so the other thing we can do is to run the order so now we can see we have both of the services here Order and product and still up and running and if now we try to get this as you can see we get the product back you change the ID we going to get the product b as well okay so that's showing that is working and then uh we have the update and uh update the product after purchase so this is basically just uh deduct one of the quantity so every time you send this is going to reduce the amount and we get 200 which is we still we have quantity uh if we change it to this which we had two and two times and the third time a b request means is out of the quantity is out of stock okay now I need to create another method for the service Discovery to return the IP address of the requested service okay both services are up and running so as you can see we received the IP address of the product which is uh only on the Local Host is the 7070 and uh the order which we receive here is 6060 so the 7070 is the IP and the port of the product service on my local machine so now we have the address of the product service we can call the product service on the endpoints for getting the quantity of the product and then update it for and you can see after that the the quantity change so what happened here is that the user send the order for the to the purchase endpoint to the order service and the order service will check with the product so if the quantity is still available is going to deduct one so here uh is the end point for the purchase on the order which is the port 6060 6060 and the product is XXX so the product ID is this and then when we send this it's going to check and then update it and here in the product if we get that uh the quantity was nine should be eight now is eight so if we change this to product y y y that also need to be updated yes so after that is going to be zero means out of stock so if one more time after that we try we get insufficient quantity which is not found the the the product is not found so I created the service Discovery two Services which is the order service and the product service they communicate each other in order to complete the task and each service is register their own IP and the port to the the service Discovery provide the health check endpoint to keep track of the health of the server and providing the IP address of other services and that was all for this video and thanks for watching
Info
Channel: xKernel
Views: 11,505
Rating: undefined out of 5
Keywords: Golang, Service Discovery, Microservices
Id: 1ArzVReLc_Y
Channel Id: undefined
Length: 30min 3sec (1803 seconds)
Published: Sat Mar 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.