In this session, let us learn how to create Eureka client to access the micro service in the registry. The Eureka client is also going to be a micro service. So it's like one service trying to access another service in the registry. Because we are using a generate enable discovery client in the micro service, it means that it can be accessed by other services or it is available for other services. In our case, we are having a service which is sitting in the registry, the service name is hotel service. Now, let us try to create a client which is which will access the hotel service.
I hope it is clear I am having one service which is trying to access another hotel service. So this is a client This is a micro service. The client is also micro service this you have to remember. Say for example, I have a book service Add an inventory service, or I have an order service and an inventory service data service is trying to access a Inventory Service to check in for the list of the books. Similarly, in our case, I'm going to create a client which is going to get the list of photos and get hotels by city or get one hotel by ID. Now let us start creating it is a new spring starter project.
Let me do the name of Eureka client, package name of scotoma. And the description of client accessing hotel service. This package also comes with an app. Click Next. Now, what are the dependencies I need to take Eureka discovery and Eureka server. Let me select both and click Finish.
The application is getting created. Now it is ready. Let me go If I need to go into SRC main Java here I have got Eureka client application. This is also a micro service and this service should be available for other services. So what I will do is I'm going to annotate this class with our direct enable discovery claim. The name itself depicts Can you understand it, enable discovery claim, it means this service is available for other services, it is getting enabled, okay, this is done.
Now, what is the next step I need to have a controller which is annotated with at the rate risk controller and what is the model class that is available in hotel service? It is Hotel simas Java similar class in this application also. So, let me first create that and then we'll start working on it. More The classes are ready, the hotel classes similar to the class that is available in hotel service. So you can just go Copy paste to this application. Next client hotel controller, I have given this name explicitly because this is a client application, which is trying to access the hotel service.
Because this is a RESTful API, I am annotating this class look at the read rest controller. What is the next step, if you want to access a RESTful web service, in that case, you need to go in for a rest template. That is you have to create an object of rest template. And with the help of that object, you need to call the method get for object get for entity exchange. Already have learned how to access RESTful API for that also use this template objects. So now what we need to do we have to create it rather than recreating it.
We are going to ask spring framework to create the object for us. So let me go into the main class in the main class, I will create an object that I'm going to use at the red bean annotation that we added I've got a big public rest template method, which is going to return a rest template object. So, here the rest template object is created by spring and given to the controller in the controller, we will be activating it. Now in this case, let me talk about one other concept called as load balancing. What is load balancing? When the number of requests coming to the server is more, it has to be balanced properly.
Say for example, if the server can handle only 10,000 clients, when the 10,001 client is coming in how to handle it, what actually will it do? whether it'll say like, wait a minute, I want to have the application saying that the application got hands by hope you remember this big billion Day Sale, wherein the number of requests going into Flipkart are small. So you are getting server timeout error in the browser. So, this must not be the scenario. So what do you need to do you have to have your application deployed on multiple servers. I hope it is clear, I am deploying my application in five or six servers, each server can handle 10,000 clients, when the 10,001 client is coming in automatically he will be rerouted to the second server.
So, this is the scenario this is called as load balancing, because it is happening on the server side it is called as server side load balancing. In this case, we are going to talk about client side load balancing. What is the meaning of client side load balancing? The client is now responsible of distributing the traffic that has come going into the server. How accurate is the figure of the traffic? Now I have a Eureka client.
This is a client application. It is time to ask for help or service. The service is deployed. Do a car service. Having multiple service registries means is responsible for checking for the traffic in the service registry. Based on the traffic it is going to pass that connect to that particular micro service.
Our service is applied in a registry in different registries. So now clients will decide whether to go into registry one knowledge is key to how client side load balancing is done. It is done with the help of Netflix ribbon. Netflix is responsible for client side load balancing. In this case, we are not using the ribbon, but we are using rest template for rest template if you want to add the functionalities of Netflix but you are to annotate this class with our separate load balanced. Now let me add annotation explain this again.
I want to add client side load balancing functionality to this template annotated with a direct load balanced, it means this template can get the functionalities of Netflix ribbon, which is nothing but client side load balancer. So this is done next let me go into the controller in the controller, I need to auto rock this template. So let me add it as a favorite template. This template let me add method to connect to the hotel service. Whatever method we're having in hotel service, get all hotels get Hotel by Id get photos by city and a great message. welcome message.
Right So to start with, let me add only one method which is going to talk to the hotel service. Take this photo from this hotel, find our classes this method I need to address request mapping It means you value and producers value equal to already we are having all hotels there. So here I'm just going to have a hotel slash hotel. And what it produces Jason data produces equal to application slash Jason Jason. Okay. Now inside this, using the rest template, I'm going to call get shot object method.
So this web template dot get caught object. This get fed object is having three methods, one with Ura. And the other one thing and response type. And the third one is you are a response type and the variables that you want to send to the hotel service are sent to the application. In our case, I'm going to use the third method. What is the first one is a URL which we have to give?
Let me just add the URL You want a call to water say your age is nothing but the full part like it has to be http colon localhost colon 30 HTTP slash slash localhost colon 8081 slash all hotels, hotels by city, whatever we are seeing in the browser, right? But instead of that we are going to view the service name. What is the service name? If I think that the application name hotel service, so responsible for converting the application name to the proper URL it has done as well, because we are used at the rate rollback load balance automatically we will state the name of the application that we have given and convert it into a proper URL. Okay, so here let me add it it has to be http colon, slash slash. Hotel service, hotel heifer service, flat out vocals that has to be in lowercase semi colon.
So, the first parameter is the URL and the second parameter is the response type. So, in our case it is list. So, I have to give it a list class what it is returning it is returning in the stop hotels. So, in this hotel hotel list equal to let me import everything and they will add a comma. So, let me return the hotel list method is ready, I will add the other methods later. Next we need to go in for this application dot YAML file.
So what I will do is in slot creating it let me copy it from hotel service Ctrl C in the sources Ctrl V application dot EML. Here I need to change the port number 8082 and let me give the name of your a coastline. Remember, this is also Let's service only for service This is a client that some other service it is going to be a REST service or a micro service only. Okay and this is the HTTP localhost 8161 slash Eureka This is completed, we were talking about the white label here right. So let me just press Enter I will add it here, I will do control space white label wh and I have given it is actually server dot euro dot white label dot enabled. So I will press Enter automatically I get that it has to be false.
I don't want to see the white label error page instead of that I want to see the direct error page only Okay. Let me save it. Now I need to run this application. before running this application we need to check or ensure that whether Eureka server is running and the micro service that it is looking for is also running. It is looking for hotel service. So hotel service also should be Running.
Let me open the console. The console, nothing is running. So I'll start with Eureka server. Yes. Eureka server started in port number 8761. Now let me open it in the browser, localhost 8761.
Let me press Enter. Now I have got the service registry ready the server is running. There are no instances available as of now, because we are not running hotel service. Now I'll go back to eclipse. I need to run the Auto Service. The application is getting started.
Now you can see it is available in Port 8081. What is the service name discovery at client underscore hotel underscore service. Okay, now we will also check in Eureka server immediately the console has changed. If you see here I'm adding Eureka server. If you want that you can go back we can switch console. So by going to this particular monitor here I've got auto service in order service This is what we were seeing right?
Suddenly change to Eureka server and open Eureka server. in Eureka server, you can see registered instance hotel service. Now we'll check it in the browser also and go to the browser. Let me refresh it. Now automatically, I'm getting the hotel service. So we have created a micro service, which has registered itself to the registry.
Now we will go and start the Eureka client. So I will open Eureka client, select Eureka client, right click Spring Boot app. The application is getting started. The application started successfully. You can see updating Port 28082. The application name is discovery client underscore Eureka client.
Okay, now let us check this out in the registry. Also. You're a cassava. Yes, can you see registered instance you're a client. Now, there are two instances running in the registry. Any other micro service can connect to the service by knowing the application name the application name is hotel underscore service and Eureka sorry hotel hyphen service Eureka hyphen claim.
Now in checking the browser I will refresh it again. Now I have got the Eureka client, I have got the link also. Let me click this link. From here what actually we are going to call hotels lists. Let me check that out. And open the controller client hotel controller in the request mapping what we have given Okay, it is not hotels list it is hotel.
Now, we will go back to the browser again. I will delete this and I have to give a hotel. So this is one service with a sign to connect to another service. Let me press enter. Yes, good, it is working perfectly fine, I have got the output. So what actually has happened?
First we created a server, this is going to be registry, whoever wants to add a service in the registry, they can do it by creating it as a micro service and giving the annotation as at the rate enable discovery client. So we created voter service, and we register it with the registry, or we register it in the registry. Then we created an earache offline, the Eureka client wants to access this particular micro service. And this Eureka client should be available for others also. So we are used at the rate enable discovery client annotation. It is not necessary that this has to be a RESTful API.
The Eureka client can be a simple web application, which is trying to access the micro service in the registry. I hope it is clear in when you talk about Eureka clients can be a web application, which is trying to access the micro service which is available in the registry. It can be a RESTful API, which is fine to access a website, I mean a micro service in the registry. In our case, the second one, this is also a service which is trying to access another service. Okay. Now, we have got the first output.
Similarly, we will try adding more methods. I'll go to the I've got a clip here a lot three methods more. We have seen find, then. Okay, the console the problem down. Now, let me change the course and then I'll explain. Now my methods are ready.
Let us see one by one. The first one is getting hotels by city. From here I'm passing it as a PATH variable, because this is also a micro service or a web service service. If this is going to be a web application, you can even pass the city Within from within the form you can get create a text fields are the user to enter the city and click of a button you can retrieve the value over here this is a web service find this using PATH variable only. So it is hotel city and I'm passing city in the other application hotel service, what is the URL that we have given what is the path that we have given hotels by city Let me open that hotel service also. So that it is easier for you to check in for the path.
So this is the hotel controller in the hotel controller what I have got hotels by city slash city, this is what I need this path so that I have given over here hotel service slash hotel space city slash plus city Okay, this is coming as a PATH variable. similar name dancer hotel Id also that is for one hotel, one hotel slash ID and supposed to fame this path. So I have given at one hotel slash plus ID. This ID I'm trying to retrieve from the PATH variable For a brief message, the first method here, grid slash named has to be. So the same thing I have done over here, grid slash name. And I'm trying to get past the user name apart variables.
Okay, so this is done. Let me save this. Now I'm going to relaunch only this particular application. So I'll select your A cos my application is getting relaunch, I can see the console. I have got the Eureka clients updated. I am also checking the Eureka server to the other face.
It has canceled instant Eureka clients. Again, it has registered the Eureka client because we have relaxed application. Now I'll go to the browser in the browser, I will refresh the server first. Okay, so this you may get if you are refreshing again, if any of the applicants If not available, don't bother about this I will come to the 808 to slash hotel spot. Now I will refresh again I've got the output now it says hotels hyphen city slash Daily Press Enter I bought the stop hotels by Delhi and similarly I will try for Bangalore also. Here we are getting it in JSON format because we have given producers this application slash Jason.
I have bought it for Bangalore Can you see? And of course I have one for pony I think I will just check that out also. Yes. Okay. We will try for one hotel for only this 127. So I have to give it a hotel, flat 127.
If we press enter, I'm getting it or if we do some other value 123 the first hotel name right. So, it was working perfectly fine, I hope you can see the output I increased the browser size also. And there was one other method to call greet. So from here the path is say hello say hello slash Priya. If we press enter, When complete, I have brought the output. What we are seeing in this particular session is how to create Eureka client.
This Eureka client is also a restful service which is trying to access another service in the registry. One thing that we have added over here is accurate load balance. We are trying to use the concept of client side load balancing because we are using a direct load balance. The rest template is getting the functionalities of Netflix ribbon in the upcoming session. So you will also learn how to use Netflix well for client side load balancing. We will also learn how to use server side load balancing using Zul so this is fire Eureka.
Client I hope it is clear. Thank you