Как определить URL-адрес Ocelot API Gateway - PullRequest
0 голосов
/ 21 ноября 2018

У меня есть три службы ASP.NET Core WebAPI: клиент, подписка, отмена подписки с помощью проекта swashbuckle и docker. Все работает хорошо. Я добавил Ocelot API Gateway (основной проект ASP.NET) с установленным Ocelot.

Доступ к обслуживанию клиентов по собственному адресу https: /// api / Customer работает отлично.Но из шлюза я не знаю, какой URL мне следует использовать, например, эту службу поддержки. Я перепробовал много вариантов, таких как:

  • http: /// api /
  • http: /// api / a / customer
  • http: /// a / api / customer

, но все они возвращают 404. Возможно, проблема в том, что шлюз httpне https?

Program.cs

public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args)
        {
            var builder = WebHost.CreateDefaultBuilder(args);

            builder.ConfigureServices(s => s.AddSingleton(builder))
                                                          .ConfigureAppConfiguration(
                              ic => ic.AddJsonFile(Path.Combine("configuration",
                                                                "configuration.json")))
                                                                .UseStartup<Startup>();
            var host = builder.Build();
            return host;
        }

Startup.cs

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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddOcelot(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }

Конфигурации:

configuration.json:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "customer.api",
      "UpstreamPathTemplate": "/a/",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    },
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "customer.api",
      "UpstreamPathTemplate": "/a/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    },
    {
      "DownstreamPathTemplate": "/",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "subscribe.api",
      "UpstreamPathTemplate": "/b/",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    },
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "subscribe.api",
      "UpstreamPathTemplate": "/b/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    },
    {
      "DownstreamPathTemplate": "/",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "unsubscribe.api",
      "UpstreamPathTemplate": "/c/",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    },
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamPort": 80,
      "DownstreamHost": "unsubscribe.api",
      "UpstreamPathTemplate": "/c/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ]
    }
  ],
  "GlobalConfiguration": {}
}

docker-compose.yml:

services:
  customer.api:
    image: ${DOCKER_REGISTRY}customer.api
    build:
      context: .
      dockerfile: Customer.API\Dockerfile
  subscribe.api:
    image: ${DOCKER_REGISTRY}subscribe.api
    build:
      context: .
      dockerfile: NewsSubscibe.API\Dockerfile
  unsubscribe.api:
    image: ${DOCKER_REGISTRY}unsubscribe.api
    build:
      context: .
      dockerfile: NewsUnSubscribe.API\Dockerfile
  gateway:
    image: gateway
    build:
      context: ./OcelotAPIGateway
      dockerfile: Dockerfile
    depends_on:
      - customer.api
      - subscribe.api
      - unsubscribe.api

1 Ответ

0 голосов
/ 27 ноября 2018

Вам необходимо добавить UseOcelot().Wait(); в Configure метода запуска:

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

    app.UseHttpsRedirection();
    app.UseMvc();
    app.UseOcelot().Wait();
}
...