在 .NET 6 docker 容器上使用 Ocelot 的 API 网关 - 无法使其工作

发布于 2025-01-15 09:34:44 字数 1668 浏览 2 评论 0原文

我对微服务架构相当陌生,并且手动启动了自己的 api 网关,该网关可以工作,但通过研究,我意识到使用 Ocelot 作为替代品的价值在于它带来的功能。今天,我创建了一个新的空 .NET 6 api-gateway 项目,并使用 Ocelot 文档引入了设置它所需的 nuget 包。最终的游戏是使用 Eureka,但由于我似乎无法使其工作,所以我先退回到使用直接调用基于 docker 的微服务。

看着文档,我想我已经严格遵循了它,但是当我启动容器时它什么也没做,我想知道是否有人可以指出我正确的方向。

我的program.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();

我的ocelot.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"
  }
}
'''

I'm fairly new to the microservices architecture and hand cranked my own api-gateway which works, but from research realised the value in using Ocelot as a replacment due to the features it brings. Today I created a new empty .NET 6 api-gateway project and using the Ocelot documentation brought in nuget packages needed to set it up. The end game is to use Eureka, but as I don't seem to be able to make it work I have stepped back to using a direct call the docker based microservice first.

Looking at the documentation I think I've follow it to the letter, but when I launch the container it does nothing, I wonder if someone could point me in the right direction.

My program.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(
quot;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();

my ocelot.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"
  }
}
'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2025-01-22 09:34:44

在本例中,将 localhost 替换为 docker compose yaml 使用的容器名称 web.api.gateway 可以解决此问题。

Replacing localhost for the container name used by the docker compose yaml in this case web.api.gateway solves this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文