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

KAn*_*and 1 api host gateway ocelot

我是API网关的新手,请点击以下链接开始。

https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core/

当我尝试运行该应用程序时,它在下面的代码行中在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
}
}
Run Code Online (Sandbox Code Playgroud)

错误:

无法启动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>
Run Code Online (Sandbox Code Playgroud)

Ric*_*h J 7

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

您需要将DownStreamHost / Port设置为:

"DownstreamHostAndPorts": [
      {
        "Host": "localhost",
        "Port": 9001
      }
    ],
Run Code Online (Sandbox Code Playgroud)