簡體   English   中英

如何定義Ocelot API網關的URL

[英]How to define URL of Ocelot API Gateway

我有三個ASP.NET Core WebAPI服務客戶,訂閱,使用swashbuckle和docker撰寫項目的Unscubscribe一切運行良好我添加了安裝了Ocelot的Ocelot API Gateway(ASP.NET核心項目)。

通過自己的地址訪問客戶服務https:/// api / Customer效果很好。 但是從網關我不知道我應該使用哪個網址,例如該客戶服務,我嘗試了許多變體,例如:

  • HTTP:/// API /
  • HTTP:/// API / A /客戶
  • HTTP:///一/ API /客戶

但它們都返回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": {}
}

泊塢窗,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

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM