Ocelot 不会重定向到微服务

Ocelot doesn't rederict to microservice

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

Catalo.API launSettings.json:

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

Program.cs 来自 API 网关:

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

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!" 的答复。 这里有什么问题?

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

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

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