Ocelot Api Gateway with ASP.NET Core 2.1 and VS Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello boys and girls this is an upper body from hyper transglobal Inc today we are going to talk about the Ocelot open-source dotnet core API gateway if you have to do anything with micro-services I highly advise you to look at Ocelot as it is a great open source solution for building api gateways don't know what an api gateways stick around you're gonna find out okay let's say you're designing an e-commerce application and you want to build that using REST API s now the application itself could be built with a bunch of different microservices you could have your order services you could have your catalog services containing all the product information you could have a reference data service containing currencies geographies you could have inventory service you could have a review service for your customers you could have user management service and if you're building a multi-tenant application you could also have a tenant API service so obviously if you go ahead and open these up as individual services that's just going to confuse your consumers so at that point you build what is called an api gateway that completely unifies all of these services and provides a common access to eat each of these services and that's where the api gateway ocelot comes in play okay so to get this going we're going to build two different api's one is going to be a catalog API that will be hosted on four seven zero zero one and one is going to be an orders API that will be important too and then the final gateway API that we create will be on four seven thousand and the way I want to do this is the catalog API obviously can be accessed on its own the orders can too but then the gateway API offers a unified access so in order to do this we need to build a solution structure and the way I'm gonna do this is I'm going to make a folder called API gateway example and underneath this folder I'm going to make separate projects so we're gonna make a catalog API and then an orders API and then we're also gonna make a keyframe API so we'll build this folder structure next great so now that we have that idea here let's go ahead and build a folder structure so let's navigate to any folder Direction directory that you want and let's go ahead and create a directory called API gateway example and then in here let's build the catalog API and also an orders API and also a gateway API alright so if you navigate to your finder you can see API gateway example you'll see a catalog API gateway API and orders API this is great now let's go ahead and build a new solution for our API gateway years so I'm just going to do a dotnet new and I'm going to build what is called as an SL n for solution if I click OK it's going to go ahead and create the API gateway example solution and at this point I think let's open up visual studio code and just make sure that everything is going good if you don't already have a solution Explorer plug-in and I think you should go ahead and install that visual studio will prompt you there's a vs code solution Explorer plug-in that I would like to turn on here so I'm just gonna go ahead and turn that on and you'll see it drops in some files here just so you know but the great thing now is you can see a solution here and then you'll be able to add new projects or existing projects here I like to keep my projects created using not the command prompt but you can go ahead and with the solution Explorer let's make API projects for the catalog API as well as the orders API next okay so we're gonna go ahead and build the catalog API to begin with so let's go into the catalog API folder and then you can see it's empty right now let's go ahead and do net and then we'll do a web api this will go ahead and create a new web api project in the catalog api folder so if you go back to your finder now you're gonna see the catalog api and you also see that in here a great thing now you can do is you can go to your solution explorer and right click on it and add an existing project and all you need to do is navigate to the folder and then pick the project that you want to add one more thing that we need to do is make sure that this runs off of port 7001 Advait originally planned so i'm gonna remove this HTTP for the time being and just going to focus on putting this on 7000 ago and let's go ahead and add the orders api now i'm gonna go one level and then go into orders api and then again we're gonna do a dotnet new web api this should add the order's API project in here and then let's come back here confirm that it is indeed at it and then let's go and add that to the solution explorer same approach we'll just go up one level here take our orders API and then pick that and then another thing that we need to do is make sure that this guy also runs off port seven thousand two alright so we've created the two API projects okay so now that we have our catalog API created as well as our orders API let's just make sure that we have something meaningful coming out of our controller so let's go to this values controller and rename this guy and let's just call it let's say currencies controller this controller is going to give us all the different currencies that are available in our system so I'm just going to create a few things here so I'm gonna create this controller with the name of currencies the action name API currencies is just going to get us a bunch of available currencies I'm just going to go ahead and delete all of these because we don't really need those okay and then I'm going to just go ahead and create a few currencies here called USD INR for Indian rupees and then I'm gonna do JPY let's just keep this very simple now I could go ahead and create a currency object and then give you all of this information but that would defeat the purpose of actually showing you the ocelot gateway so let's keep this very simple this is the currencies call let's just go into the catalog API and do a dotnet build make sure that all of that is running as expected and it is compiling so you will see that there is something going on here I'm missing something in here I think I should do a dotnet restore oops my bad just I think all right that's report just fine now okay so we have that running mouth less to or.net run hopefully that opens up on 7,001 and then we are able to navigate to API currencies to see their different currencies so let's just go there and do API slash currencies and then you can see the two policies that create perfect right just like the catalogue API I went ahead and created an orders controller inside of the orders API and you can see that I made it HTTP GET with an empty route that will amount to order slash API slash orders and then I created an orders class in here for a change and I added an ID in and a mound and created a constructor and then I'm essentially adding an o1 order with ID 1 of $200 and o2 order with ID 2 or $400 and I'm returning that very simple let's go ahead and do a dotnet run hopefully this runs on seven thousand two and then we are able to navigate to see our order information so let's go and click this guy and then we'll then the API start orders so as you can see the order objects are getting okay so so far we have built a catalogue API controller called currencies and that gives me my currencies and I've also created one for the orders that gives me an order information these are two different micro services one runs on seven thousand one one runs on seven thousand two now we want to create an API gateway that's going to unify these so if you were to go back to the project structure you'll see that we have API gateway example as the parent folder and that we had a solution and then three folders we never did anything with the Gateway API so let's go ahead and create that so I'm just going to go up one level here and get into the gateway API folder and you can see it's empty and I'm just going to do a dotnet neill Web API now it's best to create the size of that API but honestly we're not going to use the controllers in at least to begin with I'm just going to go into the gateway API here and I'm going to go ahead and delete this controller it literally serves no purpose and I'm also going to go in here and I'm going to add an existing project and then let's just go up one level here get into our gateway API and add this project in here as well so that is going to be the gateway API that's going to unify the other two so as decided we're gonna run this off of port seven thousand so let's go ahead and keep that in here and then the launch URL doesn't really even matter at this point so with that you know you're pretty much established the foundation you've pretty much established the foundation for putting ocelot in so there are two things that need to happen when you want to install ocelot one is you need to add the new get package and then the second is that you're going to configure ocelot within the api how we do that we're going to talk a lot okay so let's go ahead and add the nougat package for Ocelot control see here I'm going to clear this and I'm just going to do ctrl shift command shift P and then we can do a new get and then package manager add package so when you do that it's going to ask you for the name of the package you want to install let's do us a lot it's going to look it up and find it once you pick that it's going to give you a little bit of information here on what you need to pick I'm just going to pick the latest table here looks like 1102 is the most stable version and then where would I like to install that obviously that needs to go under your gateway API project so I'm going to pick that guy and it says it's successfully added there to confirm let's go back into our cs proj and here we see the answer lock package reference let's go in there and restore our packages so I'm just going to do a dotnet restore and that should go ahead and restore the ocelot packages I'm going to do a dotnet build make sure everything's still functional it looks like everything seems to be working fine so the next thing that we need to do is configure ocelot and then add the ocelot configuration in the tool files the program and the startup file so let me just go ahead and add a file called Ocelot dot JSON this is the file that is going to tell us a lot how it needs to behave and redirect the various incoming requests to the various microservices so that's the Ocelot dot JSON file and then I also need to plug in the Ocelot middleware in our application so I've created this Ocelot JSON but how do I tell the application that you need to be looking for that file well you navigate into the program dot C S here and you'll notice that this guy is essentially building an Ibis builder and then building and running it so this is a good place where you could go ahead and tell it hey you know by the way you need to do a configure at configuration it's a really weird name but this is the guy that allows you to configure that and then you can do a host and then we will config and then you can do it config dot add JSON file I believe is the name of the method and then you can tell it to look for Ocelot dr. song and think if I've done everything correctly this is all you need at this point and then obviously you gotta go in your startup and then you still need to wire the Ocelot services here and you also need to wired up the Ocelot middle of it so let's go ahead and do a few imports so I'm gonna do also loft middleware and then I'm gonna do a lot of dependency injection it's kind of hard when you don't have this the studio but I'm pretty sure that those are the two you need and then in here I'm gonna do services got add Ocelot and I'm also going to pass it the configuration because that's where it reads the JSON file information form and I'm going to do ab dot use also lock here and then this is an async method I already know of that so I'm just going to make that and they sink and then let's just prefix that with ASIC and I think that's pretty much all you need to get everything set up so so far let's recap what we've done we've created the catalogue API on port 7001 and we've created a currencies controller that essentially gives us a list of currencies we've also created an orders API for 7002 created an orders controller and that gives us a bunch of orders and we went to it and created the gateway API which is pretty much an empty web API project but we also added ocelot as a new get packets to it as you can see here we also added an empty Ocelot JSON file which is going to be the configuration for Ocelot and then we added references to that JSON file so that our system knows to read that JSON file and we also added Ocelot as the middleware and we also added Ocelot services in there so let's just go ahead and do a dotnet build real quick on the gateway API to make sure everything's still compiling and it should so that's good now let's focus on the Ocelot JSON this is essentially going to be the configuration for everything Ocelot related so let's go to the chrome here and then let's get into the get started documentation their documentation is pretty spot-on I really like it literally at the most base level this is what you need you need to have a bunch of rerouting information that tells you where to drive the requests when they come in to the Ocelot API gateway and also the base information for where this Ocelot API gateway is going to run from in our case we are decided this is going to be on for 7,000 non-local o so let's keep that as a base URL and then let's put in some rerouting information so if you really think about it all you need to do is tell Ocelot when to forward request to the orders API and when to forward request to the catalog API very very very straightforward to do let's look back at the documentation and let's go into the routing section here and you'll notice that they have this JSON it's pretty much summarizes how you can reroute this information so you can add multiple of these routes as you can see this is a JSON array let me just add one and then we can talk further so essentially what I'm doing here is I'm saying anytime a request comes to port 7000 I know that that's going to be cataloged API so let's just play around and say anytime something comes from HTTP for 7000 and if it goes to API dark currencies I just want that to go up I want to do API catalog and then I want to do Plan C so essentially let me also remove these HTTP methods but we want to address all sorts of HTTP method but you could potentially just do again if you want you can eliminate certain methods that you don't all I'm saying here is that any time something comes to a catalog API slash currencies it needs to internally direct it to you should be be localhost 7000 slash API slash currencies and it should be seven thousand one actually so let's try this out so if I go ahead and let me go to this terminal miracle I think it would be much easier so I'm gonna do a CD catalogue API oops go up one level and then we're gonna do a botnet run to make this guy run on port 7001 as you can see and then let's go back in here and make sure that this is still working so yep that seems to be working I'm going to close this guy out for the time being and now all I need to do is open another tab and then let's go up one level and go and let's now run that's going to run on port 7000 now if everything went right I should be able to do 7000 and then I should be able to do a catalog - API and then currencies and then that should give me my currency so this is great anytime someone comes with this prefix of catalogue API if you look at the configuration and already knows that it needs to go into that currencies call anytime someone comes with that it needs to this going internally to this particular location but if you kept on doing it this way then you would have to write one for each and every it we are calling that may be the case but if not also that allows you to create catch-all variables so you could literally define a natural variable name now this is not a reserved word you could use true for all you care I just like to call it catch-all so that someone understands that and essentially what I'm saying is the same variable name matches here so what I'm saying is any time someone prefixes anything on ocelot 7000 with a prefix of catalog api anything that follows that will internally be directed to http local o 7,000 slash API slash whatever that prefix was and if it results that great if it can for whatever reason then it will just deal with it and give you a bad request or something like that so let's let's do that now so I'm gonna shut down I'm gonna keep the catalog API on because we haven't done anything let me just do a dotnet build for the gateway API again and then now do dot mad in this case now everything should just work fine and then I should still be able to do catalog API and currencies in here but if I did like currency for example obviously I would say enough time so that's really great I've been able to do that now let's quickly do the configuration for the orders if yes I'm just going to copy this block and we're gonna create a catch-all again but this time we're gonna do it for seven thousand two because that's where the orders API assets and this one is just going to change to what is it and that's literally all you need so I'm gonna I'm gonna break this out I am going to go up one level ORS API and then I'm gonna do a document run on the order's API just to make sure that that's working so let's go in here and do it for seven thousand two slash the API orders let's make sure that that is running let's create another tab here and then we'll do CI and then let's also do a darknet run and build here to build our latest configuration and then do a dotnet okay so now if you were to go to localhost 7000 obviously you wouldn't get anything from there because there is no pages or controllers available so you could create a welcome page or something like that if you wanted to but if I go to catalog chez Pierre slash currencies I'll still be able to go there if I go to orders API slash orders now I can see the orders API index with so this is great this is exactly what we wanted you could now essentially host these on darker and then you could point all your files the corresponding or the JSON to the corresponding location in darker and then you could just simply expose that base URL make sure that no one can have access to these internal URLs and then unify your APs
Info
Channel: HyperTrends Global Inc.
Views: 31,698
Rating: 4.941545 out of 5
Keywords: ocelot, api gateway, asp.net core, api-gateway, microservices, dotnet, c#, microservice architecture, ocelot api gateway, .net core, .net core microservices
Id: kl-NeEAb1WQ
Channel Id: undefined
Length: 24min 41sec (1481 seconds)
Published: Thu Sep 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.