.net-core - ocelot 配置文件中的环境变量

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

我有多个微服务可供客户通过 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"
    },

最佳答案

我能够使用 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类(class):
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"
    }
  }
} 

关于.net-core - ocelot 配置文件中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63421563/

相关文章:

c# - 如何为 Swagger UI 定义参数的默认值?

c# - 将 google 身份验证添加到 **现有** .net core 2 web api 项目

c# - 在 ASP.NET Core Web Api 中使用 JWT 授权检查 IP

c# - MySql.Data.MySqlClient.MySqlException :“Access denied for user ' hobo' @'49.70.207.34'(使用密码 : YES)”

c# - Selenium 与 .net core : performance impact, IWebElement 中的多个线程?

c# - 如何解决dotnet核心中的nuget依赖 hell ?

security - 如何从包含 .Net Core 中的私钥/公钥的 PEM 文件中导入 PKCS1 key

c# - 将 Ocelot 16.0 与 ASP.Net Core 3.1 集成无法正常工作,因为我需要将 Swagger 与 Ocelot 一起使用

kubernetes - 微服务 API 网关和 Identity Server 4 kubernetes

c# - 下游主机的 Ocelot 占位符