Introduction
In the previous articles of this series, we discussed how to build the API Gateway in ASP.NET Core. Usually, when using Ocelot, we will store the configuration in the configuration file. It's very convenient for us to modify it during development.
However, after we publish it, we can not modify them it directly due to some reasons!
Ocelot also allows us to store the configuration in the consul so that we can modify the configuration via consul. And in this article, we will discuss how to do it.
If you want to look at the previous articles of this series, please visit the links given below.
- Building API Gateway Using Ocelot In ASP.NET Core - Basic
- Building API Gateway Using Ocelot In ASP.NET Core - Authentication
- Building API Gateway Using Ocelot In ASP.NET Core - Logging
- Building API Gateway Using Ocelot In ASP.NET Core - Rate Limiting
- Building API Gateway Using Ocelot In ASP.NET Core - QoS
- Building API Gateway Using Ocelot In ASP.NET Core - Load Balancing
- Building API Gateway Using Ocelot In ASP.NET Core - Service Discovery (Eureka)
- Building API Gateway Using Ocelot In ASP.NET Core - Service Discovery (Consul)
Step 1
Running up the Consul at first.
For the demonstration, I will use Docker to run up an instance of Consul.
- docker run -p 8500:8500 consul
Step 2
Create a downstream API service that will be used by the API gateway.
We just add some actions and both of them are returning a string.
- [Route("api/[controller]")]
- [ApiController]
- public class ValuesController : ControllerBase
- {
- // GET api/values
- [HttpGet]
- public ActionResult<string> Get()
- {
- return "value";
- }
- // GET api/values/demo1
- [HttpGet("demo1")]
- public ActionResult<string> GetDemo1()
- {
- return "demo1";
- }
- // GET api/values/demo2
- [HttpGet("demo2")]
- public ActionResult<string> GetDemo2()
- {
- return "demo2";
- }
- // GET api/values/demo3
- [HttpGet("demo3")]
- public ActionResult<string> GetDemo3()
- {
- return "demo3";
- }
- }
The next step is to build the API gateway.
Step 3
Creating an empty ASP.NET Core project and add the following two packages via .NET Core CLI.
- dotnet add package Ocelot --version 13.5.2
- dotnet add package Ocelot.Provider.Consul --version 13.5.2
Add a new JSON configuration file named ocelot.json. And here is the contents of it.








MJ EbrahimiPosted Sep 10, 2020, 3:07 PM
Good article. For the record, this article was added to this awesome repository. https://github.com/mjebrahimi/Awesome-Microservices-NetCore
Rohit SharmaPosted Jul 5, 2020, 11:53 PM
Your basic code is not working.