Search code examples
dockermicroservices.net-6.0ocelot

API Gateway using Ocelot on .NET 6 docker container - unable to make it work


I'm fairly new to the microservices architecture and hand cranked my own api-gateway which works, but from research realised the value in using Ocelot as a replacment due to the features it brings. Today I created a new empty .NET 6 api-gateway project and using the Ocelot documentation brought in nuget packages needed to set it up. The end game is to use Eureka, but as I don't seem to be able to make it work I have stepped back to using a direct call the docker based microservice first.

Looking at the documentation I think I've follow it to the letter, but when I launch the container it does nothing, I wonder if someone could point me in the right direction.

My program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseContentRoot(Directory.GetCurrentDirectory())
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        config
            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", true, true)
            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
            .AddJsonFile("ocelot.json")
            .AddEnvironmentVariables();
    })
    .ConfigureServices(s =>
    {
        s.AddOcelot();
        //.AddEureka();
    });

var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
app.UseOcelot().Wait();

app.Run();

my ocelot.json

{
  "Routes": [
    // drugService API
    {
      "DownstreamPathTemplate": "/api/TerminologyServices/SearchDrugs/{filter}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "drugservice",
          "Port": "80"
        }
      ],
      "UpstreamPathTemplate": "/TerminologyServices/SearchDrugs/{filter}",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
      "BaseUrl": "https://localhost:5003"
  }
}
'''

Solution

  • Replacing localhost for the container name used by the docker compose yaml in this case web.api.gateway solves this problem.