API 使用 OCELOT 的网关 -- 错误 - 无法启动 Ocelot

API Gateway using OCELOT -- ERROR - Unable to start Ocelot

我是 API 网关的新手,请按照以下 link 开始。

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
}
}

错误:

Unable to start Ocelot, errors are: When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!,When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!,When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!

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>

您似乎已经更新到最新的 Ocelot 软件包,因此您的 configuration.json 文件不正确。请参考 - http://ocelot.readthedocs.io/en/latest/features/routing.html.

您需要将 DownStreamHost/Port 设置为:

"DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 9001
      }
    ],

我用过 Ocelot 8.0.1,那个版本有一个错误。升级到8.0.2,现在可以正常使用了。