12. Using Feign Client

30 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
€64.94
List Price:  €92.78
You save:  €27.83
£55.57
List Price:  £79.40
You save:  £23.82
CA$95.27
List Price:  CA$136.11
You save:  CA$40.83
A$107.88
List Price:  A$154.13
You save:  A$46.24
S$94.55
List Price:  S$135.08
You save:  S$40.52
HK$547.57
List Price:  HK$782.28
You save:  HK$234.71
CHF 63.42
List Price:  CHF 90.61
You save:  CHF 27.18
NOK kr759.63
List Price:  NOK kr1,085.23
You save:  NOK kr325.60
DKK kr484.38
List Price:  DKK kr692.01
You save:  DKK kr207.62
NZ$117.46
List Price:  NZ$167.80
You save:  NZ$50.34
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,682.93
List Price:  ৳10,976.08
You save:  ৳3,293.15
₹5,837.41
List Price:  ₹8,339.52
You save:  ₹2,502.10
RM331.26
List Price:  RM473.25
You save:  RM141.99
₦99,285.71
List Price:  ₦141,842.81
You save:  ₦42,557.10
₨19,466.20
List Price:  ₨27,810.04
You save:  ₨8,343.84
฿2,553.28
List Price:  ฿3,647.70
You save:  ฿1,094.41
₺2,262.39
List Price:  ₺3,232.12
You save:  ₺969.73
B$349.43
List Price:  B$499.21
You save:  B$149.77
R1,335.92
List Price:  R1,908.54
You save:  R572.62
Лв126.45
List Price:  Лв180.65
You save:  Лв54.20
₩94,634.34
List Price:  ₩135,197.71
You save:  ₩40,563.37
₪258.03
List Price:  ₪368.63
You save:  ₪110.60
₱3,943.57
List Price:  ₱5,633.91
You save:  ₱1,690.34
¥10,600.67
List Price:  ¥15,144.47
You save:  ¥4,543.79
MX$1,161.53
List Price:  MX$1,659.40
You save:  MX$497.87
QR255
List Price:  QR364.31
You save:  QR109.30
P959.59
List Price:  P1,370.91
You save:  P411.31
KSh9,203.68
List Price:  KSh13,148.68
You save:  KSh3,945
E£3,310.52
List Price:  E£4,729.52
You save:  E£1,419
ብር3,976.27
List Price:  ብር5,680.63
You save:  ብር1,704.36
Kz58,526.41
List Price:  Kz83,612.74
You save:  Kz25,086.33
CLP$68,581.80
List Price:  CLP$97,978.20
You save:  CLP$29,396.40
CN¥506.04
List Price:  CN¥722.95
You save:  CN¥216.90
RD$4,144.87
List Price:  RD$5,921.50
You save:  RD$1,776.62
DA9,443.17
List Price:  DA13,490.83
You save:  DA4,047.65
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q545.87
List Price:  Q779.86
You save:  Q233.98
GY$14,645.83
List Price:  GY$20,923.51
You save:  GY$6,277.68
ISK kr9,762.20
List Price:  ISK kr13,946.60
You save:  ISK kr4,184.40
DH709.20
List Price:  DH1,013.19
You save:  DH303.99
L1,234.28
List Price:  L1,763.34
You save:  L529.05
ден3,991.30
List Price:  ден5,702.11
You save:  ден1,710.80
MOP$564.10
List Price:  MOP$805.89
You save:  MOP$241.79
N$1,325.35
List Price:  N$1,893.44
You save:  N$568.09
C$2,576.69
List Price:  C$3,681.15
You save:  C$1,104.45
रु9,334.54
List Price:  रु13,335.63
You save:  रु4,001.09
S/259.58
List Price:  S/370.84
You save:  S/111.26
K267.89
List Price:  K382.72
You save:  K114.82
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,765.85
List Price:  ZK2,522.76
You save:  ZK756.90
L322.98
List Price:  L461.43
You save:  L138.44
Kč1,645.46
List Price:  Kč2,350.75
You save:  Kč705.29
Ft25,709.21
List Price:  Ft36,729.02
You save:  Ft11,019.81
SEK kr749.87
List Price:  SEK kr1,071.30
You save:  SEK kr321.42
ARS$60,034.20
List Price:  ARS$85,766.82
You save:  ARS$25,732.62
Bs483.70
List Price:  Bs691.04
You save:  Bs207.33
COP$271,296.95
List Price:  COP$387,583.68
You save:  COP$116,286.73
₡35,581.11
List Price:  ₡50,832.34
You save:  ₡15,251.22
L1,728.07
List Price:  L2,468.78
You save:  L740.71
₲516,441.87
List Price:  ₲737,805.73
You save:  ₲221,363.85
$U2,647.22
List Price:  $U3,781.90
You save:  $U1,134.68
zł280.50
List Price:  zł400.73
You save:  zł120.23
Already have an account? Log In

Transcript

In this session, let us learn about the next Netflix OSS components. t. c is a declarative web service client. Whenever you want to access a micro service, you need to create a client. The client can be a web applications are another micro service. With pre creating or generating a web service client is very easy. You don't have to use this template.

Let me explain this. Already we have created multiple micro services, one such micro services portal service, actually we have done this. We have created this hotel service. And then we register it with Eureka server. This is the service registry. Then we created another micro service.

I hope you remember this. We created Eureka client. The Eureka client was saying To access hotel service, how actually to identify that it was going in inquiring in Eureka server from there it identified hotel service and we use this template. With the help of rest template we formulated the Ura and try calling the method of Portal service. But with thin clients, it is not necessary. It is very easy to create a declarative web service with the help of pain.

What you need to do is you need to create an interface and annotated with our direct beam client. The implementation will be generated automatically and the implementation will be responsible for calling the appropriate micro service possible with the same claim claim claim also support fallback and load balancing. What is the fallback already we have learned about the size tolerance library hystrix whenever any of the micro service in the flow er in the pipeline is down our if an exception occurs, we don't have to show the exception to the user to the client. Instead, we can just show a fallback method for that interview saturate enable circuit breaker annotation. And we also use accurate hystrix command annotation. With seen claims it is very easy, you don't have to go in for accurate hystrix command.

Finland also supports load balancing, what are the two types of load balancing VFC server side load balancing, which is done by Zul and client side load balancing which is done very well. Basically how this client side load balancing is done, assuming we're having two or more services of the photo service running and if a client wants access a photo service, we added this ribbon what is the purpose of this ribbon we'll go and check in for the server's effectiveness. vCenter Server is up and running properly. Based on that, that particular instance of order service will be chosen. We have done it already, we ran three instances and port number 8182 and 83 we attach ribbon to the client micro Service Ribbon was responsible for choosing the instance from the appropriate server. Here also it can be done very easily by using theme playing.

In this case, we are not going to use at the red ribbon client annotation, we saw that it says same client can do load balancing. So, this is another one then three clients suffer encoders and decoders. If you want to work with JSON or XML encoders and decoders, it is possible with the help of in plain c this last line Spring Cloud integrates ribbon and Eureka to provide a load balanced HTTP client. So, with the help of faith, you can generate or you can create an Load Balancer HTTP client, you are not going to write the client part, you are just going to create an interface with that derived class that is responsible for load balancing. Also, it cannot take care of the fault tolerance, you don't need this template in case of inclined these are these are the things you have to remember. This same slide, you can generate a load balance HTTP client.

That's a lot how to use in our application, I am going to use only one service that is hotel service. And I will create a client LPN client, which will access this hotel service. Together with this, I will also show you how to perform load balancing and fault tolerance with the help of in place. Let me go back to eclipse. The first step is I need to create a spring starter project for spin plan. So let me go to five new spring starter project.

Let me give them Name is pink light and the description has been client accessing hotel service, click Next. What are the dependencies you need Fie together with that this is a micro service. So, you are going to access the service registry for that you need Eureka discovery, if the screen is not available on the top, you can just take it over here you can just type fish automatically you will get it. So, these are the two dependencies. Of course we need hystrix also because we are going to work with circuit breaker, but I will add that in a later stage. Let me click Next and Finish it the application is getting creative application is ready Let me open it in SRC main java.com dot example demo fin client application.

This class should be annotated with at the rate enable syncline Let me add the annotation. If you see in all these projects are all these Applications whatever we have done in know, the enabling part is done in the main class, whether you want to enable, or you want to enable ribbon, or you want to enable circuit breaker, or you want to enable scene claim, all these are done only in the main class. So this you have to remember, as of now, I'm not going to touch the circuit breaker, I am just going to enable screen clients. What is the next step, I need to offer support and service. Already, we have created an application Eureka client, which is accessing the hotel service. So let me copy the package in SRC main java.com that photon app, so I'm just going to copy all the three packages assets and add it to inclining SRC main java file added it now I will just copy the screen client or I will pull the Steam Client to come Dakota left.

Click OK. And I don't need the combat example demo. Let me delete this You're a client application that is from the other one so I'm just deleting that also. Okay, so here is my package structure from that hotel app if you want to get to Socrative comm dot Potala now I want to rename it I will refactor rename it to install portal app with a CSV ad and yeah okay rename sub packages also click ok okay new packages are renamed properly my main classes over I have annotated it with at the rate enable screen client. Next I need to go into the controller, the controller here I have bought it as client portal controller. Let me rename this also refactor rename. I'm just going to have it as saying hotel controller click Finish.

Okay. Basically, in the controller only we are using the rest template to access the micro service. In this case if you see I'm using this template to access portal service, can you see already we have done this But now in this case we are generating our we are creating a declarative web service client. For this we are to create an interface which is annotated with a direct scene client that responsible for accessing hotel service. So, before more I mean changing this controller let us complete that part. So select SRC main Java right click new interface The package is comm dot fin app dot service.

Now let me give the name of fin hotel client and click Finish this should be annotated with a direct same client Okay, from this interface only we are going to access a micro service. So we have to specify the micro service name with a round bracket value equal to what is the service that I want to access hotel service, Hotel heissen service Okay, this is that what is the next step is to add methods to access the method of Portal service, what are the methods that are available in hotel service, I want to get the list of hotels get hotels by city get one one hotel and some brief message it is the same good old story which already we have done. So, now I have to add method to that of the hotel controller. What I can do is I will copy paste the methods from that particular hotel controller.

Let me open hotel service here SRC main Java here, I've got hotel controller. I'm just going to copy all the four methods. Now let me see faces. Why does an interface interface usually have only abstract methods and of course from Java eight static method and the default method let us not talk about static and default here we are going to have the method is abstract methods only. The implementation will be taken care by fee cleaners responsible for calling the methods of total service. So we don't need any additional lines.

I'm going to end it with a semicolon. All these are abstract methods. One method I'm just leaving it off that greet message method. I'm just going to have you hotels get hotels basically and get one hotel here in the end, I will remove this curly brace semicolon control the control dddd Yes. And then next one is one hotel here also and remove it with a semicolon. And this one of course we don't need to remove this control the safe import comm dot c inclined and Faena.

This is the package name. Now I have got three methods which are trying to access portal service. But if you see I have all the methods I'm having request mapping, what is the purpose of this when you want to access the methods portal service you need to know the value of request mapping something like if I want to access all the hotels from hotel service what I have to give hotel service slash our hotels we want to retrieve only one hotel hotel service slash one hotel slash ID by city means water service slash hotel space city slash bamboo This is how I how to frame right. So, here we are trying to create a proxy case which is responsible for calling hotel controller we are just created the interface the implementation is taken care basing client clean client with will create an implementation create your service template path for the hotel service, but our to remove a path based on the path that you have given here.

All these will be taken and embedded to hotel service and will be called by paying clients. So seeing clients declaratively generates the implementation part Now I don't want to have the same method name, let me change the method name find all the first method, then find hotels by city then find one. The record value mapping that you are giving over here should be the request mapping value that is available in hotel controller. If you really notice when we want to call it from a controller, we write the complete URL I have not changed the controller you can see the URL HTTP hotel service slash all hotels slash hotels the city so we are explicitly framing that you are right and we are calling the methods but now in this scenario, we are just specifying the Ura as specifying the value automatically seen clients will frame the Ura because you are given the service name properly, you're given the value of the request mapping in which is available in the hotel country.

Let's automatically be created. Okay, so this is that now let me come to the controller part in the controller part. Now do we need the rest template to be automatic? No, it is not required when you're working with syncline, you don't need a rest template. Also, you don't have to find the Ura also. So now I will remove this list template.

Instead, I need an instance of fee to hotel client to be auto wired. Because we are trying to cause this method from the controller. So I'm just copying it. Come here Ctrl V, Control V. So this is going to be faint line. I'm not going to have a bigger name just a reference a smaller me Okay. What is this like was mapping for if you want to call this particular method of implies you need the request mapping How would you call it http colon slash slash scene hyphen client slash hotel automatically enter into this method from here where to go to go to stay in hotel flights?

Yes, I have given find our method and what is the mapping water service slash all hotels, I hope you can understand this from Finland you are trying to access a method a portal micro service wherein we are trying to access a method of Portal controller. So, whatever value is available over there should be available in the same slide, but in the controller whatever you are giving will be used by the client. Okay, so now here let me just modify the code I don't need the Ura. Now, instead of this using the thin client only I'm going to call the methods which are sitting there. So instead of get four objects, find all Okay, next, here I don't need it. dot find notice basically then here, control these control find one this is also done.

And the last one I don't have so I'm just going to remove it off. I'm trying to access only three methods hotel Microsoft So, I hope it is clear whatever request mapping you have given up as a method of a controller will be used by the client of this particular micro service and whatever request mapping you have given up or that method in an interface will be used to call the micro service, these are these should have the same value as that of the micro service in a micro service I have given some mappings So, that mapping and this mapping should match and also the method signature should match. We were hoping causes returning into stop hotel, find out find out also should return a list of hotels. So, these things you have to remember. So, this is that one more thing is laptop, we need to add this application documents that we copied from Eureka client.

We open application documents here, read me give you the port number of 9000. And the client is Let me say we completed it. Let me go through this application again. In combat cleanup, I have created the main class, the main classes annotated with accurate enable fame client, meaning to specify that this is a client trying to access a micro service, you don't have to create the client it will be generated automatically by screen that has a meaning. Next, I have created an interface that is annotated with address seeing clients. Within this you need to specify which micro service you want to access, try to create the methods which are similar to that particular micro service and try to add the request mapping similar to that of the request mapping available in the controller.

So that also we have that we don't want to access all the four methods we are trying to access only three methods. So that is completed next. In the controller. You don't have to use system place because you are not creating the URL for yourself. It is taken care by seeing clients you have to offer Where the interface that is annotated was accurate in class. So, here we have done it and we are trying to call the methods of that particular interface.

And in applications it wasn't all we are done just we are giving the application name and specifying where exactly the Eureka service registry is all set now, we have to run the service run water service and again faint lines, let me do one day one select Eureka service and it started in port number 876 months. Next, let me run hotel service and finish line. Oh, both the hotel service and fin client are running. You can just take it out here saintliness running in port number nine Sosa and hotel service running Okay, now let us go to the process. I have started the Eureka service registry in 1006 months, you can see one instance of each is running client and portal service. Let me just click the thin client you can just see this I'm getting four not four not found, why I'm not getting this white label, because we have just disabled that you can have a look for white label enabled as false.

Okay. Now, before going into the path, let me go into the scene Kotel controller, I want to give a different path name already this hotel in Moorea coastline. So let me give it a scene hyphen hotel, we can have the same name not an issue, I just want to differentiate it, saying that this is a paean application, let me save to relaunch the application syncline the application is getting started. I'll go back to the browser. Now let me go and do this path. It has to be seen hyphens hotels, right.

You can now see the output. So using same client, we are trying to access one micro service which is nothing but horrible service about the output. Similarly, let me just try it. slash pony it was wrong. It has three free nights and hotels. hyphen by hyphen city.

No by hyphens city slash pony. Oh my god. I have given the name scene city I have given it a string city hotels hyphen city. Save it. I will relaunch the application inclined. I will go back to the Bros. Let me refresh.

I'm just taking the other services available now see, still latest running again I will refresh. Now I have got it. I'll go to the second one. So finishing now I see fit output IQ just getting it I think I have got daily also. Yeah, I'm getting the list of hotels by city based on Delhi. Okay again, the last one Fein hyphen hotel slash 120 fifth.

I'm getting started. buttons are the three are working perfectly fine. So using a faint line, you're trying to access another micro service without using this template. Next, let us talk about the load balancing. So for this, I'm going to run three instances of auto service. And I will show you how actually, the screen client is going to choose one port.

Basically, ribbon is attached to the screen client, you don't have to bother about it. I will show you how to see the port number. For that already we have done it in ribbon. Let me open this ribbon client. In ribbon client ribbon controller, we have added or the auto wire roll a load balancer client. Let me just select that.

I will copied auto wire load balancer client Ctrl C. I will come to this scene hotel controller here also I will auto wire it and I want to get the port number gain here. I'm just going to add it only in one method. If you want to check it out, you can just add it in all the methods that is fine. Can you see it load balancer client dot choose hotel service, which is a micro service and get the port number. This is just to check which port number is chosen by this thin client. That's it.

So this is that I'm saving it. Let me relaunch fin client first. Then I will go to Photoshop I will open application dot html already running in at age six. I'm going to run into other ports 81 and 83. Save it. Now you have to run another instance the application is getting started.

And also I want to run at 83 save it and again run portal service x makes this console now all the services are started. Let us check it out. This is first order service running in at 83 then The other bottle service running in which port number 8081. Then the first foreign service which we ran is running in at 86. And also what about Finland? That is also perfect.

It is running in port number nine tosun on let me go to this browser and minimize this. I will also open the browser and minimize it. Okay. Let me just go to the Sirocco service registry I will refresh. Now you can see one instance of inclines and three instances of auto service are running Auto Service is running in 8086 83 and 81. Okay, based on the availability and the configuration screen client is going to choose any one of this particular port number.

So let us check that out. So here I have given seen hotels are known. Let me press enter. From here only I can just get the port number. That's why I'm just selecting between hotels. You can see the port number itself.

Chosen 8081 now, I will just copy this URL Ctrl C, open another tab and pull that tab as a new window Ctrl V Enter again it has started let us check this out Yes, portal service I will have to open syncline first it was 81 then 86 Can you see that the bottom 81 then 86 now again I will open this and pull this tab Ctrl V interest port number 8083 So, the default one is round robin rule only. So, based on that it is just hotel service, open thinking okay 8186 8386 like that it will continue. So, this is how it does load balancing. I hope this particular Next, let us talk also talk about the fault tolerance, how our fallback method will be called let me go to the Eclipse and maximize it. Then let me stop the services one by one and select photo service and stopping it I have to stop all the three hotel services.

Now, I have bought Eureka server and seen client only the idea over here is fin client is a micro service, which is trying to after photo service hotel service is down. In that scenario, instead of giving a white five in white label error are for not for not for the error, it has to show a callback method to decline. So, let us create a callback method. So I'm just going to end controller let me select the controller. The first thing what you need to do is you just comment this line. Yes, you will be getting an exception.

Okay, so please come on this line first. You're trying to get the port number of hotel service hotel service itself is not running. So it will throw an exception. So first thing is you need to come and this night only then you can just see the fallback method getting called. Next you have to create an implementation class for that interface. So I will open the same hotel client.

And here I have to specify the fallback class name. That class should implement this interface. Again, let me just do that fallback. Come on I have to give fallback equal to same portal client I MPLS. dot class. The class name is too big so I'm just giving it up same client imp as Okay, this class is not created.

So let me just create it. Keep the cursor and create this class. The class should implement same hotel client, click OK and enter Good abstract methods click Finish. Okay, now I have created a class which implements the interface that is annotated with faint line. This class is a fallback class. If the service is down or if an exception occurs, the fallback method will be called automatically.

Now, I need to add code for this. So let me add the code for fine dog, instead of giving null. Well, I can just write this arrays.as list already we have done it in different places. So I'm just going to copy that code Have a look I have added the code just to return a dummy hotel. This is done next let me go into the main class the main class should be annotated with at the rate breaker. So let me add that also.

At the rate, enable circuit breaker. Save it. Okay, this is done and one more modification you have to do is you have to specify that if six is enabled, so here let me add the code for that also feign corner hystrix. Corner enabled give it a true save saved, you will get a warning don't worry about that. Now let us relaunch the same client application Jason is getting started okay, you can just see this hystrix exception is thrown as I told you, when you add a great enable such good record, it will throw an exception in lab for hystrix Let me add the hystrix library dependency also this main form dot XML here I have added hystrix dependency Okay, let me save it I will relaunch again same client there is some error Okay, now there are gone I have added one extra dependency that we start the application again and relaunching it basically okay in frame client I am feel I need to annotate it with a direct component.

I hope you can understand the spot because only if it is a component it will be identified as a spring beam else it will not be identified. So, that was the error I was getting. So, let's I have saved it, I will relaunch the application again while running the application and just getting an error saying that an instance of client imp is not available, because they are not annotated with at the rate component. So, now I have added the application has started let me open the process. I open the browser. I let me go to Eureka, I will refresh hotel instances not running only faint client instances running.

I will come here let me refresh. I must get the fallback method. I am getting the fallback method. Can you see this and again again by city also we will see hyphen city slash Delhi no hotel no city no hotel ID is one and the last one slash one whatever value we give you give you will get us get this as dummy no city was saying dummy hotel ideas was you can even check it out here it's available in scene client imp and yeah this is the same class Can you see the fault the fault and other things. So, I hope you can understand this for fallback what actually how to create a class which implements the interface that is annotated with the red fin class and this class should be annotated with that great component. I have not done that.

So that is why it was giving me an error. So I have corrected it and now it is working perfectly fine so far so far and also is taken care of a thin client. With the help of paying clients. It is very easy to generate a declarative web service client. Till now whatever we have done we were sitting and writing the recording in case of inclined it is not required. You don't need rest template load balancing is taken care very easily and also fault tolerance is taken care by inclined.

That's it said 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.