【问题标题】:Ocelot not finding reroutes filesOcelot 找不到重新路由文件
【发布时间】:2020-06-23 10:30:12
【问题描述】:

我正在尝试构建自己的微服务架构,但被困在 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>();

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

【问题讨论】:

    标签: docker microservices ocelot


    【解决方案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} 这样的变量来定义),但这应该很容易测试。

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

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2020-02-11
      • 2021-01-22
      • 1970-01-01
      • 2016-11-19
      • 2020-08-11
      • 2019-12-07
      • 2018-01-26
      • 2018-07-04
      相关资源
      最近更新 更多