在 .NET 6 docker 容器上使用 Ocelot 的 API 网关 - 无法使其工作
我对微服务架构相当陌生,并且手动启动了自己的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在本例中,将 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.