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"
},
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"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With