簡體   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