【问题标题】:Ocelot API Gateway Custom Aggregator Issue in ASP.NET Core 3.1ASP.NET Core 3.1 中的 Ocelot API Gateway 自定义聚合器问题
【发布时间】:2021-01-22 01:23:10
【问题描述】:

我在 ASP.NET 中使用 Ocelot 实现自定义聚合器,它在 Startup.cs Ocelot 中间件中引发错误。但是,这两个微服务都可以正常工作并独立获取数据。

当我从我的 API 网关调用它们时,它会抛出错误。

Startup.cs

public class Startup

{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot()
            .AddSingletonDefinedAggregator<MyAggregator>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        await app.UseOcelot();
    }
}

这是我的 ocelot.json 文件,用于不同微服务的路由。

ocelot.json


{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/user/getUser",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "44378"
        }
      ],
      "UpstreamPathTemplate": "/getUser",
      "Key": "User"
    },
    {
      "DownstreamPathTemplate": "/product/getProduct",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "44357"
        }
      ],
      "UpstreamPathTemplate": "/getProduct",
      "Key": "Product"
    }
  ],
  "Aggregates": [
    {
      "ReRouteKeys": [
        "User",
        "Product"
      ],
      "UpstreamPathTemplate": "/UserAndProduct"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000/"
  }
}

我的自定义聚合器类

MyAggregator.cs


public class MyAggregator : IDefinedAggregator
    {
      
        public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
        {
            var one = await responses[0].Items.DownstreamResponse().Content.ReadAsStringAsync();
            var two = await responses[1].Items.DownstreamResponse().Content.ReadAsStringAsync();

            var contentBuilder = new StringBuilder();
            contentBuilder.Append(one);
            contentBuilder.Append(two);

            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");
        }
    }

【问题讨论】:

    标签: asp.net-core aggregate api-gateway ocelot


    【解决方案1】:

    您忘记在 ocelot.json 文件中提及您的自定义聚合器。每当您点击 /UserAndProduct 时,Ocelot 都需要知道您的自定义聚合器。

    “聚合”:[ { “重新路由键”:[ “用户”, “产品” ], "UpstreamPathTemplate": "/UserAndProduct" } ]

    ocelot 的最新版本发生了重大变化。使用密钥 Routes 而不是 ReRoutes。您可以使用以下 json 文件。

    {
      "Routes": [
        {
          "DownstreamPathTemplate": "/user/getUser",
          "DownstreamScheme": "https",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": "44378"
            }
          ],
          "UpstreamPathTemplate": "/getUser",
          "Key": "User"
        },
        {
          "DownstreamPathTemplate": "/product/getProduct",
          "DownstreamScheme": "https",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": "44357"
            }
          ],
          "UpstreamPathTemplate": "/getProduct",
          "Key": "Product"
        }
      ],
      "Aggregates": [
        {
          "RouteKeys": [
            "User",
            "Product"
          ],
          "UpstreamPathTemplate": "/UserAndProduct",
          "Aggregator": "MyAggregator"
        }
      ],
      "GlobalConfiguration": {
        "BaseUrl": "http://localhost:5000/"
      }
    }
    

    【讨论】:

    • 我也试过这个,但仍然是同样的错误。
    • 使用“Routes”而不是“ReRoutes”和“RouteKeys”而不是“ReRouteKeys”。我已经更新了我的答案。您可以使用给定的 ocelot.json 文件。它现在应该可以工作了。
    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2023-03-27
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多