An API Gateway is an application/service that is responsible for forwarding/redirecting requests to a downstream service. It is a common practice in Microservice Architectures, however, it also enables you to combine multiple REST APIs provided by different applications into a single HTTP(S) address.
- Creating a simple rest application to act as our downstream application. (Web API Template)
- Creating the API Gateway which forwards the request to the downstream sample application.
Full Project can be found at https://github.com/dominiquekleeven/medium-apigateway-with-ocelot
- IDE of Choice
- .NET Core 3.1 or .NET 5 SDK (Asp.net Core)
Creating the Sample Application
If you’re using Visual Studio or Rider this should be quite easy, all you have to do is to create a default ASP.NET Core project using the Web API template.
You can also find the SimpleAPI at https://github.com/dominiquekleeven/medium-apigateway-with-ocelot
Creating the API Gateway
- Create an empty ASP.NET Core Web Project.
- Install the Ocelot Nuget package
Ocelot is an API Gateway. The project is aimed at people using .NET running a micro services / service orientated…
3. Update Program.cs
Your Program.cs should look like this
4. Create an ocelot.json file in your project's root directory. (same location as appsettings.json)
"UpstreamPathTemplate": "/api/weather" ,
"UpstreamHttpMethod": [ "Get" ]
DownstreamPathTemplate: the endpoint prefix of the downstream service.
DownstreamHostAndPoints: the downstream IP:PORT address. In this case, localhost:5000 which is the address, I’ll be running the SimpleAPI on.
Read the official documentation for more information
Routing - Ocelot 1.0.0 documentation
Ocelot's primary functionality is to take incoming http requests and forward them on to a downstream service. Ocelot…
5. Change the gateway port numbers to prevent a startup error. (You can do this in the Properties/launchsettings.json file.
Start both applications (SimpleAPI and the Gateway)
And that’s about it. You can add as many downstream services as you want.
Don’t like using static addresses for your downstream services? Check out Consul, which easily integrates with Ocelot.net.
Consul by HashiCorp
Technical Complexity Consul Open Source enables individuals to discover services and securely manage connections…
Check out my tutorial about integrating Consul with Ocelot.net
Source Code can be found at the following repository.
Contribute to dominiquekleeven/medium-apigateway-with-ocelot development by creating an account on GitHub.
Thanks for reading.