Как определить URL Ocelot API Gateway
У меня есть три основных ASP.NET WebAPI-сервиса: Customer, Subscribe, Unscubscribe with swashbuckle и docker compose project. Все работает хорошо. Я добавил Ocelot API Gateway (основной проект ASP.NET) с установленным Ocelot.
Получите доступ к обслуживанию клиентов по собственному адресу https: /// api / Клиент работает отлично. Но из шлюза я не знаю, какой URL мне следует использовать, например, в этой службе поддержки клиентов, я пробовал много вариантов, таких как:
- Http: /// апи /
- Http: /// апи / а / клиент
- 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
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();
}