API Gateway with Ocelot using ASP.Net Core - Part 2 - Step by Step setup
Venkateswaran Rama AWS SAA, ICSL, PMI-ACP?, CSM, SAFe Agilist, PAHM?
Startup || Product & Services Delivery Leader || Product Engr. & Implementation Practitioner || Offshore Delivery Setup & Operations || || GenAI, .Net, Azure, AWS, React.js
As I mentioned in my previous article, Ocelot helps in setting up an API Gateway for routing, it can help in other typical API Gateway features like authentication, caching, ratelimiting, Response aggregation (which we will see in the next article).
Let us see how to setup Ocelot to setup API Gateway for routing to other APIs (typically this can be used to route to different microservices applications).
Here are the steps we will follow
Client/Consumer will hit Cms/Content to view the actual api/content. This way, we can achieve API Gateway routing and abstraction over the internal endpoints.
Step 1 - Open Visual Studio and create the API
Step 2 - Model, Service and Controller creation
A simple ContentItem Model might look like
and the Service Implementation with hardcoded data may look like the one below
Note that ContentService contract and implementation should be hooked in the startup of Program.cs itself to be able to receive the objects of it through Dependency injection (in the Controller)
In Program.cs, add the highlighted code (AddScoped)
Below is the actual controller that calls the Service implementation of GetContentItems.
Note : Minimal APIs can also be used (Controllers are not required).
领英推荐
Step 3 - Run the API application and see the result
Now that we have created the Model, Service and Controller that calls the service implementation, the API should run (under the endpoint URL - https://localhost:5001/api/content). Check the launchSettings.json under Properties folder for the port mapped for http. /api/content is by convention the /api/[Controller].
Step 4 - Create another Project, Content.APIGateway
Create another WebAPI project under the same solution and call it Content.APIGateway
Add the following packages through NuGet
Update the code in Program.cs with the following
Create a Ocelot.json file and add the following
The above configuration is conveying the following
Now, make sure the Solution properties have both the projects chosen as startup project (multi-startup). Also, the sequence is correct (the Content.API and then Content.APIGateway)
Now run the application and open the browser and enter the URL (https://localhost:5151/cms/content), we are able to get the api (/api/content) invoked. You can see the results
We will see the Ratelimiting feature and ability to use API Gateway for Backend for Frontend Pattern. While using BFF pattern, we may need at times to handle the Response Aggregation from multiple services, instead of us doing it explicitly in another API by ourselves. Ocelot supports that as well. Let us see that in the next article. Until then, Happy coding !!!
#ocelot #APIGateway
Senior Backend Developer(.Net)
1 个月Also you can watch this: https://youtu.be/s3DyxLb5a_o