05. Creating a Eureka Client

21 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.59
List Price:  €93.70
You save:  €28.11
£56.03
List Price:  £80.05
You save:  £24.01
CA$96.39
List Price:  CA$137.71
You save:  CA$41.31
A$107.98
List Price:  A$154.26
You save:  A$46.28
S$95.50
List Price:  S$136.44
You save:  S$40.93
HK$547.57
List Price:  HK$782.28
You save:  HK$234.70
CHF 64.43
List Price:  CHF 92.05
You save:  CHF 27.61
NOK kr776.82
List Price:  NOK kr1,109.79
You save:  NOK kr332.97
DKK kr489.21
List Price:  DKK kr698.91
You save:  DKK kr209.69
NZ$118.81
List Price:  NZ$169.74
You save:  NZ$50.92
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,729.18
List Price:  ৳11,042.16
You save:  ৳3,312.97
₹5,842.93
List Price:  ₹8,347.41
You save:  ₹2,504.47
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,745.84
List Price:  ₦132,499.74
You save:  ₦39,753.90
₨19,599.98
List Price:  ₨28,001.17
You save:  ₨8,401.19
฿2,600.12
List Price:  ฿3,714.62
You save:  ฿1,114.50
₺2,271.31
List Price:  ₺3,244.87
You save:  ₺973.56
B$363.64
List Price:  B$519.51
You save:  B$155.87
R1,308.28
List Price:  R1,869.05
You save:  R560.77
Лв128.30
List Price:  Лв183.29
You save:  Лв54.99
₩96,996.60
List Price:  ₩138,572.52
You save:  ₩41,575.91
₪261.41
List Price:  ₪373.46
You save:  ₪112.04
₱4,045.59
List Price:  ₱5,779.67
You save:  ₱1,734.07
¥11,052.75
List Price:  ¥15,790.32
You save:  ¥4,737.57
MX$1,197.81
List Price:  MX$1,711.23
You save:  MX$513.42
QR257.31
List Price:  QR367.61
You save:  QR110.29
P1,000.11
List Price:  P1,428.79
You save:  P428.68
KSh9,335.98
List Price:  KSh13,337.69
You save:  KSh4,001.70
E£3,349.95
List Price:  E£4,785.86
You save:  E£1,435.90
ብር4,013.63
List Price:  ብር5,734.01
You save:  ብር1,720.37
Kz58,409.45
List Price:  Kz83,445.65
You save:  Kz25,036.20
CLP$67,298.07
List Price:  CLP$96,144.23
You save:  CLP$28,846.15
CN¥506.79
List Price:  CN¥724.02
You save:  CN¥217.23
RD$4,121.81
List Price:  RD$5,888.55
You save:  RD$1,766.74
DA9,423.87
List Price:  DA13,463.25
You save:  DA4,039.38
FJ$159.96
List Price:  FJ$228.52
You save:  FJ$68.56
Q547.38
List Price:  Q782
You save:  Q234.62
GY$14,742.16
List Price:  GY$21,061.13
You save:  GY$6,318.97
ISK kr9,832.89
List Price:  ISK kr14,047.59
You save:  ISK kr4,214.70
DH712.24
List Price:  DH1,017.53
You save:  DH305.29
L1,243.01
List Price:  L1,775.81
You save:  L532.79
ден4,046.68
List Price:  ден5,781.23
You save:  ден1,734.54
MOP$564.08
List Price:  MOP$805.87
You save:  MOP$241.78
N$1,315.96
List Price:  N$1,880.02
You save:  N$564.06
C$2,592.36
List Price:  C$3,703.54
You save:  C$1,111.17
रु9,349.94
List Price:  रु13,357.63
You save:  रु4,007.69
S/263.70
List Price:  S/376.73
You save:  S/113.03
K271.96
List Price:  K388.53
You save:  K116.57
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,886.12
List Price:  ZK2,694.57
You save:  ZK808.45
L326.39
List Price:  L466.29
You save:  L139.90
Kč1,650.20
List Price:  Kč2,357.53
You save:  Kč707.33
Ft25,629.21
List Price:  Ft36,614.73
You save:  Ft10,985.52
SEK kr770.71
List Price:  SEK kr1,101.06
You save:  SEK kr330.35
ARS$61,698.11
List Price:  ARS$88,143.94
You save:  ARS$26,445.82
Bs486.86
List Price:  Bs695.54
You save:  Bs208.68
COP$271,471.23
List Price:  COP$387,832.67
You save:  COP$116,361.43
₡35,819.54
List Price:  ₡51,172.97
You save:  ₡15,353.42
L1,740.31
List Price:  L2,486.27
You save:  L745.95
₲526,285.13
List Price:  ₲751,868.13
You save:  ₲225,582.99
$U2,699.91
List Price:  $U3,857.19
You save:  $U1,157.27
zł284.80
List Price:  zł406.88
You save:  zł122.07
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.