簡體   English   中英

API 網關在 .NET 6 docker 容器上使用 Ocelot - 無法使其工作

[英]API Gateway using Ocelot on .NET 6 docker container - unable to make it work

我對微服務架構相當陌生,並且手動啟動了我自己的 api-gateway,它可以工作,但從研究中意識到使用 Ocelot 作為替代品的價值,因為它帶來的功能。 今天我創建了一個新的空 .NET 6 api-gateway 項目,並使用 Ocelot 文檔引入了設置它所需的 nuget 包。 最終游戲是使用 Eureka,但由於我似乎無法讓它工作,所以我退后一步,首先使用直接調用基於 docker 的微服務。

查看文檔,我想我已經嚴格遵守了它,但是當我啟動容器時它什么也沒做,我想知道是否有人可以指出我正確的方向。

我的程序.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Host.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();
        //.AddEureka();
    });

var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
app.UseOcelot().Wait();

app.Run();

我的豹貓.json

{
  "Routes": [
    // drugService API
    {
      "DownstreamPathTemplate": "/api/TerminologyServices/SearchDrugs/{filter}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "drugservice",
          "Port": "80"
        }
      ],
      "UpstreamPathTemplate": "/TerminologyServices/SearchDrugs/{filter}",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
      "BaseUrl": "https://localhost:5003"
  }
}
'''

將 docker 使用的容器名稱替換為 localhost,在本例中為 yaml web.api.gateway 可解決此問題。

暫無
暫無

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

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