1

我正在尝试构建自己的微服务架构,但被困在 API.Gateway 部分。我试图让Ocelot V14.sln文件中找到所有配置ocelot.json 或 configuration.json文件。

解决方案文件设置

Program.cs文件中,我试图将配置文件与此链接中的以下代码合并https://ocelot.readthedocs.io/en/latest/features/configuration.html#react-to-configuration-changes

builder.ConfigureServices(s => s.AddSingleton(builder))
             .ConfigureAppConfiguration((hostingContext, config) =>
             {
                 config
                     .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                     .AddJsonFile("appsettings.json", true, true)
                     .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                     .AddOcelot(hostingContext.HostingEnvironment)
                     .AddEnvironmentVariables();
             })
            .UseStartup<Startup>();

当我运行它时,应用程序会在我的OcelotApiGw 项目中创建以下ocelot.json文件

{
  "ReRoutes": [
  ]
}

问题是它是空的,并且重新路由不起作用。当我将所需的重新路由粘贴到此ocelot.json文件中时,重新路由会起作用,这不是我想要的功能。

我想要的是从不同的.json文件中自动合并配置文件。

任何帮助将不胜感激。

eShopOnContainers 如何使用 Ocelot V12 以这种方式实现它

        IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args);
        builder.ConfigureServices(s => s.AddSingleton(builder))
            .ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json")))
            .UseStartup<Startup>();

如果您需要更多代码、文件结构或其他任何内容,请发表评论并询问。

4

1 回答 1

1

不确定这是否是您要找的东西?这是Ocelot将多个路由文件合并在一起的方式

https://ocelot.readthedocs.io/en/latest/features/configuration.html#merging-configuration-files

我们不使用它,但这就是我们定义启动的方式:

var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddJsonFile(this.env.IsDevelopment() ? "ocelot.json" : "ocelot.octopus.json")
                .AddEnvironmentVariables();

所以我们有我们的标准 appSettings 加上我们使用的 Ocelot 设置,当我们的 Ocelot 实例部署到我们的测试/生产环境(或只是我们的测试/本地环境)时,Octopus 将转换我们想要的各种变量。

这似乎是定义如何处理多个文件的位:

在这种情况下,Ocelot 将查找与 (?i)ocelot.([a-zA-Z0-9]*).json 模式匹配的任何文件,然后将它们合并在一起。如果要设置 GlobalConfiguration 属性,则必须有一个名为 ocelot.global.json 的文件。

不确定是否需要显式定义每个文件(除非它们可以通过像 {env.EnvironmentName} 这样的变量来定义),但这应该很容易测试。

对不起,如果我弄错了,但希望这会有所帮助。

于 2020-03-15T20:11:11.397 回答