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
€66.35
List Price:  €94.79
You save:  €28.44
£55.27
List Price:  £78.97
You save:  £23.69
CA$97.91
List Price:  CA$139.89
You save:  CA$41.97
A$107.61
List Price:  A$153.73
You save:  A$46.12
S$94.02
List Price:  S$134.32
You save:  S$40.30
HK$544.76
List Price:  HK$778.26
You save:  HK$233.50
CHF 61.99
List Price:  CHF 88.57
You save:  CHF 26.57
NOK kr773.52
List Price:  NOK kr1,105.08
You save:  NOK kr331.55
DKK kr494.87
List Price:  DKK kr706.99
You save:  DKK kr212.12
NZ$119.04
List Price:  NZ$170.06
You save:  NZ$51.02
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,334.01
List Price:  ৳11,906.24
You save:  ৳3,572.23
₹5,905.12
List Price:  ₹8,436.24
You save:  ₹2,531.12
RM312.96
List Price:  RM447.10
You save:  RM134.14
₦117,219.95
List Price:  ₦167,464.25
You save:  ₦50,244.30
₨19,380.58
List Price:  ₨27,687.73
You save:  ₨8,307.15
฿2,429.14
List Price:  ฿3,470.35
You save:  ฿1,041.21
₺2,412.08
List Price:  ₺3,445.98
You save:  ₺1,033.89
B$405.06
List Price:  B$578.68
You save:  B$173.62
R1,267.31
List Price:  R1,810.53
You save:  R543.21
Лв129.73
List Price:  Лв185.34
You save:  Лв55.60
₩97,779.80
List Price:  ₩139,691.42
You save:  ₩41,911.61
₪261.55
List Price:  ₪373.67
You save:  ₪112.11
₱4,130.77
List Price:  ₱5,901.36
You save:  ₱1,770.58
¥10,901.93
List Price:  ¥15,574.86
You save:  ¥4,672.92
MX$1,414.26
List Price:  MX$2,020.46
You save:  MX$606.19
QR254.33
List Price:  QR363.35
You save:  QR109.01
P948.83
List Price:  P1,355.53
You save:  P406.70
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.50
List Price:  E£4,969.50
You save:  E£1,491
ብር8,583.46
List Price:  ብር12,262.61
You save:  ብር3,679.15
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$67,980.58
List Price:  CLP$97,119.28
You save:  CLP$29,138.70
CN¥507.18
List Price:  CN¥724.58
You save:  CN¥217.39
RD$4,200.06
List Price:  RD$6,000.34
You save:  RD$1,800.28
DA9,338.81
List Price:  DA13,341.73
You save:  DA4,002.91
FJ$158.92
List Price:  FJ$227.04
You save:  FJ$68.12
Q538.38
List Price:  Q769.16
You save:  Q230.77
GY$14,583.53
List Price:  GY$20,834.51
You save:  GY$6,250.97
ISK kr9,640.42
List Price:  ISK kr13,772.62
You save:  ISK kr4,132.20
DH696.78
List Price:  DH995.44
You save:  DH298.66
L1,273.82
List Price:  L1,819.82
You save:  L546
ден4,077.60
List Price:  ден5,825.40
You save:  ден1,747.79
MOP$559.09
List Price:  MOP$798.74
You save:  MOP$239.64
N$1,260.89
List Price:  N$1,801.35
You save:  N$540.46
C$2,566.52
List Price:  C$3,666.61
You save:  C$1,100.09
रु9,423.16
List Price:  रु13,462.23
You save:  रु4,039.07
S/264.65
List Price:  S/378.10
You save:  S/113.44
K280.62
List Price:  K400.90
You save:  K120.28
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,926.57
List Price:  ZK2,752.36
You save:  ZK825.79
L330.17
List Price:  L471.69
You save:  L141.52
Kč1,676.98
List Price:  Kč2,395.79
You save:  Kč718.80
Ft27,187.34
List Price:  Ft38,840.72
You save:  Ft11,653.38
SEK kr769.78
List Price:  SEK kr1,099.74
You save:  SEK kr329.95
ARS$70,112.88
List Price:  ARS$100,165.56
You save:  ARS$30,052.67
Bs483.65
List Price:  Bs690.96
You save:  Bs207.30
COP$308,470.35
List Price:  COP$440,690.82
You save:  COP$132,220.46
₡35,450.59
List Price:  ₡50,645.88
You save:  ₡15,195.28
L1,762.11
List Price:  L2,517.41
You save:  L755.29
₲543,153.02
List Price:  ₲775,966.15
You save:  ₲232,813.12
$U2,992.07
List Price:  $U4,274.58
You save:  $U1,282.50
zł287.83
List Price:  zł411.21
You save:  zł123.37
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.