【发布时间】:2018-12-29 17:17:29
【问题描述】:
我是 API 网关的新手,请按照以下链接开始。
https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core/
当我尝试运行应用程序时,它会在 Startup.cs 文件中的以下代码行中引发错误。
Startup.cs
public Startup(IHostingEnvironment env){
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
builder.SetBasePath(env.ContentRootPath)
.AddJsonFile("configuration.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services){
Action<ConfigurationBuilderCachePart> settings = (x) =>{
x.WithMicrosoftLogging(log =>{
log.AddConsole(LogLevel.Debug);
}).WithDictionaryHandle();
};
//services.AddOcelot(Configuration, settings);
services.AddOcelot(Configuration);
}
public async void Configure(IApplicationBuilder app, IHostingEnvironment env){
await app.UseOcelot(); // Error in this line number
}
}
错误:
无法启动Ocelot,报错为:不使用服务发现时DownstreamHostAndPorts必须设置且不为空否则Ocelot找不到你的服务!,不使用服务发现时DownstreamHostAndPorts必须设置且不为空否则Ocelot找不到你的服务! ,不使用服务发现的时候DownstreamHostAndPorts一定要设置且不能为空否则Ocelot找不到你的服务!
configuration.json
{</br>
"ReRoutes": [</br>
{</br>
"DownstreamPathTemplate": "/api/customers",
"DownstreamScheme": "http",
"DownstreamHost": "localhost",
"DownstreamPort": 9001,
"UpstreamPathTemplate": "/customers",
"UpstreamHttpMethod": [ "Get" ]</br>
},</br>
{</br>
"DownstreamPathTemplate": "/api/customers/{id}",
"DownstreamScheme": "http",
"DownstreamHost": "localhost",
"DownstreamPort": 9001,
"UpstreamPathTemplate": "/customers/{id}",
"UpstreamHttpMethod": [ "Get" ]</br>
},</br>
{</br>
"DownstreamPathTemplate": "/api/products",
"DownstreamScheme": "http",
"DownstreamPort": 9002,
"DownstreamHost": "localhost",
"UpstreamPathTemplate": "/api/products",
"UpstreamHttpMethod": [ "Get" ]</br>
}</br>
],</br>
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}</br>
}</br></br>
【问题讨论】:
-
我修正了问题的格式。
</br>不是必需的。而且错误更容易理解为引用或代码(我所做的)。