404错误:试图在docker内部部署ocelot网关来调用微服务



我们开发了两个ASP.NET CORE(5.0(微服务,并将它们部署在docker中http://localhost:28621/stockhttp://localhost:62362/user

两者都可以从浏览器轻松访问。

我们已经将ocelot网关用于API网关。当我们在docker中部署它时,我们出现了以下错误

warn: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0] requestId: 0HM8658EF6KHC:00000002, previousRequestId: no previous request id, message: DownstreamRouteFinderMiddleware setting pipeline errors. IDownstreamRouteFinder returned Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /user, verb: GET. warn: Ocelot.Responder.Middleware.ResponderMiddleware[0] requestId: 0HM8658EF6KHC:00000002, previousRequestId: no previous request id, message: Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /user, verb: GET. errors found in ResponderMiddleware. Setting error response for request path:/user, request method: GET--------------------------------------------------------------------------------------------

ocelot.Development.json如下

{
"Routes": [
{
"DownstreamPathTemplate": "/user",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 62362
}
],
"UpstreamPathTemplate": "/user",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/stock",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 28621
}
],
"UpstreamPathTemplate": "/stock",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:5021"
}
}

启动.cs

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
}
public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
await app.UseOcelot();
}
}

程序.cs

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile($"ocelot.                                          
{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

请指导我们哪里做错了我们使用的是.net core 5.0

感谢

请将路由更改为ReRoutes。我把自己搞定了。也许你忘了在.cs程序中添加json文件

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) 
.ConfigureAppConfiguration((host, config) => 
{
config.AddJsonFile("Ocelot.json", false, true); 
})
.ConfigureWebHostDefaults(webBuilder => 
{ 
webBuilder .UseStartup<Startup>(); 
}); 

您的Ocelot容器无法使用"DownstreamHostAndPorts";settings:{'Host':'localhost'}。容器的localhost与主机的localhost不同!

要解决这个问题,请朝这个方向看:

  • 创建Docker网络
  • 使用--network alias标志在同一网络下运行所有三个容器
  • 在Ocelot-json文件中,使用网络别名和默认端口(由于容器可以直接相互通信,因此无需映射外部端口!(

相关内容

  • 没有找到相关文章

最新更新