Mini workshop using https://github.com/fabiolb/fabio, https://github.com/hashicorp/consul, https://github.com/Drawaes/CondenserDotNet and https://github.com/dotnet/core
The Workshop aims to implement a simple API which registers itself with Consul. The API uses Fabio supported tags.
Run solution and verify that http://localhost:5000 returns "hello, world"
Add this to the Main method:
var port = ServiceManagerConfig.GetNextAvailablePort();
var serviceName = "Service.Api";
var serviceId = $"{serviceName}_127.0.0.1:{port}";
Replace kestrel config
options.Listen(IPAddress.Any, 5000, listenOptions =>
with
options.Listen(IPAddress.Any, port, listenOptions =>
Add this to the kestrel config:
.ConfigureServices(
services =>
{
services.Configure<ServiceManagerConfig>(
options =>
{
options.ServicePort = port;
options.ServiceName = serviceName;
options.ServiceId = serviceId;
options.ServiceAddress = "127.0.0.1";
});
services.AddSingleton<IConfigurationRegistry>(
CondenserConfigBuilder.FromConsul().WithAgentAddress("127.0.0.1").WithAgentPort(8500).Build());
})
Modify Startup Configure method signature with this
, IServiceManager manager, IServiceRegistry serviceRegistry
Add this to the begining of the startup configure method:
manager.AddApiUrl("/api/ strip=/api/");
manager.WithDeregisterIfCriticalAfter(TimeSpan.FromSeconds(30));
manager.AddHttpHealthCheck("health", 10).RegisterServiceAsync();
Add this to the start ConfigureServices method:
services.AddConsulServices();
Modify the app.run response with this:
var response = $"hello, world from {manager.ServiceName} @ {manager.ServiceAddress}:{manager.ServicePort}";
- Run Consul (with commandline arguments "agent -dev -ui").
- go to http://localhost:8500 and verify consul is running (keep open)
- Run Fabio (no commandline arugments)
- go to http://localhost:9998 and verify fabio is running (keep open)
- verify fabio is registered with consul
- Run a few instances of ServiceDiscovery.exe
- go to Consul and verify they are registered
- go to Fabio and verify they are registered
- go to http://localhost:9999/api/ and verify that you can hit all instance [just reload it and watch the port change :-)]
- close all ServiceDiscovery.exe and let Consul and Fabio run
- Go to part 2.
Replace ServiceDiscovery app.run response with this:
var serviceData = await serviceRegistry.GetServiceInstanceAsync("Service.Data");
var response = $"hello, world from {manager.ServiceName} @ {manager.ServiceAddress}:{manager.ServicePort} using Service.Data: {serviceData.ID}";
- Reload DataService
- Run a few instances of DataService
- Verify in Consul that they are registered
- Run a few instances of ServiceDiscovery.exe
- go to http://localhost:9999/api/ and see the response and how the ports rotate.