将单片应用程序分离为几个微服务,现在我对如何将它们一起运行很感兴趣。所以,我正在为此构建ocelot网关。还有一些问题。
我尝试将一个简单的微服务与主api连接起来。如果我转到网关路由并查询我的微服务=>我会得到404。然而,如果我去微服务的uri,我会得到信息。
Ocelot.json/oslot。开发.json
{
"Routes": [
{
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7001
}
],
"DownstreamPathTemplate": "/order",
"DownstreamScheme": "https",
"UpstreamPathTemplate": "/order",
"UpstreamHttpMethod": [ "GET" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:5001"
}
}
程序:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom
.Configuration(hostingContext.Configuration);
})
.ConfigureWebHostDefaults(webBuilder =>
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
webBuilder.ConfigureAppConfiguration(config =>
{
config
.AddJsonFile("ocelot.json")
.AddJsonFile($"ocelot.{environment}.json");
});
webBuilder.UseStartup<Startup>();
});
启动:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseSerilogRequestLogging();
app.UseCors(_AllowSpecificOrigin);
app.UseSwagger(options =>
{
options.SerializeAsV2 = true;
});
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "ShoppingCart API");
options.RoutePrefix = string.Empty;
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
"default",
"{controller=Products}/{action=GetAll}/{id?}");
});
app.UseOcelot();
}
您需要使用:
Use app.UseOcelot().Wait();