【问题标题】:Environment variable in ocelot config fileocelot 配置文件中的环境变量
【发布时间】:2020-12-04 20:39:39
【问题描述】:

我有多个微服务可供客户端通过Ocelot 网关访问。在配置文件中,有一些属性可以指定下游主机和端口。这必须为每条路线完成。

问题在于,如果服务的主机名或端口发生变化,我将不得不修改与该特定服务关联的每条路由。

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

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

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

【问题讨论】:

  • Ocelot 中缺少此功能,但会非常有用。我还没有尝试过,但我认为可以使用 IConfiguration 上的 PostConfigure 扩展方法解决它

标签: .net-core asp.net-core-webapi ocelot


【解决方案1】:

我能够使用PostConfigure 扩展方法来实现这个缺失的功能。就我而言,我更喜欢将占位符配置放入Ocelot.json,但您可以更改代码以查看IConfiguration,而不是使用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 文件并将 GlobalConfiguration/hosts 部分放在我的 appsettings.json 中,然后它就可以工作了。谢谢!
猜你喜欢
  • 2020-05-01
  • 1970-01-01
  • 2020-01-24
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2015-03-27
相关资源
最近更新 更多