In this session, let us learn about ribbon. Apa ribbon. Apa is an inter process communication Cloud Library, it is used for client side load balancing. Already we have learned how to do server side load balancing with the help of Zul API gateway. In that case, whenever a request comes in, it directly hits only the Zul API gateway, then the Ura it will connect to the appropriate micro service. In this scenario, we are going to talk about client side load balancing.
This is provided by ribbon, APA, which has its own algorithm. Say for example, you are having a micro service which is running in different servers in different ports. A request comes in if you are using ribbony apa, they're done the server availability the request will be given to the appropriate So, but ribbony apa you will say certain algorithms to identify the server in which the micro service is running. He also has got few features $5 that is, it can check what are the servers which are up and running there done that only it will connect the records to that appropriate server configurable load balancing rules. When load balancing is taken care of a ribbon APA it will identify the server based on this particular rule round robin rules that is each server will be given this many number of requests when it is over automatically to switch to the next server.
The availability filtering rule weighted response time rules are these we will discuss now, this ribbon API also supports multiple protocols. Let me explain this with the pictorial representation. I have a micro service which is running in different servers in different form. Let's say for example, our hotel services running in 8182 83. Now I have a client, the client wants to connect to this hotel service. Now, in the previous sessions, what actually we have that we are identifying the service ID and then trying to connect to a hotel service.
In this case, there are three servers which are having three I mean the same micro service. That is I'm having three instances of total micro service. So if the client is going to have the ribbon APA, the ribbon APA is responsible for identifying the server and connects to the client. I hope it is clear all that whatever request has come in given by the client. This ribbon API will identify the server and choose that particular server to the example now before that, let us talk about the components of load balancer, rule pink and serverless. What does rule allow components to determine the server from the list of servers in our case I have got 8182 83 which is running the same micro service.
So, based on a rule only one particular server will be determined. So, what are the different types of rules round robin rules it is in the service are selected by round robin fashion, then availability filtering rule, in this case, the round robin sorry, the ribbon APA will check in for the servers which are prone to fall that is they immediately immediately servitude breaks these kinds of servers are avoided. So, that is what is called as availability filtering rule. Next rated response time rule. Each server is given a weight based on the response that they are returning back to the client. The longer the response, the lesser the weight they get.
So, based on the weight the servers will be given the request. So, these are the three different rules, how actually ribbon APA will know whether the server are up and running. For that we will be using Ping Ping is a common and to check in for the liveliness of these servers, and how to get the server list that is provided by server lists. The server lists that a service can be static or dynamic. Now just read through this, within APA will check in for the server availability by pinging the server continuously. If the flower is up and running automatically the request from the client will be given to this particular server running the micro service.
These are the components of load balancer. Let us also talk about the ribbon configuration ribbon. Apa has got its own default configurations, either you can go with that are you can provide your own configuration, the configuration can be provided in terms of a Java class or you can just add it in the YAML file. I will show you both the things now let us try working on this applicant What actually I'm going to do with already we are having hotel service, I will run three instances of photo service that is I will run hotel service on port number 8081, then at 83, then at 85. So three instances of photo services running then I will have a client as usual this also is going to be a micro service. This client will have ribbon APA within itself.
This ribbon APA will choose any one of this particular server to access the hotel service. Let me go back to eclipse. I am an eclipse for understanding within APA we need Eureka server one micro service which is acting as a client one micro service which we are going to access in our case already we have created Eureka server and also we are having hotel service. I have started Eureka server. The next thing is I need to run hotel service, hotel service. I want to run this Three different ports.
Let me show you. I'll open water service here application documents. First I have given a ti 85. So I will select it, and I will run it. The application has started, you can see that it is running in port number 8085. Now I'll come back to this hotel service application dot YAML file, I am going to change the port number to 8081.
Let me save it, I will run it again. Here I'll go and select hotel service and I will run it again. So this is the second instance. The application has started now let me also check the port numbers. Here port number 8081. Then I'll go and change it to 8082.
Let me save this. Now again, I will run it application started in port number 8082. Now let me just come to on the spot here you can see Eureka server is running together with that three instances of photo services running. Now, let us create a client. I told you you're the client can be a web application or a micro service. In this case, we are going to use a ribbon API.
So I'm going to create the client as a micro service. Let me start go to File, New spring starter project. Let me give the name ribbon client. So what is the description ribbon client accessing hotel service. Let me give the package name as simply combat example only. Click Next.
Because this is a client, what we need to select Eureka. Then, we are going to use ribbon select ribbon. Click Next. Click Finish the application is getting created. Let me open it. This is the main class, let me annotate it with the red ribbon client.
This ribbon client takes two parameters. One last name, the other owners configuration. As I told you, either I can just go with a default configuration of ribbon APA or I can do my own configuration. To start with, let me go over the default configuration. And this client is going to access hotel service. So you are to specify which service you want to access.
Let me give the name hotel hyphen service. Okay, because we are going to add the photo service again what we need, we need that we need a class which is annotated with accurate rest controller, and we need models. So let me just copied because already we have done it and you recognize I'll just copy the passage to the application. have brought all the packages ready, I have renamed it to combat rebel, then combat ribbon controller combat ribbon models, you know what is there and model it is going to be the model class in combat robot controller and having a controller the controller. Now, how to access it, you know that using a direct rest template only you can access the application. And of course, you know how to get the URL, either you can tap on the port number or you can use the service name service ID.
So in our case it's a total service. I'm going to use it only with that service name. Why because the same application will be running in different ports as I told you, in our case, I have bought a hotel service which is running in three different ports 8180 to 185. So here what I have done, you can just have a look. It is the same thing. I have got all the methods.
Like I will copy the data from your a client. It is slash hotel photo city hotel hotel ID one extra thing I have added here, what is that, I want to check to which server my request is connected. That is I want to identify the port number. So for getting the port number, I am auto wiring load balancer client, you can just see this and using that load balancer client and trying to call load balancer dot choose which application you want to run or which application you want to connect portal service that is a service ID that gets put. Okay, so this is going to return the port number to which this application is connected. Okay, so two things are all said I'm not going to work with hotel city hotels ID or say Hello, I am going to check in for this particular slash hotels only.
It has to return the list of hotels for hotel service. Okay, this is that what is the next thing we have to go in for application documents in application the yamo what you have to give, of course the port number in which this application will be running Then you have to specify the Eureka Eureka service registry details together with that you are going to tell the list of servers in which hotel service is running. So, let me first copy paste it from some other application, this application dot YAML file and then we will come. So this is a YAML file I have given the port number is 9000. The application name is Reuben client, also the service registry details. Now together with that, let me also add one more detail.
That is the detail Support Portal service. I have to tell the list of servers or I have to specify the list of servers in which this hotel services landing. Basically when you're working with ribbon, APA ribbon APA itself will go and check in the registry, the list of servers in which the hotel services running but I don't want the Sivan APA to check in and the service registry, but instead it has to read from this application dot YAML In which service this hotel service is running. So let me add it hotel hyphen service corner Eureka enable Eureka cola enabled. Corner. True sorry false.
So what is the meaning of it? I am telling ribbon APA don't go and check in the registry for the list of service the indigenous Auto Service is running. It is better that I will give them the server list for in the next slide I'm going to give the permanent list of service. So it is localhost colon 8081 comma, FAQ all the three port numbers are sorry, all the three server location 8081 82 comma 85. Okay, let me save this. You will be getting a warning.
Don't worry. Let it be This is the next I have to run this application select ribbon client right click ranas Spring Boot app will open the console. Okay this ribbon client is giving me an error field rest template is requires a B now press template actually we have done auto wiring, but we have not created it as a beam. So what I have to do I have to do that in ribbon client application. Let me just copy it from some other application. Here it is, I have configured rest template as a spring beam.
Together with this annotation, I am going to add at the rate load balance. What exactly is the meaning of it? This ribbon APA has to identify the server in which the service is running hotel services running based on the rules that you are providing. In this case we are not providing any configuration details. So automatically, APA will choose round robin rules for choosing the server. So what I have to add I have to add at this load balanced annotation Okay, this is done now let us run the application again select ribbon client here it is already available here I have given it let me check whether the application started yeah good it is started in ninth port number nine Sosa Okay.
Now, let us go to the browser and check all the details first let me start your Echo server. The service registry started let me come down you can see three instances of photo services running in which port number 8182 and 85. So, if you want to check it alone, you can even check it here directly in 8081. What is output like here? What is that all hyphen hotels right? And again, in 8082 then again in 8085 all the three dots to show the proper output, all hotels all hyphen hotels it is coming in at 82.
Also, let me check all hyphen hotels. So, your hotel service is working perfectly fine. Now let me go to this break apart here. I'm just going to click the button client, what is the port number 9000 and what is the URL slash hotel? Let me press enter it has given me the list of hotels. We will go back to eclipse and see I am in Reuben client.
You can see the server statistics what are the stuff servers in which this particular service is running micro services running? So the setting for that right can you see this here for our 190 2.1 68.0 dot 680 80 to 8085. But already we are given a code to check to which server this application this ribbon client is connected. I have given that right so you can see we are getting the gone to Eureka server and send it to frontline. Yes, you are a port 8081. Now, I am just going to keep it assets.
Let me open the browser again. I'll minimize the browser. I'm going to pull out a new tab, and I'm going to run the application the same application Ctrl C, and open the new tab, Control V enter, it is connected to port number 8082. Can you see this and then Control V enter, port number 8085. Then again, new tab, Control V enter. So what actually it is doing now right is going through round robin rules.
If you want to change the configuration that is also possible, either you have to create it as a Java class or you can go in for Yama, Yama, you have to give it as properties. Now, I will just show you how to use it and how to save the configuration using a Java class. Let me go to this eclipse I mean sorry, I'm an eclipse. Only let me minimize this, I have to create a separate Java class and then specify the configuration available in that Java class. So it is configuration. I didn't give a comma that's right my content This is not coming and minimize this configuration equal to ribbon config dot class.
I have not created this class so if that's why it's giving me an error, let me select it and create the class create ribbon country. Okay. Now, let me click Finish. It is in the root folder where exactly I have bought the main class. A configuration class in which you are to provide the configuration details for working with ribbon. You can view the pin details, the client config, the list of servers and also the rule in this particular class are going for application dot html for our demonstration I'm just going to take the role part.
So here I need to go in for oh my god I will just minimize this console or close this console here public eye rule I rule rules return knew I told you either you can go in for availability filtering rules or weighted response rules. If you don't even remember just you w e and give a control space automatically you will get the list you got the control space is taking time because I am running nearly six applications weighted response time rule, can you see this, press enter and it's with a semicolon and also annotate this class with at the rate beam only then this object will be created. I need to import been also. Okay this is done now I'll save the application, I will relaunch it, the application has started successfully. Let me go back to the browser. Let me refresh it.
Now I will maximize console, you can just oh my god, this Eureka server is coming again. ribbon client, you can just see this, the port number is now at 85 because the response time is less for this particular port number and see what rule it is using weighted response time rules. I didn't show in the previous application basically in the previous application if you see it will show up round robin rule. In this case, we are using our one configuration, but by the first round robin rule will be used to select the server that is why it was selecting 88 I mean 8182 85 then again 8182 85 goes on like that. Okay, let me run the application again. Refresh, port number 8081.
This time, let me refresh it to refresh 85 can you see? So, what is the principle it is using the same or what is the rule it is using this time weighted response time rule, weight adjusting job started. Okay. Let me also show you the GitHub site from where you can just get the configuration details to work with ribbon API. So, this is the GitHub location, wherein you can just know more about the load balancers. From here only I was talking about the components of load balancer and the rules round robin availability filtering rule weighted response time rule, right and to view the configuration details not using a Java class, but in application dot Yama, it is also possible you can just have a look at this given over here.
Client name dot client config namespace dot enough load balancer rule class name. So what is that comm dot netflix dot load balancer dot weighted response time rule. So how will I give in my case, it is like a hotel. Service dot enough load balancer rules classmates that is weighted response time rule. So I hope you understood how to work with ribbon APA. With the help of ribbon APA, you can perform client side load balancing.
That's it. Thank you