如何定义Ocelot API网关的URL

时间:2018-11-21 15:11:17

标签: docker asp.net-core .net-core ocelot

我有三个ASP.NET Core WebAPI服务客户,订阅,使用swashbuckle和Docker撰写项目的Unscubscribe 一切都很好 我已经添加了安装了Ocelot的Ocelot API网关(ASP.NET核心项目)。

通过自己的地址访问客户服务 https:/// api / Customer 效果很好。但是从网关我不知道我应该使用哪个网址,例如此客户服务 我尝试了许多变体,例如:

  • 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 :(得分:1)

您需要在“启动的配置”方法中添加UseOcelot().Wait();

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

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