.net core ocelot配置文件中的环境变量
我有多个微服务,客户可以通过.net core ocelot配置文件中的环境变量,.net-core,asp.net-core-webapi,ocelot,.net Core,Asp.net Core Webapi,Ocelot,我有多个微服务,客户可以通过Ocelot网关访问这些微服务。在配置文件中,有用于指定下游主机和端口的属性。必须对每条路线执行此操作 问题是,如果服务的主机名或端口发生更改,我将不得不修改与此特定服务关联的每个路由 所以,问题是,是否可以在ocelot.json配置文件中引入ENV变量?在这种情况下,我将不得不修改只有一个环境变量和所有相关的路线将受到影响 这是我当前的配置文件(我使用的是docker compose,因此服务名称被用作主机): 我想要的是: "Routes":
Ocelot
网关访问这些微服务。在配置文件中,有用于指定下游主机和端口的属性。必须对每条路线执行此操作
问题是,如果服务的主机名或端口发生更改,我将不得不修改与此特定服务关联的每个路由
所以,问题是,是否可以在ocelot.json
配置文件中引入ENV变量?在这种情况下,我将不得不修改只有一个环境变量和所有相关的路线将受到影响
这是我当前的配置文件(我使用的是docker compose
,因此服务名称被用作主机):
我想要的是:
"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;
}
}
}
}
}
用法:
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中缺少此功能,但将非常有用。我还没有尝试过,但我认为可以在
IConfiguration上使用PostConfigure
扩展方法解决这个问题
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOcelot();
services.ConfigureDownstreamHostAndPortsPlaceholders(Configuration);
}
{
"Routes": [
{
"UpstreamPathTemplate": "/my-resource",
"UpstreamHttpMethod": [ "POST" ],
"DownstreamPathTemplate": "/my/resource",
"DownstreamHostAndPorts": [
{
"Host": "{MyService}"
}
]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:5000",
"Hosts": {
"MyService": "http://localhost:3999"
}
}
}