Spring Cloud Gateway with Resilience4j and Redis rate limiter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
spring cloud gateway is nothing but an api gateway which is implemented by spring for its spring cloud framework so let us first see what is an api gateway an api gateway is basically an api management tool or a service which sits between the clients and the macro services or a monolithic application right so this api gateway will be a single point of entry for an application so client will only have to invoke or interact with this api gateway and the job of this api gateway is to take the client's request and route it to particular micro service also double macro services to get the response the client needs this way api gateway actually provides an abstraction between the micro services architecture or a monolithic application architecture and the clients so here i have multiple micro services listed here for example trip service payment service map service and bunch of other services are there and all of these micro services are sitting behind an api gateway so the client which will be interacting with this application will not be interacting individually with trip service or payment service or map service for example they will only interact with an api gateway and its api gateway job to decide which micro services needs to be invoked to return the particular response the client needs so let's say for example i am the client and i want to access for example let's say this is an uber kind of application a car or taxi hailing application and i want to know what is the payment i need to pay for the last trip i took so i will go ahead and open my application whether it's a mobile application or web application that will be a request which will go to the api gateway and as soon as i go and ask for a payment for my last script a request will get triggered to an api gateway now api gateway will match that particular request to uh related microservices that which services that request needs to go to then once it finalizes that okay this request needs to go to the trip service it will go and invoke the trip service api now trip service will go and see okay for this particular trip what will be the payment of that uh trip so it will go and it will invoke internally the payment service and it will get the response back and similarly it will return back the response to the api gateway so it will return back the response to the client right similarly client two will come and they want to know what is the status of driver which they for the cab they have booked so they will go ahead and they will invoke the api gateway api gateway will in turn invoke the driver management api to face the response and give it to the client too so this is how api gateway sits between the client and the micro services or monthly application and it provides a kind of an abstraction or interface between these two worlds right the outside world which is the clients and the internal uh back-end system of a microservices architecture or a monolithic application now let us see what are the benefits via gateway provides so there are multiple benefits which api gateway provides so i am just listing down few major important benefits so api gateway benefits the first point is basically it provides abstraction between the micro services or the monolithic application and the client right so client will not have any information about which technology that macro services is built into and what is their locations what are their ip addresses what are their each individual urls so they they are not bothered about that and that's why api gateway is there to provide that kind of abstraction api gateway also provides a single point of entry it restricts the access for an unauthorized person okay so for example a person who is not logged in who is not a particular authorized user of the application will not get access to in the macro services or the apis of that particular application because the request will go through an api gateway and api gateway straight away will reject the request if that is not an authenticated or authorized request so in turn it reduces the potential attacks to an application so that is a major benefit of having an api gateway layer then it provides load balancing feature as well right so in many times there can be a search time or peak time where there are multiple number of requests coming to an application right for example let's say there is an office office timing right so the morning and the evening those were the two peak times where multiple employees will be either going to talk or coming back to their home so for example let's say all our uber kind of application which are like taxi services so those will get maximum number of requests in those two peak times those many requests and to evenly distribute those requests to the instances or number of instances of those micro services load balancing feature is needed and api gateway is able to provide that load balancing feature as well that will make sure that the distribution of the incoming request is weighted across all the instances of all the macro services then the next feature which api gateway provides is a rate limiting features all right what is the rate limiting feature so it will basically limit the number of times an api can be invoked in a particular duration right this is the feature which api gateway provides so suppose for example we must have seen multiple uh free websites online online services for example url shortening services or text to pdf converter or those kind of services right which gives us a free amount of request like for like couple of times we can go and we can convert we can convert it will be free of cost but after a certain number of times we have visited that website they will put a cap on that and they will say that okay from here onwards you need to pay or you need to subscribe to our services to continue so those kind of limiting features we can apply to our apis so that kind of feature is basically provided by api gateway as well and spring cloud gateway is also one of those gateway which provides this feature and there are much more feature than this so there are also monitoring feature to see the metrics how many requests are coming into which service is getting the maximum request and all those uh features are also available as part of api gateway so spring cloud gateway is also nothing but an api gateway so how it works basically so this gateway client is basically a client right it can be a web browser it can be a mobile application or it can be a third party application so whenever spring cloud gateway receives a request or an incoming request what it does is it will pass on that particular request to get a handler mapping and this gateway handler mapping what it will do it will read that particular request the path of that request and it will match whether that request is being configured in the spring cloud gateway or not if that request matches to the route which is configured inside spring cloud gateway then it will go and fetch the destination uh url which needs to be invoked right because this will be the single entry point right as a spring as an api gateway so this will know all the locations of microservices what is the ip address and what is the port number on which that microservices is hosted so it will go ahead and find those micro services based on the routes which is being mapped by which is being mapped by using gateway handler mapping once that is identified that which macro services is needs to be invoked then it will go ahead and it will look into the filter of those routes right so there will be multiple filters which can be configured in spring cloud gateway and those filters are basically so if you want to pre-process the request before sending that particular request to the destination microservices right so those kind of filter can be run through and once that filter is done then that request will get proxied and it will go ahead and it will invoke that particular macro service which needs to be invoked and then it will come back again to the gateway web handler if there is any post processing needed before finally making before making a final response then those post process will be done in this gateway web handler in those filters which spring cloud gateway provides and then that particular final response will go and it will be returned back to the client so there are three main components in spring cloud gateway one is route route as the name suggests it will be an api path so this will contains the destination url the microservice url basically which we want to invoke it will also contain a list of filters which can be available for a particular route then we have a predicate predicate is nothing it's just like a java 8 function predicate so it will basically helps us to match whatever is present in the http request so it should be request can have a body strip request can have a header so we can apply such kind of conditions like so that the header should match this or header should match that so those things calm down in predicate then filter is basically spring framework provides a gateway filter okay and that filter basically helps us in uh pre-process or post process of requesting responses okay so in a nutshell spring cloud gateway is basically an api gateway and it has multiple layers one is a gateway handler mapping which will find out which request it needs to get mapped to now let's see what we will build today so first we will implement an api gateway right we will have two services one will be our services which will give a list of countries another will be a service which will give a list of books so those two services will be running in a different port number and different uh as a different spring application and we will implement and spring api gateway which will route our request to those services then second we will implement a circuit breaker in case one of the services is down though and then in that scenario we need to have a circuit breaker in place so that a default response needs to be returned right to the client and third we will implement a rate limiting feature okay now let's go into the id and see how we can achieve these three so i'm in spring initializer and i have given my name of the application i have selected gradle project java 2.4.3 and java 8 the dependency which we will need for creating our spring cloud gateway application is just one dependency which will be our gateway dependency right so this provides a simple but effective way to route apis and this is a spring cloud routing so we'll do this and we will hit generate so i've already generated and imported this application into my id and here it is so as you can see if i open build.gradle so we have a dependency which is uh spring cloud starter gateway dependency right and nothing else so these are like lombok which we will not need in this particular example so this will be our api gateway uh application which will receive all the requests from the client and it will route to two different microservices which i already have created so let me just open those two micro services so those are one is books okay and the other one is countries it just has one or two api and it is reading it from this json file and returning the response back okay so i'll give the link of these two application in my github repository so let me go to the second one which is the books api and it also has one json file and the adjacent file will get converted and it will get returned back as part of response to this apis right at all or at all delayed so we will see what all these apis are so let me go back to spring cloud gateway so the first thing which we need to do here is we need to configure our routes so how spring will know that how this application will know that which application or which macro services it needs to route to those requests too okay so let me create one config package okay and this package will hold our class let's say let me name that class as uh just a gateway okay gateway config okay so this will be our configuration class for a spring cloud gateway so let me annotate it with the configuration now what we need to do is we need to create a route locator so whatever whatever routes we want to configure in our spring cloud gateway those dots we need to mention in this particular configuration so what i'll do i'll do a public route locator okay let me give it a name as my routes and this will take input as route locator builder sorry route locator builder route locator builder okay so this will be a bean okay so this will be returned doubt locator builder dot routes dot build okay so between routes and build we need to map our route so we will give as route and the first route which will have is let me go and see what is the api so we have our countries api let me open that so we have our country as countries api which is uh the path of which is slash v1 slash countries less capitals okay let me go ahead and configure this route in our spring gateway so this is our dot route and inside this we will create our path okay so as you can see here p dot path so inside this path we will be giving our path of that particular api which we want to invoke so let me give as v1 slash country slash capitals okay now this is the path so this is the path spring will spring the cloud gateway will look into it will try to match this path okay whatever we will send as part of the get request it will try to get and it will try to match with this and then it will try to look into this destination url which will provide us http localhost and we will be running that application into 808.1 port number okay so we have configured our first route which the path of which is v1 slash country slash capitals and the destination url which to which this request will get routed to is localhost coron 8081 okay so these two configuration we have done so let me just save this and let me first start that countries services so as you can see the port number in which it is running is eight zero eight one okay let it get started okay it has started now so let me invoke that or basically i'll try to invoke it from here only so localhost 8080 one then we have v1 country slash capitals okay so this is giving us the response of list of countries and the capital of them so this is the api this is the countries api right so we are directly invoking that but we don't want to invoke that we want to invoke through an api gateway so what we'll now do is we'll go to api gateway and we will restart this particular application so let me just go ahead and see what is the port number configured so port number is 8080 so this application or gateway application is running in 8080 okay here as you can see we have this route predicate factory list of dot predicate factories which has been loaded and the application has started on 8080. so let me go back to the browser and do a localhost 8080 and what is the url v1 slash country slash capitals it says not found okay let us go and see why it is showing that and the reason is we have missed the initial v1 i mean slash okay now it has started so let me go ahead and refresh this so as you can see we are invoking localhost 8080 so this is going through our api gateway it is going to hit 8081 this particular api and it is returning back the response so our api gateway is set up and it is working as expected so let us add one or two more uh api in it a route in it so let me just copy this okay so there is one more api in um countries capitals and that will be a post line okay and let me also configure for the books api so for that our api path is books slash all and this application will be running in eight zero eight two okay let me restart this gateway okay as you can see right now we have three route so one is for this country slash capitals api the other one is for country slash coastline api and the third one is books slash all api so the first two api is basically running in eight zero eight one port number and the second api or the last api is running in eight zero eight two port number let me restart that books application as well okay so the book application is up let me see spring cloud gateway is also up now if i invoke capitals it will return me the capitals then if i change it to coastline it will return me the coastline of those countries right so this second api is also getting mapped it is working properly now let me change it to the third one so this will be books slash all and it is giving me the list of books so all of these apis i've been working into 8080 which is our api gateway and this api gateway is taking care of matching these uh route to the actual route of those application which is running in eight through eight one eight zero eight two port number okay so this is the basic api gateway configuration which we can do with spring cloud gateway so obviously there are multiple uh configuration which can do we can apply filters after this path and we can do pre-processing of the request suppose you want to add some request header into the request we can add it suppose we want to add some cookies into the request we can add it or we can or we want to modify something into the request we can do that as very well and also before returning back the response to the client we can also apply a filter here to do a post processing let's say we are getting a response back or we want to append something into the response then we want to do that in this particular filter so those are gateway filter which is provided by spring cloud gateway okay so apart from this java way of doing it we can also configure we can also make this configuration or write this configuration in application.yaml file okay so that that is given in the spring documentation i'll provide the link in the description so basically i like the java wave configuration so i have shown you in the java wave configuration so this is the part where we have configured our api gateway right now let us see how we can implement a circuit breaker in case we have an api which is not responding in a specified time and it will fall back to a default url so let us take this particular api which is the all delayed api let me invoke that api once so this is all slash delayed so this will make the thread to sleep for five seconds and then it will return back the response okay now to implement our circuit breaker there are two there are two kind of circuit breakers spring provides the first one is histrix which is well known that is a netflix uh a circuit breaker and that circuit breaker is now in maintenance mode so basically spring will not gonna provide support uh in future so that's why spring has come up with a new circuit breaker which is resilience 4j and we will go ahead with that particular circuit breaker and we will go ahead and add the dependency for that circuit breaker in our build.gradle file so the dependency is org spring framework cloud spring cloud starter circuit breaker reactor and resilience 4j okay so let me just uh download this dependency i think the dependency has been downloaded so now i have mentioned earlier while discussing spring cloud gateway that apart from this route and filter we have route and particle we have one more important thing which is filters right so that is a gateway filter so spring provides multiple gateway filters through which we can do a pre-processing or post processing of routes so basically here what we'll do what we'll do is basically we want to invoke this ap in this api but if that api is not available we want to fall back to our default url instead of giving up for 500 gateway timer exception to client so we will add a filter so filters then f dot circuit breaker then this will take a consumer in so c dot we will set the name of the circuit breaker as any name let me name it as drive cb okay so circuit breaker and then we have to provide a fallback url so let me provide a fallback url as slash default default fallback let's say okay so this will be our fallback url once the api is not reachable to provide the default implementation of circuit breaker we will provide a customizer bean of reactive resilience for the circuit breaker factory so we will create a public customizer which is coming from spring framework cloud which is coming from spring framework cloud client dot circuit breaker okay then reactive resilience for the circuit breaker factory and we will make it as default customizer cool now what we'll do we will annotate it with beam then you have to return a factory factory dot we will configure the default uh configuration basically so default will be what will be the id so id will be new so this will be new resilience builder of id dot circuit breaker config and inside this config we will mention circuit breaker.config.default of defaults so this one right and in here let me just do this bit okay so after that we have to provide the time limit configure time limiter configuration right so what will the time limit so we want let's say for example you want a duration of two seconds so beyond two second if any request takes so that we'll get the request will get ordered to circuit breaker okay so in time limited configuration what we'll do we'll create a time limit config so time limiter config okay then dot custom dot timeout duration and this duration let's say we will take a duration dot of seconds and we want two seconds okay so let me just segregate this okay so this being we have created now let us restart this application now let me go back here and we have to invoke this api one two so after two second it waited and it has thrown us a white label error base and that is the reason because we have not even provided this fallback url okay so it is trying to go to this fallback url but there is no url present so what we'll do now is we will create a controller let's say we'll name it as gateway controller okay and we will name it as rest rest controller and let me give it as public string default default message and we will return there was some error in connecting [Music] please try again later okay we will return this and the mapping we will be giving is uh sorry this one okay let me restart the server okay so now let me invoke this one two three so as you can see there is some error in connecting please try again later so it tried and after two seconds when it is not getting back the response it is reverting back to the fallback url so if i remove the delayed from here it will go to the normal ui url and it will return back and if i again mention delay it will fall back to the same url all right now we will see how we can actually configure a rate limiting uh feature in spring cloud gateway and we will apply that particular uh rate limiting feature in any of the apis right so for that partial purpose we will be using radius rate limiter so reduce will be taking care of keeping into account keeping or tracking how many requests has been hit and what in what duration so those count on those rate limiting count will be done by redis so for that we will have we will want to have a ready server up and running right so the ready servers which we'll be using here i'll not show how to install and how to start a server so basically you can go and check out my other api or springboard with redis and there i have explained how it is what is reduced and how we can actually configure and install edits and run that ready server so i'll go back and i'll just start the redis server which i already have so let me go back okay okay so i will just go and start the radio server right so now the radio server has been started and it is running on the port number six through seven nine and it is running in uh localhost okay so the next thing which you wanna do is we want to provide the port number of redis right so redis then the host so the host will be basically localhost and then we want to provide the port support will be 6379 right so this is the radius configuration and we have to add one dependency which will take care of making the connection with the radius services which is running locally and which we have provided the information for so this is the uh so this is the dependency spring boot starter data radius reactive so let me just repress the dependency all right so this will take care of making connection to ready server and sending all the details which is required to keep a track of number of requests which has been invoked okay so now let us go ahead and do the configuration for our rate limiting so what i'll do i'll go and do the configuration in here in for the api which is slash books slash all okay so again we have to add a filters then f f dot f dot here we'll have a request rate limiter dot configure and inside configure we want to provide the consumer function to c dot set that limiter and this red limiter we want to set here is reduce rate limiter right so for that purpose what we'll do we will create a bean which will return us a radius rate limiter public uh radius rate limiter reduce red limiter so return new radius red limiter and this will take in two parameters the first one will be your default replenish rate and the second one will be your capacity okay so we'll enter these two thing and we will return back so i'll tell you what these two are let me first add this okay right so now we have added a redis red limiter so apart from this we also have to add one more thing which is known as key resolver okay so key resolver and we will name it as let's say user key resolver okay and return return exchange and then mono dot just mono dot just just and we'll give it as 1 and this also we will provide it as a b okay now let me just restart the application okay now the application has restarted right so before that let me just tell you guys what is these two right default represents rate and default burst capacity so this represents spread is basically the property which says how many requests per second you want a user to be allowed to do so for this particular api for which we are implementing this rate limiter in one second how many requests you want to be allowed okay without any drop request so 10 is the number of requests which is allowed in one second right then what is the default burst capacity so default default burst capacity is the property which is the maximum number of requests a user is allowed to do in a single second okay so in a single second what is the maximum number of requests a user can do okay so this capacity is in one second how many requests a user can do which is 20 and this is represent is basically in one second how many requests you want a user to do without any drop request so out of 10 all of the 10 requests will go so that is the replenish rate and this is the burst capacity so the next thing is key resolver so key is always the name suggest so radius rate limiting will only work if the key is present if the key is not present the request will be denied so for the sole purpose of allowing that request we are just mentioning a bean of key resolver which returns one as a key always so now uh since we have added our rate limiting okay and our ready service is also up let me go back to the postman let me go back here and the api which we added is this one right so let me just copy this go to postman here and we invoke this and in the headers if you see we have this x rate limiting remaining 19 x rate limiting requested tokens which and burst capacity is 20. so bus capacity is 20 which we have configured and the representatives 10 which we have configured so we have invoked one so this 20 minus 1 is 19 so if we again invoke again it becomes 19 19 19 since the burst capacity is 20 right in one second we can make 20 request so that's why we are not seeing any difference here so let me just go back and decrease this let me just decrease this to 1 and the capacity to let's say 2 okay and restart the server so now what will happen the user can only invoke two requests per second right if the user is working more than two requests per second then after two after two requests the third request will uh not be executed and it will throw that too many request error basically so let it start and see okay it has started and now let me invoke this api yeah it has worked so the capacity is two and replace rate is one so if i again invoke one two two let me invoke multiple times yeah so as you can see in between we are getting for 29 too many requests so whenever the request is getting more than two times in one second there we are getting four to nine two main request so here the rate limiting feature is coming to picture so radius is taking it is keeping track of the number of requests which is invoked in one second and as soon as it reaches or it reaches beyond the capacity it will just throw an error saying four to nine too many requests it will not go to uh the particular uh api so this is how we can implement our api gateway we can implement our circuit breaker and we can implement our uh rate limiting feature with the spring cloud gateway so if you have any doubt just put your comments in the comment section and i'll i'll be happy to clear your doubts out till then happy coding
Info
Channel: Coded Tribe
Views: 4,038
Rating: undefined out of 5
Keywords: spring cloud gateway, circuit breaker spring boot, rate limiter spring boot, spring cloud gateway tutorial, spring cloud gateway example, spring cloud gateway rate limiting, spring cloud gateway circuit breaker filter, spring cloud gateway circuit breaker, spring cloud gateway circuit breaker example, resilience4j circuit breaker spring boot example, resilience4j with spring boot, resilience4j circuit breaker, resilience4j ratelimiter, resilience4j vs hystrix, coded tribe
Id: 5eAQLygfmbg
Channel Id: undefined
Length: 32min 18sec (1938 seconds)
Published: Mon Mar 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.