簡體   English   中英

如何配置 ocelot api-gateway.net 6.0,以便將請求重定向到同一 Web 應用程序(同一主機和端口)中的 controller?

[英]How to configure ocelot api-gateway net 6.0, in order to redirect request to a controller in the same web-application (same host and port)?

目標 framework.net 6.0 ocelot 18.0.0

程序.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

var ocelotConfig = builder.Configuration.AddJsonFile(Path.Combine(builder.Environment.ContentRootPath, "ocelot.json"), optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddControllers();

var app = builder.Build();

app.UseOcelot();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

豹貓.json

{
  "GlobalConfiguration": {
    "BaseAddress": "https://localhost:7249"
  },
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/WeatherForecast",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7249
        }
      ],
      "UpstreamPathTemplate": "/gtw/WeatherForecast",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ]
}

天氣預報控制器.cs

using Microsoft.AspNetCore.Mvc;

namespace DemoApiGateway.Controllers;

[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

當我嘗試調用 https://localhost:7249/gtw/WeatherForecast 或直接調用 https://localhost:7249/api/WeatherForecast 時,我收到響應 Http 404。

PS 在 .net 核心 (2.2) 的早期版本中它可以工作。 但它已被棄用,我想重建到 .net 的 LTS 版本。

它不起作用,因為 Ocelot 處理對/api/WeatherForecast的上游調用,啟動對/gtw/WeatherForecast的下游調用,它再次由 Ocelot 處理並且永遠不會到達 controller。
Minimal API 做了一些隱含的魔法 實際上,您的中間件管道如下所示:

app.UseRouting();       // added implicitly
app.UseOcelot();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseEndpoints(e =>   // added implicitly
{
    e.MapControllers();
});

中間件順序注冊是必不可少的:傳入的請求由每個中間件按照它們注冊的順序處理。 您的代碼中存在多個問題:

  • UseHttpsRedirection應在UseRouting (隱式添加)之前調用。 要禁用UseRouting的隱式添加,您應該在正確的位置顯式調用它。
  • 如果您在 Ocelot 配置中使用授權,則應在UseAuthorization之前調用UseOcelot
  • Ocelot 是一個終端中間件,這意味着在UseOcelot之后什么都不會執行。 這意味着Endpoints中間件將沒有機會處理請求並將其分派給WeatherForecastController 要在Endpoints之后注冊 Ocelot,您應該在正確的位置顯式調用UseEndpoints

固定代碼應該是:

app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(e =>
{
    e.MapControllers();
});
app.UseOcelot();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM