0

我正在尝试实现我的微服务应用程序。我在 localhost:5001 上的目录 API 微服务 - 基础 CRUD。我想使用 Ocelot 实现 Api Gateway。

Catalo.API launSettings.json:

"reservation_system.Catalo.Api": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "swagger/index.html",
  "applicationUrl": "http://localhost:5001",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

来自 API 网关的 Program.cs:

  public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((host, config) =>
                {
                    config
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", true, true)
                    .AddEnvironmentVariables();
                    config.AddJsonFile("configuration.json");
                })
            .UseStartup<Startup>();
    }

启动.cs

public class Startup
    {
        public IConfiguration Configuration { get; set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddOcelot();
        }

        // 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();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
            app.UseMvc();
            await app.UseOcelot();
        }
    }

我正在尝试通过http://localhost:50121/catalog访问我的目录 API 我得到“Hello World!” 回答。这里有什么问题?

4

1 回答 1

1

Run()Ocelot 中间件未执行,因为您通过调用委托和写入响应流来短路请求管道。

中间件组件在Configure方法中注册的顺序很重要。这些组件的调用顺序与它们添加的顺序相同。

因此,如果您向上移动await app.UseOcelot();,进入Configure()方法,就在 之前app.Run(),Ocelot 中间件将被执行。

于 2019-08-14T15:40:07.960 回答