0

I am new to microservices architecture and already implemented micro services and API Gateway with single ocelot.json file as shown in below image.

Project structure

You can see there is one ocelot.json file in Gateway.WebApi project which contains routes.

Which one is better option to handle this?

  1. single ocelot.json for all microservices
  2. single ocelot.json for each microservice

Can we do something like we have single ocelot.json for each micro service?

15
  • The question is unclear. Whatever ocelot is, why can't it work with multiple settings files? What is ocelot.json? And what's your problem? The question is full of marketing terms (microservices architecture, API Gateway, all microservices`) that are information-free. Every microservice is a service, and since you used the ASP.NET Tag, an HTTP endpoint. Perhaps you created 40 separate projects for 40 endpoints? 1 project with 40 controllers? 5 projects with 4 minimal APIs each? Commented Jul 18, 2023 at 6:43
  • You may be asking how to use the same JSON file across multiple independent services - store it in one location and load it from all files. Or you may be asking how to use multiple files in the same service. Commented Jul 18, 2023 at 6:45
  • Wait, were you trying to add the entire Implement API Gateways with Ocelot book chapter to your application? The chapter warns at the very top that Envoy is used in the sample application now and even says the JSON schema changed. Besides, this only shows you how to use API gateways, it doesn't say you should do so. And it doesn't show one gateway per service either. Commented Jul 18, 2023 at 6:51
  • 1
    It seems ocelot already provides the functionality which is mentioned here. All you need to name your json files according to the ocelot convention. Commented Jul 18, 2023 at 11:39
  • 1
    @Eldar that's just ASP.NET Configuration code, not specific to Ocelot at all. That's what I've been writing about in the comments. If anything, the docs are buggy because the appsettings.json files are already configured. Commented Jul 18, 2023 at 11:50

2 Answers 2

0

We can Write Multiple Ocelot Json Files:

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); //For Masters Related API
builder.Configuration.AddJsonFile("ocelot2.json", optional: false, reloadOnChange: true);//For Sms Service Related API
builder.Configuration.AddJsonFile("ocelot3.json", optional: false, reloadOnChange: true);//For Login Registration Related API

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0
var configFolder = Path.Combine(Directory.GetCurrentDirectory(), "Configs");

foreach (var file in Directory.GetFiles(configFolder, "*.ocelot.json"))
{
    builder.Configuration.AddJsonFile(file, optional: true, reloadOnChange: true);
}

enter image description here

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.