【问题标题】:Cannot merge Ocelot config files无法合并 Ocelot 配置文件
【发布时间】:2021-07-07 10:56:03
【问题描述】:

根据documentation,我尝试合并我的配置文件,以便它们更具可读性。然而,生成的 ocelot.json 文件并不像预期的那样。我的文件夹结构如下:

Folder structure

下面是这个的文本表示:

.
└── Ocelot route configs
    ├── ocelot.pokemon.json
    ├── ocelot.tweet.json
    └── ocelot.weather.json

ocelot.pokemon.json 文件如下(其他类似):

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/v2/pokemon",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "pokeapi.co",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/api/pokemon",
      "UpstreamHttpMethod": [ "GET" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "MyTestKey",
        "AllowedScopes": []
      }
    },
    {
      "DownstreamPathTemplate": "/api/v2/pokemon/ditto",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "pokeapi.co",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/api/pokemon/ditto",
      "UpstreamHttpMethod": [ "GET" ]
    }
  ]
}

生成的ocelot.json文件如下所示:

{
  "Routes": [
  ],
  "DynamicRoutes": [
  ],
  "Aggregates": [
  ],
  "GlobalConfiguration": {
    "RequestIdKey": null,
    "ServiceDiscoveryProvider": {
      "Scheme": null,
      "Host": null,
      "Port": 0,
      "Type": null,
      "Token": null,
      "ConfigurationKey": null,
      "PollingInterval": 0,
      "Namespace": null
    },
    "RateLimitOptions": {
      "ClientIdHeader": "ClientId",
      "QuotaExceededMessage": null,
      "RateLimitCounterPrefix": "ocelot",
      "DisableRateLimitHeaders": false,
      "HttpStatusCode": 429
    },
    "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking": 0,
      "DurationOfBreak": 0,
      "TimeoutValue": 0
    },
    "BaseUrl": null,
    "LoadBalancerOptions": {
      "Type": null,
      "Key": null,
      "Expiry": 0
    },
    "DownstreamScheme": null,
    "HttpHandlerOptions": {
      "AllowAutoRedirect": false,
      "UseCookieContainer": false,
      "UseTracing": false,
      "UseProxy": true,
      "MaxConnectionsPerServer": 2147483647
    },
    "DownstreamHttpVersion": null
  }
}

如您所见,我定义的路由没有添加。我尝试在互联网上查找此特定问题,但找不到任何东西。我不知道我做错了什么,我们将不胜感激。

【问题讨论】:

    标签: ocelot


    【解决方案1】:

    由于您的不同路由配置文件位于一个文件夹中,您应该确保调用正确的 AddOcelot 方法重载。在这种情况下,应该使用包含路由文件的文件夹名称调用该方法。

    例如:

    config.AddOcelot("Ocelot route configs", hostingContext.HostingEnvironment)
    

    【讨论】:

    • 这确实是答案,非常感谢:)
    • 如何从不同的文件夹中获取 json 文件?像这样的结构 XFolder->ocelot.x.json ,YFolder-> ocelot.y.json
    • 嘿@captainblack,根据 Ocelot 的文档,只要文件以“ocelot”为前缀,它就应该可以正常工作。并以“.json”结尾。所以你的文件应该在配置过程中使用,如果它不起作用,也许你应该创建另一个 Stackoverflow 帖子,人们可以帮助你解决你的具体问题
    【解决方案2】:

    更新:.NET Core 3+Ocelot 17.0.0

    因为AddOcelot 方法需要IWebHostEnvironment,而HostBuilderContext 中没有:

    您需要通过WebHostBuilderContext获取它:

    
    

    【讨论】:

      【解决方案3】:

      我创建了两个不同的目录 Development 和 Production,通过下面的代码,我可以根据开发环境读取 ocelot 配置,生成最终的 ocelot.json 供 ocelot 中间件使用。

      【讨论】:

        猜你喜欢
        • 2020-12-04
        • 2020-05-01
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        相关资源
        最近更新 更多