PPM.Ocelot

Custom implementation of API gateway which features including dynamic routing, load balancing, service discovery, rate limiting, authentication, and scope-based authorization.

Getting Started

1. Install the NuGet Package

dotnet add package PPM.Ocelot

2. Dependency Injection


builder.Services.AddOcelot(builder);

app.UseOcelot();
        

3. Install Other Dependencies (Due to service discovery)


<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.13" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        

4. Configure Ocelot (ocelot.json, ocelot.Development.json, ocelot.Production.json)

        
{
    "Routes": [],
    "GlobalConfiguration": {
    "BaseUrl": "https://api.mybusiness.com"
    }
}

5. Add Routes

        
"Routes": [
    {
        "UpstreamPathTemplate": "/api/gateway/Blog/{everything}",
        "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
        "DownstreamHostAndPorts": [
            {
                "Host": "localhost",
                "Port": 5027
            }
        ],
        "DownstreamPathTemplate": "/api/Blog/{everything}",
        "DownstreamScheme": "http"
    }
]
        

6. Add Load Balancing

Supports 2 load balancing algorithms (RoundRobin, LeastConnection).
        
"LoadBalancerOptions": {
    "Type": "RoundRobin"
}
        

7. Add Authentication and scope-based authorization

Supports 2 load balancing algorithms (RoundRobin, LeastConnection).
        
"AuthenticationOptions": {
    "AuthenticationProviderKey": "Bearer",
    "AllowedScopes": []
}
        

8. Add Rate limiting

You can whitelist the client IP addresses. To use rate limiting feature, you must set EnableRateLimiting to true. If the limit has over, you have to wait according to the PeriodTimespan.
        
"RateLimitOptions": {
    "ClientWhitelist": [], // IP whitelist
    "EnableRateLimiting": true, // this must be set to true
    "PeriodTimespan": 100, // seconds
    "Limit": 1 // count
}
        

9. Service Discovery (Read for service mesh: CustomServiceMesh)

        
"ServiceName": "auth"