ASP.Net Core API Gateway - Ocelot API Microservice

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi. Welcome to CodingDroplets. In this video we  are going to create an api gateway using ocelot in asp.net core. We are currently developing a demo  micro service solution in which there are multiple micro services which uses different databases. So  first let me give you an overview of the solution which we are creating. We have already created the  customer service which uses sql server database. Then product service which uses mysql database  and order service which uses mongodb database. Now in this video we are  developing the ocelot api gateway which will route the api requests to these three  services. Ocelot is a dotnet api gateway. Ocelot is aimed at people using .Net, running a micro service  or service oriented architecture that needs a unified point of entry into their system. However  it will work with anything that speaks http and run on any platform that asp.net core supports.  I'll also walk you through the implementation of ocelot api gateway in a web application. I'll be  implementing it in a blazor application. From this video you'll get an idea of the api gateway  implementation in any type of applications. We'll be able to do the CRUD operations in all the  modules from the web application. CRUD means create, read, update and delete. In this video we'll  also add container orchestration support for the api gateway and the blazor web application. We'll also configure the docker compose yaml file. So before starting you should know one  thing, this video is part of a series named micro service architecture. You can find  the playlist link in the video description to watch the other videos in  this series. So let's get started. I have opened the demo micro service solution  which we have created in the previous videos. In the solution explorer you can see the  solution folder named micro services and inside that we have the customer web api, product  web api and older web api. Also we have the docker compose project which will get created  while adding the docker orchestrator support. Before starting the api gateway development, let's create a list of api request urls which we need to route from the api gateway. First I am opening the customer api controller. In the customer web api project, for  get customers api method the url is the same controller url. slash api slash  customer and the http method should be http get. Then for get by id the http method is  again get. But the url is slash api customer slash customer id. For create method again the url is  same controller url and the method is http post. Next method is update in which the url is the same  with http put method. The final method in customer api is for deleting a customer. For this method  as well the url should be slash api customer slash customer id. The unique id should be there  along with the url and the method is http delete. Now let's open the order api controller. For  get orders, the request url should be slash api slash order and the method should be http  get. Then get by id should have order id along with the url. So the url will be slash api  slash order slash order id and the method is http get. For create and update the url is same as  controller url. Create needs http post method and update needs http put method. Then delete method  needs order id as well. The method is http delete. Now let's have a look in  the product api controller. So getproducts method uses the same controller url, slash api slash product and the method is http get. Then get by id needs product id along  with the url and the method is again http get. Create and update use of same controller  url. Methods are http post and http put. Finally delete method, slash api slash product  slash product id. The method is http delete. Let's have a look on our docker combose yaml file which we have created in our previous videos. We have different services for web apis and its  databases. Now I am creating a new solution folder and naming it as api gateway. Under that folder I am creating a new project for API gateway. Let's search for .net core and we can see an empty  asp.net core project template. I'm selecting it and clicking on the next button. Then providing  the project name here. Let it be api gateway. As before, again I'm using dotnet 6 framework  and unchecking the configure for https checkbox. Now let's create the project. So the project  got created. We can see the project in solution explorer. Now the first thing to do is installing  the library for api gateway from nuget package manager. I am clicking on the browse tab and  searching for ocelot. The library is listed in the first place. Let me install it. It got installed  we can see the library now in the install tab. So let me close all the tabs now. In the program.cs  class we can do the needed dependency injection for ocelot api gateway. We need to create a json  configuration file for ocelot api gateway. We'll do it soon. So here I'm just providing the method  for adding the json file to the configuration. Initially I'm assigning the base path to the  content root folder. We'll be placing the json file in the content root folder. Next providing  the add json file method to add the file to the configuration. The file name is ocelot.json. Currently we don't have this file. But we'll be creating it soon. Also providing add environment  variables which will add an iConfiguration provider that reads configuration values from  the environment variables with a specified prefix. Now I can provide builder dot services dot add ocelot and provide the configuration as the parameter. Also don't forget to provide app dot use ocelot  method. This method can be awaited. Next let's add ocelot.json file to the project. So  I'm adding a new item in the project. We can search for json template here. Naming it as ocelot.json.  In the ocelot configuration file, we need to provide two sections. An array of routes and a  global configuration. The global configuration allows you to override route specific settings. It's useful if you don't want to manage lots of route specific settings. Currently I am only  providing the base url in the global configuration. Let it be localhost:800 for now. I think port  8001 can be used. Anyway we can assign that later. Let it be 800 for now. Now let's configure the  routes. This is the most important section of the ocelot configuration. There are two terms  that we'll be using in this configuration file, upstream and downstream. The term upstream is  used for the incoming request that means the request coming to the api gateway. The downstream  is the request to be routed to the api services. Now here in this route section, we'll  provide how to route a request if the api gateway receives a request in a particular  url. So first let's add the routing details of customer web api. In the customer web api, we  have slash api slash customer url which accepts different http methods like get, post and put. I'm  providing upstream path template in which we can mention the template of the incoming request. I'm  providing the same url for api gateway as well. That means we are saying to the api gateway that, if you receive a request in slash api slash customer url. Then you must route it to the downstream  details that we are going to provide now. Next is the upstream http method. Here we can  provide the methods that we accept for this particular url. So for this particular url  we accepts get post and put http methods. Now we can provide the downstream details. Let  me provide downstream scheme first. We know that all the micro services are using http scheme. Next  is downstream host and ports. It accepts an array. So let me provide the host name first. We can copy  the host name from our docker compose yaml file. Then the port number is 80 only. All the services  will be listening on port 80 in their containers. Next I'm providing the downstream path template. That means the exact url to which the ocelot api gateway should route the request to. In our  case both upstream path template and downstream path template are the same. Next we need to create  one more route details for the customer api service. In customer api controller get by id and  delete methods are using a different url format. The url will also have the customer id along with  it. So let me provide the app stream path template first. It is slash api customer slash customer id  in curly braces. We can use the same customer id in downstream path template as well. So ocelot  will use the value from the customer id in the upstream path template and add it to downstream  request. Next is upstream http method. We only need get and delete methods here. Now let me copy  rest of the things from the previous route details. Here we just need to add customer id in the  downstream path template. So now we have completed the ocelot configuration for customer web api. Let me add a comment here. So that we can identify the configurations easily. Next we are going to  add the routing details for product web api. The upstream path template is slash api slash product. After that I'm providing the upstream http method. Here we need get, post and put. Then downstream  scheme is http. Next is downstream host and ports. Let me copy the host name from the docker compose  yaml file. Then port is 80 only. Downstream path template is slash api slash product. Next we can  add the route details for get by id and delete methods. Downstream path template should also have  the product id along with the url. Upstream http methods are get and delete. Downstream scheme is  http. Then the downstream host and ports. It is the same as before. After that downstream path template. It should also have product ID in it. Next is the routing details for order web api. It is also  in the same way. So let me complete that as well. So the ocelot configuration is completed now. We  can add container orchestration support for the ocelot api gateway project. Choosing docker  compose as container orchestrator and linux as the container operating system. Now the docker file  has been created. Let me open that. Here you can see ocelot api gateway will listen for port 80 in  the container. Now let's open the docker compose yaml file. Here we have the service named api  gateway. Let me add a container name. That would be api hyphen gateway. Next we can add the port   mapping. 8001 for port 80. Then just adding the same backend network as we did for other containers. Now let's build the docker compose project to ensure that there are no errors. Build got  succeeded. Let's also remove the port mappings for other api containers. Now we won't need them. Now onwards we'll only use the api gateway url. Building the project again. Now let's test  the ocelot api gateway. Let me run the docker compose project. Visual studio is creating  the needed containers. So the containers are running now. In the containers window  you can see api gateway container details. I'll also show you the running  containers in my docker desktop as well. So before testing the api gateway, let's open  the database tools first. I am opening sql server. Currently we don't have any databases. Next let me open mysql workbench. Now I'm opening studio 3t for mongodb database. Next we can start testing the api gateway using  postman. On the left side pane named my workspace is already showing the collection named demo  microservice solution that we have created in our previous videos. Under that collection  there are three folders customer web api, product web api and order web api. So first let me  test by creating a new customer. I am just changing the port number. Now ocelot api gateway is using  port 8001. Let the request body be the same. We got 200 okay response. That means the api execution  is successful. Let's see in the sql server. Now the customer record is saved successfully in the  database. Let's also test the get customers method. That is also working fine. Next  let me create a new product. Let's use the same body content. Got the successful  response. We can check in MySQL workbench now. Product got added successfully. Let's also test get products method. That is also working  fine. Next I'm creating a new order and that is also done. Let me check in studio3t. Here you can see the order which  we have created now. Let's also test get orders method. So all the methods are working  fine. Next is ocelot api gateway implementation in an application. I'm not going in detail about  the blazor application development as we have done a separate series for blazor development. So if you need to learn blazor, please find the playlist url in the video description. In the  solution, I have added a new solution folder named UI and in that folder I have created a blazor  application which uses our ocelot api gateway. In the blazor project's program.cs class, I have  added http client dependency injection and you can see here I have used the api gateway container  as the host name. I have just copied the hostname from the docker compose yaml file. It should be  the same. You can find the source code of this entire demo microservice solution in our github  repository. In the repositories tab you can find demo microservice solution. Now let me add docker  orchestration support for this blazor project as well. Selecting docker compose as container  orchestrator and linux as the container operating system. The docker file got created in the project. Let's open docker-compose yaml file. Here we have the service named blazor server web ui. First let  me provide a container name. Then we can map port 8002 for port 80. Now we can add a new network. So under the network section, I'm adding one more network named frontend. Next I'm adding the  front-end network in api gateway's network section. So the api gateway container will have both  the networks. Now in the blazor application, I'm adding only front-end network. So now we  have two different networks in our solution. All the api services and api gateway are the  part of back-end network. So that they can communicate each other. The blazor application and  the api gateway are the part of front-end network. So that they can also communicate each other.  But the blazor application cannot communicate directly to any of the api services. It can only  communicate with the api gateway. Now let's run the docker compose project and try to do the  CRUD operations from the blazor application. Now the application is running. You can see the  url is having the port 8002 which we have assigned in the dockercompose yaml file. We have three menu  options on the left side menu - customers, products and orders. First let me create a customer. Let the  name be john. I'm providing a dummy mobile number. Then let the email be john@email.com. Now  the customer is created. This customer record is saved in the sql server database. Let me  create one more customer. Let the name be peter. Providing mobile number and email. Now we  have two customers in our sql server database. Next let's add a product let the product name  be product 001. Then the product code be P1. Then let the price be 100. The product has been saved in  our mysql database. Adding one more product. Saved that as well. Now let's create an order. I can  select the customer from this drop down. Next I can select a product from the second drop down.  This text box is for providing the quantity. The plus icon is for adding the product to this order. We can also add one more product in the same order. Now that is also added. Let's save this order now. You can see that the order has been saved. It got saved in our mongodb database. We can see the order  details by clicking on the drop down arrow here. Also we have the option to edit the order. I am  just changing the customer from this drop-down and saving it. Now the record got updated. We  have the edit option in other modules as well. Let me edit a product and modify the values it. Got updated now. Also I'm editing the other product. It is also updated now. Let's try to edit the  customer. Instead of john I am providing john thomas. Now you can see the customer data got  updated. We also have a delete option in the application. Let's try to delete the order. It  got deleted. Let me delete the products now. Next we can delete the customers. So all the  functionalities are working perfectly. So now we have completed developing all the applications in  the demo micro service solution. Next we will see some more advanced things in our upcoming videos. So that's it for this video. Hope you liked it. Please subscribe, like and share this video. See you all in the next video. Thank You!
Info
Channel: Coding Droplets
Views: 18,540
Rating: undefined out of 5
Keywords: asp.net core api gateway, ocelot api microservice, .net core api gateway, api gateway, ocelot api gateway, ocelot api gateway .net core, ocelot asp.net core, ocelot .net core, api gateway asp.net core, ocelot api gateway asp.net core, ocelot api gateway microservice, ocelot api gateway docker, ocelot api gateway container, .net ocelot, .net core ocelot, ocelot api gateway implementation, implement ocelot api gateway, asp.net core api routing, ocelot api upstream
Id: k4l3Ptd4yjw
Channel Id: undefined
Length: 21min 4sec (1264 seconds)
Published: Tue Jun 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.