.Net 6 Ocelot 18.0.0 导航到网关 URL 时返回错误 404

Joe*_*nte 2 .net c# api ocelot

我有两个 API。其中之一是网关(Ocelot + .Net 6),另一个是普通的 .Net 6 API,我将其称为 Backoffice。在它们两个上,我都有一个带有端点“api/health”的控制器,用于指示 API 是否正在运行并描述环境。当我调用每个 API 的端点时,两个 API 都在工作。

端点是:

  • http://localhost:5039/api/health
  • http://localhost:5105/api/health

但是,当我调用指向 Backoffice API 的网关端点时,它返回 404。

重定向到 Backoffice API 的 Gatewayt 是:

  • http://localhost:5039/backoffice/api/health

但它返回 404。我不太明白为什么。我已配置 ocelot.json 并将其添加到 Program.cs 文件中。

更糟糕的是,我似乎找不到任何有关 .Net 6 实现的文档。.Net 6 现在只有 Program.cs 而没有 Startup.cs,所以它有点令人困惑,并且似乎无法在网上找到示例。

ocelot.Development.json

{
  "Routes": [
    //Backoffice API
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5105
        }
      ],
      "UpstreamPathTemplate": "/backoffice/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ],
      "Key": "user-transactions"
    }
  ],
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration",
    "BaseUrl": "http://localhost:5039"
  }
}
Run Code Online (Sandbox Code Playgroud)

启动设置.json

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5039",
      "sslPort": 0
    }
  },
  "profiles": {
    "Dimatur.APIGateway": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5039",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5039",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

程序.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;


var builder = WebApplication.CreateBuilder(args);

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
// Add services to the container.

builder.Services.AddControllers();

IConfiguration configuration = builder.Configuration.AddJsonFile($"ocelot.{env}.json", true, true).Build();

builder.Services.AddOcelot(configuration);

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
 
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

app.UseOcelot().Wait();
Run Code Online (Sandbox Code Playgroud)

我尝试编辑两个 API 上的 launchSettings.json,但从未成功。另外,尝试使用版本 17.0.0 但它也不起作用(我正在使用 18.0.0)。

我已经添加了:

IConfiguration configuration = builder.Configuration.AddJsonFile($"ocelot.{env}.json", true, true).Build();

builder.Services.AddOcelot(configuration);

并在文件末尾: app.UseOcelot().Wait();

我期望的是,当我调用 http://localhost:5039/backoffice/api/health 时,它应该返回一个 JSON。http://localhost:5105/api/health 中返回的结果相同。但我似乎无法调试正在发生的事情,也不知道该怎么办。当我直接调用 API 时,一切正常。

小智 5

把 放在app.UseOcelot().Wait();上面app.UseAuthorization();并尝试。 在此输入图像描述