【问题标题】:.net core 2.1 ocelot gateway returns 404.net core 2.1 ocelot 网关返回 404
【发布时间】:2020-03-24 00:15:49
【问题描述】:

我正在尝试在我的应用程序中实现 ocelot 网关,但它总是在我在 ocelot.json 中配置的所有路径上返回 404。

每当我使用简单调用或聚合调用对 Postman 进行 GET 时,它总是返回 404 并且程序继续运行,它不会崩溃。

Ocelot.json:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/Buildings",
      "UpstreamPathTemplate": "/allBuildings",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "Key": "Buildings"
    },
    {
      "DownstreamPathTemplate": "/api/Device",
      "UpstreamPathTemplate": "/allDevices",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "Key": "Devices"
    }
  ],
  "Aggregates": [
    {
      "ReRouteKeys": [
        "Buildings",
        "Devices"
      ],
      "UpstreamPathTemplate": "/BAD",
      "Aggregator":  "FakeDefinedAggregator"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:44351/"
  }
}

程序.cs:

 public class Program
    {
        public static void Main(string[] args)
        {
            new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", true, true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                        .AddJsonFile("ocelot.json")
                        .AddEnvironmentVariables();
                })
                .ConfigureServices(s =>
                {
                    s.AddOcelot();
                })
                .UseIISIntegration()
                .Configure(app =>
                {
                    app.UseOcelot().Wait();
                })
                .Build()
                .Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Startup.cs:

public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddOcelot(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

         //   await app.UseOcelot();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

我正在使用 .NET Core 2.1 和 Ocelot NuGet 包版本 13.5.1 。

【问题讨论】:

  • 你通过邮递员打电话的具体路径是什么?
  • 另外,你为什么要使用请求聚合?
  • 我使用的邮递员路径是:localhost:61234/BAD(61234 是网关的端口)。我使用请求聚合是因为我需要来自两者的数据并且不想使用两个请求,主要是因为请求在不同的微服务中,而这些请求在同一个微服务中,仅用作测试。
  • 你解决了吗?

标签: c# asp.net-core gateway ocelot


【解决方案1】:

如果您使用请求聚合,则需要向 Ocelot 注册您的 IDefinedAggregator 实现。

喜欢:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot(Configuration).AddSingletonDefinedAggregator<FakeDefinedAggregator>();
}

否则 Ocelot 将无法在容器中找到您的聚合器。

【讨论】:

  • 哪里说是必须的?
猜你喜欢
  • 1970-01-01
  • 2022-09-25
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2018-11-28
  • 2021-06-22
  • 2023-02-15
相关资源
最近更新 更多