-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_ConsulServiceDisco.cs
More file actions
44 lines (41 loc) · 1.68 KB
/
09_ConsulServiceDisco.cs
File metadata and controls
44 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace ProxyKit.Recipes
{
public class ConsulServiceDiscovery : Recipe<ConsulServiceDiscovery.Startup>
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddProxy();
services.AddSingleton<ConsulClient>();
}
public void Configure(IApplicationBuilder app)
{
app.RunProxy("api/foo", async context =>
{
var consulClient = context.RequestServices.GetRequiredService<ConsulClient>();
var service = await consulClient.Catalog.Service("foo-service");
var upstreamHost = new Uri($"https://{service.Response[0].Address}:{service.Response[0].ServicePort}");
return await context
.ForwardTo(upstreamHost)
.AddXForwardedHeaders()
.Send();
});
app.RunProxy("api/bar", async context =>
{
var consulClient = context.RequestServices.GetRequiredService<ConsulClient>();
var service = await consulClient.Catalog.Service("bar-service");
var upstreamHost = new Uri($"https://{service.Response[0].Address}:{service.Response[0].ServicePort}");
return await context
.ForwardTo(upstreamHost)
.AddXForwardedHeaders()
.Send();
});
}
}
}
}