Domain service-discovery, business rules, aggregates, value objects, and domain services for Nera applications
$ dotnet add package Nera.Lib.ServiceDiscoveryA flexible service discovery library for Nextera Systems microservices, providing abstracted service discovery capabilities with support for multiple backends including Consul and Kubernetes.
dotnet add package Nera.Lib.ServiceDiscovery
Program.cs or Startup.cs:using Nera.Lib.ServiceDiscovery;
var builder = WebApplication.CreateBuilder(args);
// Add Consul service discovery
builder.Services.AddConsulServiceDiscovery(builder.Configuration);
var app = builder.Build();
appsettings.json:{
"Consul": {
"ConsulUrl": "http://localhost:8500",
"DataCenter": "dc1",
"Token": "your-consul-token"
}
}
using Nera.Lib.ServiceDiscovery;
var builder = WebApplication.CreateBuilder(args);
// Add Kubernetes service discovery
builder.Services.AddKubernetesServiceDiscovery(builder.Configuration);
var app = builder.Build();
appsettings.json:{
"Kubernetes": {
"Namespace": "default",
"KubeConfigPath": "/path/to/kubeconfig"
}
}
public class MyService
{
private readonly IServiceDiscoveryProvider _serviceDiscovery;
public MyService(IServiceDiscoveryProvider serviceDiscovery)
{
_serviceDiscovery = serviceDiscovery;
}
public async Task RegisterAsync()
{
await _serviceDiscovery.RegisterServiceAsync(
serviceId: "my-service-001",
serviceName: "my-service",
serviceAddress: "192.168.1.100",
servicePort: 8080,
tags: new[] { "api", "v1" },
metadata: new Dictionary<string, string>
{
{ "version", "1.0.0" },
{ "environment", "production" }
});
}
}
public class MyConsumer
{
private readonly IServiceDiscoveryProvider _serviceDiscovery;
public MyConsumer(IServiceDiscoveryProvider serviceDiscovery)
{
_serviceDiscovery = serviceDiscovery;
}
public async Task<string> CallServiceAsync()
{
// Discover all instances of a service
var instances = await _serviceDiscovery.DiscoverServiceAsync("my-service");
// Or get a single instance using load balancing
var instance = await _serviceDiscovery.DiscoverServiceInstanceAsync(
"my-service",
LoadBalancingStrategy.RoundRobin);
if (instance != null)
{
var client = new HttpClient();
var response = await client.GetAsync($"http://{instance.Address}:{instance.Port}/api/health");
return await response.Content.ReadAsStringAsync();
}
throw new ServiceUnavailableException("No healthy instances found");
}
}
public async Task DeregisterAsync()
{
await _serviceDiscovery.DeregisterServiceAsync("my-service-001");
}
The library supports multiple load balancing strategies:
var instance = await _serviceDiscovery.DiscoverServiceInstanceAsync(
"my-service",
LoadBalancingStrategy.Random);
| Property | Description | Default |
|---|---|---|
ConsulUrl | Consul server URL | http://localhost:8500 |
DataCenter | Consul datacenter | dc1 |
Token | Consul ACL token | null |
| Property | Description | Default |
|---|---|---|
Namespace | Kubernetes namespace | default |
KubeConfigPath | Path to kubeconfig file | null (uses in-cluster config) |
public class ServiceInstance
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Port { get; set; }
public IEnumerable<string> Tags { get; set; }
public IDictionary<string, string> Metadata { get; set; }
public bool IsHealthy { get; set; }
}
The library includes comprehensive error handling:
try
{
var instance = await _serviceDiscovery.DiscoverServiceInstanceAsync("my-service");
}
catch (ServiceDiscoveryException ex)
{
_logger.LogError(ex, "Failed to discover service: {ServiceName}", "my-service");
// Handle the error appropriately
}
Service instances are automatically monitored for health when supported by the backend:
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions, please contact the Nextera Systems development team or create an issue in the repository.
© 2025 Nextera Systems. All rights reserved.