An API gateway is a single entry point that sits between clients and backend services (often microservices). Clients call the gateway, which routes each request to the right service and applies cross-cutting concerns centrally. Responsibilities of an API gateway - Request routing: /api/orders/* to the Orders service, /api/catalog/* to the Catalog service. - Authentication and authorization: validate JWTs before requests reach services. - Rate limiting and throttling, quotas per client. - Load balancing across service instances, with service discovery. - Response caching, request and response transformation, header manipulation. - Request aggregation: combining several downstream calls into one response (a Backend for Frontend role). - SSL termination, CORS, logging, correlation IDs and metrics. - Resilience: timeouts, retries and circuit breakers (QoS). Examples: Azure API Management, AWS API Gateway, Kong, NGINX, YARP (Microsoft's reverse proxy library), Spring Cloud Gateway, and Ocelot for .NET. Ocelot Ocelot is an open-source API gateway for .NET, configured mainly through an ocelot.json file. Setup builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); builder.Services.AddOcelot(builder.Configuration); var app = builder.Build(); await app.UseOcelot(); app.Run(); Example ocelot.json { "Routes": [ { "UpstreamPathTemplate": "/api/orders/{everything}", "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "DownstreamPathTemplate": "/api/orders/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "orders-service", "Port": 443 }, { "Host": "orders-service-2", "Port": 443 } ], "LoadBalancerOptions": { "Type": "RoundRobin" }, "AuthenticationOptions": { "AuthenticationProviderKey": "Bearer", "AllowedScopes": [ "orders.read" ] }, "RateLimitOptions": { "EnableRateLimiting": true, "Period": "1m", "Limit": 100 }, "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, "DurationOfBreak": 10000, "TimeoutValue": 5000 }, "FileCacheOptions": { "TtlSeconds": 30 } } ], "GlobalConfiguration": { "BaseUrl": "https://api.example.com", "RequestIdKey": "X-Correlation-ID" } } Key fields - Upstream: what the client calls on the gateway (path template and methods). - Downstream: where the gateway forwards the request (scheme, host, port, path). - LoadBalancerOptions: RoundRobin, LeastConnection and others; service discovery (Consul, Kubernetes) can replace fixed hosts. - AuthenticationOptions: links the route to an authentication scheme registered in ASP.NET Core (AddAuthentication().AddJwtBearer("Bearer", ...)). - RateLimitOptions, QoSOptions (circuit breaker and timeout, via Polly), caching and header transformations. - Aggregates: combine several routes into one response. Considerations: the gateway must be highly available and should not contain business logic. Microsoft's YARP is a popular alternative for high-performance .NET reverse proxying.
Ravindra Wadile · · 0 likes