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.
2
Answers
you can download json directly , not using config
For json text starts with
{}
is type ofJObject
. For starts with[]
is type ofJArray
. And you need Nuget PackageNewtonsoft.Json
to convertstring
toJObject
orJArrary
Below is a sample of read the first "route" and added it to the json file.