7

I have multiple microservices which are accessible by clients through Ocelot gateway. Inside configuration file there are properties to specify downstream host and port. This have to be done for EACH route.

The problem is that if the service's hostname or port changes, I will have to modify every single route associated with this particular service.

So, the question is, Is it possible to introduce ENV variable inside ocelot.json configuration file? In that case I will have to modify only one ENV variable and all associated routes will be affected.

Here is my current configuration file (I'm using docker-compose so service names are used as hosts):

"Routes": [
    {
      "UpstreamPathTemplate": "/api/v1/signIn",
      "DownstreamPathTemplate": "/api/v1/signIn",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "identity-api",
          "Port": 80
        }
      ],
      "SwaggerKey": "Identity"
    },
    {
      "UpstreamPathTemplate": "/api/v1/validate",
      "DownstreamPathTemplate": "/api/v1/validate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "identity-api",
          "Port": 80
        }
      ],
      "SwaggerKey": "Identity"
    },

What I want:

"Routes": [
    {
      "UpstreamPathTemplate": "/api/v1/signIn",
      "DownstreamPathTemplate": "/api/v1/signIn",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": {SERVICE_HOST},
          "Port": {SERVICE_PORT}
        }
      ],
      "SwaggerKey": "Identity"
    },
    {
      "UpstreamPathTemplate": "/api/v1/validate",
      "DownstreamPathTemplate": "/api/v1/validate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": {SERVICE_HOST},
          "Port": {SERVICE_PORT}
        }
      ],
      "SwaggerKey": "Identity"
    },
  • This feature is missing in Ocelot, but will be very useful. I didn't try it yet but I think it can be possible to workaroud it with `PostConfigure` extension method on `IConfiguration` – Artur Aug 17 '20 at 20:20

1 Answers1

12

I was able to implement this missing functionality with PostConfigure extension method. In my case I prefer to put placeholders configuration into Ocelot.json but you can change the code to look in IConfiguration rather using GlobalHosts

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;

public static class FileConfigurationExtensions
{
    public static IServiceCollection ConfigureDownstreamHostAndPortsPlaceholders(
        this IServiceCollection services,
        IConfiguration configuration)
    {
        services.PostConfigure<FileConfiguration>(fileConfiguration =>
        {
            var globalHosts = configuration
                .GetSection($"{nameof(FileConfiguration.GlobalConfiguration)}:Hosts")
                .Get<GlobalHosts>();

            foreach (var route in fileConfiguration.Routes)
            {
                ConfigureRote(route, globalHosts);
            }
        });

        return services;
    }

    private static void ConfigureRote(FileRoute route, GlobalHosts globalHosts)
    {
        foreach (var hostAndPort in route.DownstreamHostAndPorts)
        {
            var host = hostAndPort.Host;
            if (host.StartsWith("{") && host.EndsWith("}"))
            {
                var placeHolder = host.TrimStart('{').TrimEnd('}');
                if (globalHosts.TryGetValue(placeHolder, out var uri))
                {
                    route.DownstreamScheme = uri.Scheme;
                    hostAndPort.Host = uri.Host;
                    hostAndPort.Port = uri.Port;
                }
            }
        }
    }
}

The GlobalHosts class:

public class GlobalHosts : Dictionary<string, Uri> { }

The usage:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOcelot();
    services.ConfigureDownstreamHostAndPortsPlaceholders(Configuration);
}

The Ocelot.json

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/my-resource",
      "UpstreamHttpMethod": [ "POST" ],
      "DownstreamPathTemplate": "/my/resource",
      "DownstreamHostAndPorts": [
        {
          "Host": "{MyService}"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5000",
    "Hosts": {
      "MyService": "http://localhost:3999"
    }
  }
}
Artur
  • 4,595
  • 25
  • 38
  • I had multiple ocelot files and put the GlobalConfiguration/hosts section in my appsettings.json and then it worked. Thanks! – NicoJuicy Jan 05 '22 at 07:05