Building and Designing an API Gateway

An API gateway is a server that acts as an intermediary between clients and backend services. It receives requests from clients and forwards them to the backend services. An API gateway can also perform other tasks, such as authentication, monitoring, load balancing, caching, request analysis, and logging.

The BFF Pattern for Aggregating Multiple Services

An API gateway can aggregate multiple services based on the BFF (Backend for Frontend) pattern. When a client requests data, the API gateway can call multiple services to fetch the data and then aggregate it into a single response returned to the client, reducing the number of round trips between the client and backend services and thereby improving performance.

Common API Gateways

Nginx, Envoy, Ocelot, Yarp, Zuul, Kong, Traefik, Tyk, Ambassador, Gloo, AWS API Gateway, Azure API Management, Google Cloud Endpoints, Apigee, MuleSoft Anypoint API Gateway, WSO2 API Manager, TIBCO Mashery, 3scale API Management, Akana, CA API Gateway, Dell Boomi, DreamFactory, IBM API Connect, Mashape, Mashery, MuleSoft Anypoint Platform, OpenLegacy, Oracle API Platform Cloud Service, Repre.

Open-Source API Gateways Based on .NET

Ocelot and Yarp are open-source API gateways based on .NET that can be used to build and design API gateways in a microservice architecture. Yarp provides many features, such as routing, load balancing, authentication, authorization, rate limiting, circuit breaking, retries, caching, request forwarding, request rewriting, request logging, response caching, response compression, response rewriting, response logging, monitoring, tracing, cross-origin support, security, performance, extensibility, configurability, manageability, observability, testability, deployability, maintainability, and upgradability.

Github open-source repository

Official reference documentation

Build and Design an API Gateway with Yarp

dotnet add package Yarp.ReverseProxy

Configure Yarp with a Configuration File

{
  "ReverseProxy": {
    "Routes": {
      "testserviceRoute": {
        "ClusterId": "testServiceCluster",
        "Match": {
          "Path": "testservice/{**remainder}"
        },
        "Transforms": [
          { "PathPattern": "{**remainder}" }
        ]
      }
    },
    "Clusters": {
      "testServiceCluster": {
        "Destinations": {
          "testServiceCluster/destination1": {
            "Address": "http://identityservice"
          }
        }
      }
    }
  }
}

Service Discovery in .NET

Service discovery is a mechanism for discovering and locating services; it helps clients find a service’s location and address. Service discovery can be used when building and designing API gateways in a microservice architecture, so that clients can access backend services through the API gateway.

Using Service Discovery

https://learn.microsoft.com/zh-cn/dotnet/core/extensions/service-discovery

dotnet add package Microsoft.Extensions.ServiceDiscovery --prerelease
builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(static http =>
{
    // Turn on service discovery by default
    http.UseServiceDiscovery();
});
{
  "Services": {
    "catalog": {
      "https": [
        "localhost:8080",
        "10.46.24.90:80"
      ]
    }
  }
}

Using Service Discovery in a Reverse Proxy

dotnet add package Microsoft.Extensions.ServiceDiscovery.Yarp