2

我有三个 ASP.NET Core WebAPI 服务 Customer、Subscribe、Unscubscribe 和 swashbuckle 和 docker compose 项目一切运行良好我添加了安装了 Ocelot 的 Ocelot API Gateway(ASP.NET 核心项目)。

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

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

但他们都返回404。 网关可能是http而不是https的问题?

程序.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;
        }

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

配置:

配置.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": {}
}

码头工人-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
4

1 回答 1

4

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

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

    app.UseHttpsRedirection();
    app.UseMvc();
    app.UseOcelot().Wait();
}
于 2018-11-26T21:39:03.770 回答