Ocelot 网关不想应用微服务路由

时间:2021-05-16 06:25:09

标签: asp.net-core microservices ocelot

将单体应用程序分成几个微服务,现在我对如何一起运行它们很感兴趣。 所以,我正在为此构建豹猫网关。还有一些问题。

我尝试将一个简单的微服务与主 API 链接起来。如果我转到网关路由并查询我的微服务 => 我会得到 404。但是,如果我转到该微服务的 uri 我会得到信息。

Ocelot.json / ocelot.Development.json

{
  "Routes": [
    {
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7001
        }
      ],
      "DownstreamPathTemplate": "/order",
      "DownstreamScheme": "https",
      "UpstreamPathTemplate": "/order",
      "UpstreamHttpMethod": [ "GET" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5001"
  }
}

程序:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog((hostingContext, loggerConfiguration) =>
                {
                    loggerConfiguration.ReadFrom
                        .Configuration(hostingContext.Configuration);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

                    webBuilder.ConfigureAppConfiguration(config =>
                    {
                        config
                            .AddJsonFile("ocelot.json")
                            .AddJsonFile($"ocelot.{environment}.json");
                    });

                    webBuilder.UseStartup<Startup>();
                });

启动:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseAuthorization();
            app.UseAuthentication();
            app.UseSerilogRequestLogging();
            app.UseCors(_AllowSpecificOrigin);

            app.UseSwagger(options =>
            {
                options.SerializeAsV2 = true;
            });

            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "ShoppingCart API");
                options.RoutePrefix = string.Empty;
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapControllerRoute(
                    "default", 
                    "{controller=Products}/{action=GetAll}/{id?}");
            });

            app.UseOcelot();
        }

0 个答案:

没有答案
相关问题