ocelot配置文件中的环境变量 [英] Environment variable in ocelot config file

查看:168
本文介绍了ocelot配置文件中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个微服务,客户端可以通过 Ocelot 网关访问这些微服务.在配置文件中,有一些属性可以指定下游主机和端口.对于每个路由,都必须这样做.

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.

因此,问题是,是否可以在 ocelot.json 配置文件中引入ENV变量?在这种情况下,我只需要修改一个ENV变量,所有关联的路由都会受到影响.

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.

这是我当前的配置文件(我使用的是 docker-compose ,因此服务名称用作主机):

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"
    },

我想要什么:

"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"
    },

推荐答案

我能够使用 PostConfigure 扩展方法来实现此缺失的功能.就我而言,我更喜欢将占位符配置放入 Ocelot.json 中,但是您可以更改代码以在 IConfiguration 中查找,而不是使用 GlobalHosts

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;
                }
            }
        }
    }
}

GlobalHosts 类:

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

用法:

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

Ocelot.json

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/my-resource",
      "UpstreamHttpMethod": [ "POST" ],
      "DownstreamPathTemplate": "/my/resource",
      "DownstreamHostAndPorts": [
        {
          "Host": "{MyService}"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5000",
    "Hosts": {
      "MyService": "http://localhost:3999"
    }
  }
} 

这篇关于ocelot配置文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆