Here I am trying to implement Ocelot
for API Gateway for my multiple micro services. I have Seven Micro services which have lots of API almost 550 APIs. While preparing ocelot.json it is a huge file which seems unmaintainable. For which I want to have different json array objects for each services. Those array objects to be inserted to in routes array of ocelot.json. I have a sample ocelot.json like this:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/ObjectHeads/GetObjectHeadFromBudget?{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 59035
}
],
"UpstreamPathTemplate": "/ObjectHeads/GetObjectHeadFromBudget?{everything}",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/ObjectHeads/GetObjectHeadByDepartmentWise?{everything}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44358
}
],
"UpstreamPathTemplate": "/ObjectHeads/GetObjectHeadByDepartmentWise?{everything}",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7000"
}
}
It is working absolutely fine I am able to perform get
, pos
t from different micro services
. Now my concern is there a way to add json array objects in routes array in the ocelot.json? I am bit new to the json as I never worked on it. Is there a way out for managing such stuff. I am sure that somebody may have gone through such situation, there may be a solution. My program.cs looks simple like this.
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddJsonFile("ocelot.json")
.AddJsonFile($"ocelot.{builder.Environment.EnvironmentName}.json");
builder.Services.AddOcelot()
.AddDelegatingHandler<RequestLogger>()
.AddCacheManager(settings => settings.WithDictionaryHandle())
.AddCustomLoadBalancer((serviceProvider, downstreamRoute, serviceDiscoveryProvider) =>
{
return new CustomLoadBalancer(serviceDiscoveryProvider.Get);
});
var app = builder.Build();
app.UseOcelot();
app.Run();
Any solutions and suggestions are highly appreciated please.
you can download json directly , not using config
var path = @"C:\...";
// or maybe you can try this
var path = Directory.GetCurrentDirectory();
var filePath = Path.Combine(path, "ocelot.json");
var json = File.ReadAllText(filePath);
var jObj = JObject.Parse(json);
var newRoute = new
{
DownstreamPathTemplate = "/api/ObjectHeads/GetObjectHeadByDepartmentWise?{everything}",
DownstreamScheme = "https",
DownstreamHostAndPorts = new[] {
new {
Host= "localhost",
Port= 44358
}
},
UpstreamPathTemplate = "/ObjectHeads/GetObjectHeadByDepartmentWise?{everything}",
UpstreamHttpMethod = new string[] { "Get" }
};
((JArray)jObj["Routes"]).Add(JObject.FromObject(newRoute));
File.WriteAllText(filePath,jObj.ToString());