我正在创建一个带有微服务的项目,现在我希望每次尝试访问https://localhost:4482/gateway/product
时都能够用ocelot测试API网关,给我“无法到达这个页面”
ocelot.json:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44
}
],
"UpstreamPathTemplate": "/gateway/product",
"UpstreamHttpMethod": [ "GET"]
},
{
"DownstreamPathTemplate": "/api/Order",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44
}
],
"UpstreamPathTemplate": "/gateway/Order",
"UpstreamHttpMethod": [ "GET", "PUT", "POST" ]
}
]
}
Gateway.Program.cs
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json");
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseOcelot().Wait();
app.UseAuthorization();
app.MapControllers();
app.Run();
我错过了一些东西,但不知道它是什么,我检查了下游的路径和上游的路径。如果有任何帮助,我将不胜感激
发布于 2022-06-07 15:28:52
在.net核心6中,从ocelot 17.0.1开始,您必须使用“路由”而不是"ReRoute“
你可以直接把它放在appsettings.json里
我假设在您的解决方案中,您有一个类似于
在这里的样本
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Routes": [
{
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44
}
],
"UpstreamPathTemplate": "/gateway/product",
"UpstreamHttpMethod": [ "GET" ]
},
{
"DownstreamPathTemplate": "/api/Order",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44
}
],
"UpstreamPathTemplate": "/gateway/Order",
"UpstreamHttpMethod": [ "GET", "PUT", "POST" ]
}
]
}
program.cs
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseOcelot();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
如果有用的话请告诉我。
https://stackoverflow.com/questions/72511674
复制相似问题