繁体   English   中英

Ocelot API Gateway Custom Aggregator Issue in ASP.NET Core 3.1

[英]Ocelot API Gateway Custom Aggregator Issue in ASP.NET Core 3.1

我在 ASP.NET 中使用 Ocelot 实现自定义聚合器,它在 Startup.cs Ocelot 中间件中抛出错误。 但是,这两个微服务都运行良好并独立获取数据。

当我从我的 API 网关呼叫他们时,它抛出了以下错误。

在此处输入图像描述

启动.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 文件,用于不同微服务的路由。

豹猫.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/"
  }
}

我的自定义聚合器 class

我的聚合器.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");
        }
    }

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

“聚合”:[{“ReRouteKeys”:[“用户”,“产品”],“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/"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM