API Gateway using Ocelot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay sure so uh hi everyone so this is um one of our series for learning microservices um the topic that i cho i choose to discuss today is api gateways i'm not sure if any of you are familiar with the pattern of api gateways but it is used to solve a particular problem in microservices so i'll open my notepad them or something so for example um boost has several services coaching rca performance reviews um initially uh boost has been created as a monolith so all of these are under a single uh the initial two modules are under a single monolith structure um the problem that i have right now is that i need to work with the team so it would be a collaborative development um we all know that collaborative development is really hard to do if you're working on a single source code um in a single project because you'll be uh you'll eventually bump to each other and then you'll have merge conflicts that you'll have to resolve um it's time consuming it's not what you call this it's not effective uh in my opinion so uh to solve that problem um we're gonna break boos into uh services into microservices so we have the boost the primary boost which is the spa this the front end so each services that we have will be converted to a micro service uh the micro service model that we're going to follow is a one database per service pattern so one db service service now you have a bunch of um apis now so you have two options to do this you can create individual apis so let's say you host this in iais your your spa file so and then all your apis will be hosted as a sub site that would work the problem with that is that you'll have to replicate your logic for authorization and authentication in other stuff here so yeah so that you'll have a redundant code but that works that's a that's a pattern in a microservice so you could do that um for me i don't want to deal with that i want to use something called an api gateway so the concept is similar to subsiting in iis but instead of iis handling the uh what you call this the aggregation so uh you'll handle it via code okay by the way there is also api gateway in amazon and aws you can also use that service but it's not you can't work on it offline so i don't recommend it for our development i recommend using an api gateway provider in c sharp which is called ocelot so what does an api gateway do so an api gateway do is that it acts like a reverse proxy or a proxy if you prefer that term uh put out all the requests to uh mini to our other services so for example um let's see there's a diagram hold on where is the background big picture event service battery console so there so if you see this the idea is each services so let's say each services is an api hosted under a single iis but it's not publicly exposed we'll have an i uh another application this house is the ocelot and then it would drop all the uh the requests to other apis belonging to the same server it could be the same server it could be a different server um it could be anywhere but definitely you can drop it somewhere so that's the idea that's what we want to solve is that notebook so there so yeah so it's just routing uh so how does that solve my initial problem of collaboration by using an api gateway uh i can work on this and i wish i could work on this so the only time we would need is um is in the front end so one could take turns with that but that's less minimal as opposed to working in a giant monolithic code so we have varying approaches and how we code that's totally up to them and how they're going to code their apis but as long as it returns a specific endpoint then i think we can immediately integrate our working solution so we won't run into conflicts hey i'm going to update the database schema to this and that we don't have to collaborate with that anymore because we have one database per service and it's isolated by domain so there's that um with all that talk i'll give a quick demo on how to set up austral um before i start um let's see what do i have here uh in my local machine i have epms api i don't know if you're aware of epms api but basically uh it's this one so it houses the epms employee information it's an endpoint so that this is already in production we can we can use it so we have uh this endpoints okay now i'm going to create a new project to show you how to uh set up in ocelot uh i'll create a new one oh let's just file new project so i'm gonna select asp.net core web application project s here and i trade so it's gonna ask uh i'm gonna use an empty one i don't need uh let's just say this one so it's automatically pre-configured for me so there so i have a controllers i have this weather forecast it's a demo but we won't need it so i'll just delete and it i have no need for those um to get started with ocelot you need to add a pr uh the new nougat package oh not this one [Music] search for ocelot here this one and you select this and you click install so this is the provider it's by tom palister there you go now in your startup you need to say you're gonna i don't think you need this anymore because you're not using any controllers services that and then here i don't think we also need this we just need that use select and we have to say wait uh the weight command is there to handle the asynchronous pattern uh internally this is uses uh this uses an async await pattern so uh long story short it prevents the deadlocks happening from your code within so you can read more about the documentation if i go further it'll take much of our time so i'll skip that but this is required for you to set it up in your code so that's not the only setup that you need so we i we added in the services we added oscillate here in our configure you also need a configuration file you need to add a json file where is that and let's say that jason yeah so you now have this bare bones uh oscillate json and we need to copy the configuration file so configuration file contains this yeah so this is the oscillate file and then you need to register that here this it says copy uh you need to add this line that specific line so what that line does is it registers the uh oscillate configuration so it added uh it added this file so let's just say we built it okay now um recall earlier that i have the epms api so there let's just go here so i don't have to type so there so let's just say i type all of it so there are specific um keywords here in the json structure so in your routes you have to define the downstream path downstream down stream scheme in downstream hosts and ports so downstream when we see downstream this will be your source in in a server somewhere so it could be in a remote server it could be the local host so as you can see i have epms api in what you call this in my machine so i added that uh i need to route for example in ocelot i have this hosted somewhere when i call slash employees api it should return me the employee's endpoint okay so let's try that oh and by the way this is another important configuration i forgot to explain in ocelot you have to define the base url the base url is the url that ocelot would be running in in your client it can't be the local uh what you call this it can't be the local base url it should be the front facing one so for example if you're under a load balancer url and then it's registered with the name it's gonna be a sample sample.taskcast.com so that's what you need to put here you need to put the actual one so for production you'll have to do that uh why is that needed uh ocelot needs that so that it can translate the incoming url addresses and map it properly to this downstream path okay so going back to routes downstream is where you're gonna source the api upstream path is what you're gonna map it to so for example i receive a request of employees it's a get method uh i should return this downstream path source from this uh domain so let's try that right now is it running [Music] okay it's running so here um it's running in 5001. i don't have anything map under um the default one so i'll go and call employees now if i go call employees um it worked i'm connected to my epms api but as you can see here i am missing an api key so how are we going to add those so luckily oscillate is very flexible enough to allow us to add our own configuration so it's very simple you just add a delegate handler so you can add it per route or you can make it global so for the specific instance i need it just for this specific route so how did i register that so in the startup i added a delegate handler and i register a class here um if you want to make it global you just say through here but you're not going to do that so the epms handler is here um it's a middleware um it executes prior calling the downstream path so what i have here is that what i'm telling you is that in my requests i have headers i want you to add the api key api key headers which is x dash abe and then the password so there so once we have that i've registered it in my oscillat configuration upon running this i should now be able to go through it okay so it's empty slash there you go so now it loads so um if you're worried about the overhead so um the overhead as for what they stated is somewhere around two to three ms so that's very negligible compared to the benefits that it would provide in a development um yeah but there's that so yeah so that's how it works so let's try adding uh more endpoints so let's say if i want to use another endpoint so this is a very specific endpoint let's use this one so i'll stop this i'll replicate this routing so in this route that i'm going to add i'm going to add uh employees slash example so it's a very long url i'm just gonna use it here and then if you see this uh i think that's how you map it so i'm just guessing but i think it will work uh so its employees slash employee number okay so let's try that running it's running and then let's say slash employees slash 301 for dating zero i hope that works and it worked so there you go so this is my employee number i've successfully routed it by the way you can use wildcard so you don't have to create a manual mapping here you can read more in the documentation of ocelot so i'm just giving you the idea of how to use it so if you have multiple apis you can definitely use this for example you want to integrate epms api to your application but you don't want to create a bunch of http client within your application you can use this pattern so so there's that um what the so i've added the handler um there are more specific and advanced use cases here where you could check the claims so for example um you would have authorization in place in your up uh in your api gateway so you don't have to put authentication authorization in your microservice and then you would require this specific endpoint to just have uh what you call this a role of admin you can definitely do it by mapping here so require role claims uh something it's in it's there in the documentation there claims transformation add claims to requests uh for example you have claims in your uh identity you can transform them you can put them as a header you can put them as part of the query string and so on and so forth um and for the authorization part so where is that authentication where is it request segregation oh there so it's very straightforward so once you register how you do your jwt so you can just say in this route i would require the user type or let's say roles that's the cleaning and then say admin here so you can do that so i don't have to duplicate all my so going back to my initial problem i solved the duplicate code issue i don't have to put authorization and authentication measures in my api although i could but it's much more centralized now my microservice can be now reused by other what you call this other uh web services that we have uh without worrying on how to uh integrate an authorization thing um yeah so basically that's it it's straightforward that yes any questions from the team hello guys do you have any questions so cj basically ocelot is a wrapper for our api and can you provide swagger yes definitely you can so as you can see earlier um this is the basic uh what you call this the net course startup page you can register any middleware that you have here so since i guess middleware and a service that you could register you could definitely use it here so they have that use case in their demo site we definitely have to worry about database because there are uh many common tables that must be utilized among different microservices so that is the duplication here or do you think that can be available uh sorry can you can you uh db duplication database tables duplication because as you mentioned in each microservice we have a separate database for ourselves so there might be tables that is needed in each of those apis so those tables will be duplicated among apis yeah so the idea in microservices it should be standalone as much as possible so uh with what you're saying is a valid concern yes there would be a duplication but definitely that redundancy is required so i'll show you a use case why that is important well done um i still have some time hold on so i can use this where is that so slow so oh by the way guys while waiting for this don't access the dev server uh as infosec to scan it and there might be a malware inside so if you already copied files within it just don't execute those files and delete them as soon as you can okay here we go so um what abhishek was saying is that there will be redundant table so definitely that would be your case um yeah so as you can see here there is employees uh what you call this um employees database employee stable here and rca root cause analysis is part of uh boost it's a micro service uh of boost so there it also has its own uh employees table so that's the redundancy that uh abhishek was talking about so why is that necessary in a micro service uh this is to make them not so uh dependent so for example if i'm just going to get data from what you call this from this service let's say i want to get all the employees who has uh coaching logs created so i can definitely do that because all the data that i require are here and the copies um they're not inserted there not unless they're really added so for example a coaching log for for me my employee record would only be created if a coaching log was created for me it's not going to be created in uh what you call this in a synchronized manner where we have epms let's say we don't need to dump the entire epms employees data here in your application you don't need to do that you would only add them um if you have the need for their data so there's that uh i hope that answers uh your concern abhishek yeah i get that but we are not going to publish the data but if schema will be there that's what my concern is but uh yes sure we can handle it yeah thank you okay um yeah so hey by the way guys api gateway is should not what you call this it's not uh what you call this it's not a mass for every project that you're gonna create api gateway solves a specific problem it's not uh one solution it's not a ultimate solution for all it should be used with a careful consideration because by doing this you add uh complexity to your application so instead of being a monolith you remove the simplicity and you move to a more complex architecture but the complex architecture trade-off is that you can now work in teams more efficiently so so there's that uh you must be you must have that mindset so and before considering changing your application to uh entire microservices architecture another consideration is that it's best that you start with the monolith because you can definitely see the segregation in the services and then branch out uh later on so that's and that's a better path to take instead of immediately starting with the micro services architecture because uh that added that adds a lot of what you call this boiler plate and complexity to your initial project and you might not you might take a long time just to create an mvp other than that uh that's pretty much it so if you have any questions if you want to explore it more just go to oscillat so just search for ocelot in google or select in c sharp it will be the first one that would appear there um any other questions from the team before we conclude uh by the way in our next session what i'll be showing is how to use api gateway in conjunction with the spa so there's that [Music] oh so any questions from the team okay i think that's good uh i hope you learned something new uh santosh uh we're done thank you cj and thank you team uh cj i'll be arranging next session on tuesday or thursday when you want thursday i prefer thursday okay i'll arrange a session on thursday
Info
Channel: KabezangPuyat
Views: 1,100
Rating: undefined out of 5
Keywords:
Id: 3gXXvy3Gbjg
Channel Id: undefined
Length: 26min 43sec (1603 seconds)
Published: Sun Sep 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.