API Gateway with Ocelot using ASP.Net Core - Part 2 - Step by Step setup

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

  1. Open a ASPNet Core editor (VS Code or Visual Studio Community 2022 - note this works with V17.0 and above). I am using Visual Studio community 2022.
  2. Create an API, ContentAPI that spews out a set of contents created and another. Data is hardcoded just for quick demo purpose.
  3. Test the APIs by running them (in my case) https://localhost:5001/api/content. Note : Though in the launchSettings.json, by default https is mapped, I have commented it to use only http for demo purpose.
  4. Create another Project under the same solution and call it Content.APIGateway. We will add Ocelot.json and configure the downstream and upstreampath.
  5. Add few lines of code (change the default code) in Program.cs in Content.APIGateway project.
  6. Change the Project as Multi-starter project. Do the necessary changes in the Properties window at the solution level to allow both Ocelot/APIGateway base Content.APIGateway project as well as Content.API project.
  7. Run the solution. [Ideally the API should run and open the app in its own port. Ignore them. Its just a proof to know that both the projects are running simultaneously.]
  8. Open browser and enter https://localhost:5151/cms/content [the upstreampath mentioned for the apis] to view the output of /api/content.

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

Create a New Project
Configure New Project and name it Content.API

By default Configure for HTTPS, OpenAPI Support also is enabled. I have disabled them for this demo

Step 2 - Model, Service and Controller creation

A simple ContentItem Model might look like

ContentItem Model
Service Implementation

and the Service Implementation with hardcoded data may look like the one below

Service Implementation for GetContentItems method

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).

Content Controller

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].

/api/content/ results by directly hitting the internal API endpoint

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

  1. Ocelot
  2. Ocelot.Cache.CacheManager

Update the code in Program.cs with the following

Program.cs contents in Content.APIGateway project

Create a Ocelot.json file and add the following

Ocelot.json file content

The above configuration is conveying the following

  1. The API Gateway will use the /cms/content (with GET http method) to abstract out the actual API (api/content). Upstream is what consumer will get to hit and downstream is what the gateway will internally route.
  2. The overall API Gateway path would be BaseUrl + Upstreampath = https://localhost:5151/cms/content to get the https://localhost:5001/api/content results.
  3. In the Routes element, we can configure any number of APIs and its Upstream path to act as gateway. Ideally, each Microservices or set of APIs working together should have its own ocelot.json version.


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

Results of /api/content/ from API Gateway endpoint /cms/content/

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

Mohammad Negah

Senior Backend Developer(.Net)

1 个月

Also you can watch this: https://youtu.be/s3DyxLb5a_o

回复

要查看或添加评论,请登录

Venkateswaran Rama AWS SAA, ICSL, PMI-ACP?, CSM, SAFe Agilist, PAHM?的更多文章

社区洞察

其他会员也浏览了