我正在尝试在我的应用程序中实现 ocelot 网关,但它总是在我在 ocelot.json 中配置的所有路径上返回 404。
每当我使用简单调用或聚合调用对 Postman 进行 GET 时,它总是返回 404 并且程序继续运行,它不会崩溃。
豹猫.json:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/Buildings",
"UpstreamPathTemplate": "/allBuildings",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"Key": "Buildings"
},
{
"DownstreamPathTemplate": "/api/Device",
"UpstreamPathTemplate": "/allDevices",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"Key": "Devices"
}
],
"Aggregates": [
{
"ReRouteKeys": [
"Buildings",
"Devices"
],
"UpstreamPathTemplate": "/BAD",
"Aggregator": "FakeDefinedAggregator"
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:44351/"
}
}
程序.cs:
public class Program
{
public static void Main(string[] args)
{
new WebHostBuilder()
.UseKestrel()
.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();
})
.UseIISIntegration()
.Configure(app =>
{
app.UseOcelot().Wait();
})
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
启动.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// await app.UseOcelot();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
我正在使用 .NET Core 2.1 和 Ocelot NuGet 包版本 13.5.1 。