1

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

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

在此处输入图像描述

启动.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/"
  }
}

我的自定义聚合器类

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

4

1 回答 1

0

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

“聚合”:[{“ReRouteKeys”:[“用户”,“产品”],“上游路径模板”:“/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/"
  }
}
于 2020-10-08T04:43:22.767 回答