首页 > 解决方案 > Ocelot 路由聚合:要么不存在,要么没有正确的 ServiceName 属性

问题描述

我正在尝试使用 Ocelot 创建概念证明并查看 API 聚合。

我的 Ocelot 配置看起来有点像这样

  {
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/fee/calculator",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/fee/calculator",
      "UpstreamHttpMethod": ["GET", "POST"],
      "Key": "FeeCalculator",
      "FileCacheOptions": {
        "TtlSeconds": 600, // Time to Live
        "Region": "fee-calc" // Cache name
      }
    },
    {
      "DownstreamPathTemplate": "/api/form/getlexicon",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/form/getlexicon",
      "UpstreamHttpMethod": ["GET", "POST"],
      "Key": "FormEngine",
      "RateLimitOptions": {
        "ClientWhitelist": [], // array of clients not effected by rate limiting
        "EnableRateLimiting": true,
        "Period": "1m", // time period limit applies for e.g. 1s, 1m, 1h, 1d etc 
        "PeriodTimespan": 60, // retry after certain number of seconds
        "Limit": 5 // number of requests in given period
        //"QuotaExceededMessage" custom message to client for quota exceeded
        //"HttpStatusCode" custom http status code
      },
      "FileCacheOptions": {
        "TtlSeconds": 600,
        "Region": "form-engine"
      }

    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:4001",
    "RequestIdKey": "OcRequestId"
  },
  "Aggregates": [
    {
      "RouteKeys": [
        "FormEngine",
        "FeeCalculator"
      ],
      "UpstreamPathTemplate": "/FormAndFeeCalculation",
      "Aggregator": "FakeDefinedAggregator"
    }
  ]
}

我已删除不属于此问题的路线

我的 Startup.cs 有这个

services.AddOcelot(Configuration)
            .AddTransientDefinedAggregator<FakeDefinedAggregator>()
            .AddCacheManager(configCacheBuilder =>
            {
                configCacheBuilder.WithDictionaryHandle();
            });

然后我需要为 FakeDefinedAggregator 创建类,就像这样

public class FakeDefinedAggregator : IDefinedAggregator
{
    public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
    {
        var contentBuilder = new StringBuilder();
        contentBuilder.Append(responses);

        var stringContent = new StringContent(contentBuilder.ToString())
        {
            Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
        };

        return new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "OK");

    }
}

运行 \ 调试应用程序时,我会在app.UseOcelot().Wait();

异常:无法启动 Ocelot,错误是:aggregateRoute /FormEngineAndFeeCalculation 的路由不存在或没有正确的 ServiceName 属性

我们将不胜感激地收到有关此问题的任何帮助。

标签: asp.net-core.net-coreocelot

解决方案


您的路线有 GET 和 POST 方法:

"UpstreamHttpMethod": ["GET", "POST"],

文档说 Aggregation 只支持 GET HTTP Verb


推荐阅读