What is API Gateway in Asp.net Core Micro Service?


In ASP.NET Core microservices architecture, an API Gateway acts as a central entry point for client applications to access various microservices. It provides a single unified interface for clients to interact with multiple services.

The API Gateway handles requests from clients, performs necessary routing and protocol translation, and dispatches those requests to the appropriate microservices.

Here are some key features and benefits of using API Gateways in ASP.NET Core microservices:

Request aggregation: An API Gateway can aggregate multiple requests from clients into a single request to reduce chattiness between clients and microservices. This helps improve performance and reduce network overhead.

Routing and load balancing: The API Gateway can handle routing requests to different microservices based on the requested resource or operation. It can also perform load balancing to distribute the incoming traffic across multiple instances of microservices.

Protocol translation: Clients may use different protocols or data formats than what the microservices support. The API Gateway can handle the translation of protocols (e.g., HTTP to gRPC) and data formats (e.g., JSON to Protobuf), ensuring seamless communication between clients and microservices.

Authentication and authorization: The API Gateway can handle authentication and authorization for incoming requests. It can enforce security policies, authenticate client requests, and authorize access to specific microservices based on the client’s identity and permissions.

Caching and rate limiting: API Gateways can implement caching mechanisms to improve performance by serving cached responses for repeated requests. They can also enforce rate limits to prevent abuse and protect microservices from excessive traffic.

Logging and monitoring: API Gateways can log incoming requests and responses, providing valuable insights into the system’s behavior. They can also integrate with monitoring tools to collect metrics and perform health checks on microservices.

There are so many api gateways which can we use in asp.net core microservice. like Ocelot, Azure API Management, AWS API Gateway, Kong etc

In this demo we will see basic demo of Ocelot.

Step 1: Create the 3 project layers using Asp.net Core for StudentService, TeacherService and Gateways layer

Step 2: Go to Ocelot gateway layer and installed the ocelot NuGet package.

Step 3: Create Ocelot.json file and configure the routing using Upstream and Downstream like this

{
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5003"
  },
  "Routes": [
    {
      "UpstreamPathTemplate": "/gateway/students",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/api/student",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/gateway/students/{Id}",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/api/student/{Id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/gateway/teachers",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/api/teacher",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5002
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/gateway/teacher/{Id}",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/api/teachers/{Id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5002
        }
      ]
    }
  ]
}

This is main setting configuration file, which will read the input request and route to specific project api Countroller URL.

If you will give

http://localhost:5003/gateway/students

Then It will Call http://localhost:5001/api/student

Again If you type http://localhost:5003/gateway/teachers

It will route to http://localhost:5002/api/teacher

In the above setting we are seeing with use of single api endpoint http://localhost:5003/

we are calling multiple microservice api endpoints.

Step 4: Go to the program file of OcelotGateway project and configure the middleware pipeline like this

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();


builder.Configuration.AddJsonFile("ocelot.json",optional:false,reloadOnChange:true);
builder.Services.AddOcelot(builder.Configuration);
var app = builder.Build();


app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

await app.UseOcelot();

app.Run();

Step 5: Give some dummy data in Student and Teacher We API Controller

Step 6: Make all 3-project layer as Starup project layer.

Step 7: Run the application and test it.

Source code to Download: https://github.com/Chandradev819/Ocelot_API_Gateway_Demo

Advertisement