10. Using Zuul Filters

12 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 Zul filters. Already we have learned about soul APA gateway, this whole APA access of undos for all the requests that want to connect to the micro service ecosystem. We have seen an example of using Zul API gateway together with that the Zul has got filters. What is the purpose of a filter filters are used to filter the records that are coming in from the client. You can use filters for logging for authenticating and slide for providing static content are for dynamically routing the request I mean routing the in response based on the request that you have got. What are the filters that are available in routes are how to create a filter in Zul there are standard predefined filters and Zul if you want to create your own filters that are Also possible by creating a class which extends Zul filter, then it will be identified as a spring beam.

Of course, you know how to convert it into a string bass using the red bean annotation, what actually you are going to do in this filter, with the help of request context, you can get the HTTP servlet request and also HTTP servlet response. So once you've got the request and response object, you can perform whatever operation you want. If you want to log some information in the console that is possible if you want to authenticate the user based on the records that is coming in, or if you want to provide a static content if an error occurs. All these are possible with the help of the sisters. These are the four standard filters of Zul pre route posts and pre filters are executed before the request is routed. Route filters will be executed on the routing path for filters are executed.

After the request has been executed, I mean routers then Yara filters will be called when an exception occurs while connecting to the micro system a covert and now, let us talk about the method that has to be implemented, you are creating a class which extends Zul filter. So, it has got four methods which has to be implemented filter type. This specifies what type of filter you are using, whether it is a free route or force or era, you're just going to return a string which says that whether it is a route filter or era filter, then filter order this specifies in which order the filter will be executed related to other filters should filter of course the name itself is a special I mean specifies based on a request, whether you want to filter it or you want to continue with the flow, then this has the functionality of the filters.

Let me just show you that exactly these filters within in Zulu APA We'll show the pictorial representation. Here I've got assume I have a few services, service one service to service three. And this is only a PA, this is a client. The client can be a web client, R can be a micro service connecting to the backend using Zul AP, both are possible. In my case, I'm having it as a web client. So when a request comes in, it will be taken in by what is the first filter, pre filter.

Okay, and then next, it has to go through route filter. What is the purpose of this route? I am checking in for the request. If the request doesn't have the name, what I'm expecting, I don't want to proceed forward, the further to the back end micro service, I can return a static content to that particular application to this web client. Then Okay, When the error filter will be caused, if an exception occurs automatically the CRR filter will be called This is a pictorial representation. Now, let us try this example, already we have done a session on this zulay pH, I am going to work on the same application, we had actually Zul web client to a client connecting to Zul API gateway and the source connects to order service.

So I was having like kind order service and just change it order service and internally this order service was connecting to book service. If you see actually the order services directly connecting to book service if you want if you want to give a try you can also add this book service in Zulu APA and make our service Connect only to Zulu APA through Zulu APA, it can connect to a web service that has one micro service trying to connect to another micro service using the Zulu AP that is also possible. Okay, now, let us go back to a clip. This time, we are going to run Eureka Service order service book service, these two are micro services then Zul gateway and Zul web playing already running Eureka server order service and book service. So the micro service part is done in Eureka gateway for in Zul gateway, what actually we need we need to add the filters, I have already added the filters, let me show you the code it is very simple, you need to create a class which extends Zul filter, see if you just go here, this is my rule, a gateway application right and then I have got filters, I have created, pre filter post filter and route filters.

So what is the first step, create a class which extends all filters and then override the methods which are available there. Let us see the methods one by one first thing filter type, because this is a free pre filter, I have to return three within double quotes. Then want to start this is First filter which will be executed. Suppose if you're having multiple pre filters, you need to specify the order in which they have to be executed filter one filter to filter three means you need to specify the order then shouldn't filter of course, whatever request is coming in should be filtered by this pre filter, then I have got run method, this run method is going to give the functionality for this particular filter. So, I am trying to get the request context you can just have a look request context context is equal to request context dot get current context.

I am trying to get the context of this application with the help of which I am trying to retrieve the request. You can see this HTTP servlet request. One way about the request, I can ident I can call request dot getparameter request dot get URL then request dot get method. So, whatever information you want that you can just give it over here, so, I have got request dot get url.to string Okay, there is one more thing that records dot get URL that also is fine, then return null URI or returning null if you want to return an object that also is possible, this is part three filters. Similarly, I have done for route filter and post filter, no change here in case should filter is true filter on this one, and then filter a filter out which one means what in raw filter, this is the first filter which has to be executed, what is the filter type, this is route filter, so I have to give it a route.

And then now I can just have the same thing in whatever I have got in pre filter, I'm just going to copy it as such, you can write your own logic, this is just a simple demo. So I am just showing how actually the filters work. So here I am trying to retrieve the request and get the warrant. Then next post filter info filter when it will be called only after the request is routed means it is called on the return path. So this will have a response. So yeah, I have got HTTP servlet response trying to retrieve the response object and get the status so it is returned true okay here let me just remove and then should filter through filter order one and what type of filter it is it is post filter.

Now let me save this and then if you see here I am having been annotated i mean i mean that year symbol only in three filters I'm not having it enforced filter out filter why because I have not created them as being that is you have to go to the main class rule gateway APA and here you have to create a create this as a beam and add the red bean annotation are what they can do very simple. You can add add rate component classes. So, this is post filter. I've got route filter Also, if you need to create your filter also, let me scroll up and here I will import style just give it a start. Okay and I think have not got the error filter, I need to create it. So let me just show you now.

Conduct example demo dot filters new class. Class Name is era filter and then click Finish there itself if you want, you can extend it, extend the Zul filters and now you are to override all the methods keep the cursor and add unimplemented methods. Okay, what is the filter type, this is here and then filter order one true here on the top. If we add the status, the response should be response. I'll just copy paste it. Now see, actually I'm getting your symbol in all the three but not an error filter.

I have to add it in Zul API gateway application. I'll copy paste this and I will just change it. So this is the error syntax. See errors Okay all set. Now let us run this application. This is also micro service so select it and run it directly the application has started.

Now I will run this whole web client also select the application right click run as the non service service getting started. The application has started here and just copying it. Let me open the open it in the browser. You can see the application of scattering Eclipse, I will open the browser I am in Eureka registry. You can see here book service order service rule service, these are the three services we ran and they are running successfully. Now I will come to this URL here I'll just change it to Zul web client.

Let me press Enter I must get the first page here. Let me give the author name is Mohan or whatever. And click Search. In here let me just check it out. Exception has occurred anyhow it is fine. We wanted to check in for the CRR filter.

You can see the status we are given it as response dot get status. To say, so I have to be in Zul gateway. And let us check whether three filter and route filter is called. Let me scroll up to the top. What is the exception the exception is forwarding error, it is not able to forward you can see the URL which is given. So this is from the first one is from pre filter.

The second one is from route filters. Okay, what is the next one it has to call for filter if everything is fine, but in our case there was an exception so automatically this calling only. I mean this giving me an exception. Now I'm just returning it let me check. It has to work fine. Now it is working perfectly fine.

I'll scroll down to Zul gateway pH, this one's the server dual gateway application fee. This is pre filter, then this is route filter. And what is this 200 this is post filters. Okay, and now again, like if I just click order now Again Can you see this pre route and post filter. So, in this order only the filters will be called your I thought of showing it using the show hotels where we are not earning hotel service. So obviously this gives you it will be giving an exception so I thought of explaining the error filter using show hotels but instead of that because the request timed out automatically it was giving the exception now I have given show for hotels.

Okay and trying to run it from here that's why but still it has to throw the exception. Can you see 500 entities avoided if you see actually on the top you're trying to call APA slash hotel slash all hotels. This is pre filter. The second one is route filter and the last one is yes filter. So this is all about Julie APA gateway and the filters associated with food. 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.