Nera.Lib.ServiceDiscovery
1.0.0
dotnet add package Nera.Lib.ServiceDiscovery --version 1.0.0
NuGet\Install-Package Nera.Lib.ServiceDiscovery -Version 1.0.0
<PackageReference Include="Nera.Lib.ServiceDiscovery" Version="1.0.0" />
<PackageVersion Include="Nera.Lib.ServiceDiscovery" Version="1.0.0" />
<PackageReference Include="Nera.Lib.ServiceDiscovery" />
paket add Nera.Lib.ServiceDiscovery --version 1.0.0
#r "nuget: Nera.Lib.ServiceDiscovery, 1.0.0"
#:package Nera.Lib.ServiceDiscovery@1.0.0
#addin nuget:?package=Nera.Lib.ServiceDiscovery&version=1.0.0
#tool nuget:?package=Nera.Lib.ServiceDiscovery&version=1.0.0
Nera.Lib.ServiceDiscovery
A flexible service discovery library for Nextera Systems microservices, providing abstracted service discovery capabilities with support for multiple backends including Consul and Kubernetes.
Features
- Multi-backend Support: Supports both Consul and Kubernetes service discovery
- Load Balancing: Built-in load balancing strategies for service selection
- Async/Await: Full async support for all operations
- Dependency Injection: Seamless integration with Microsoft.Extensions.DependencyInjection
- Strongly Typed Configuration: Type-safe configuration options
- Comprehensive Logging: Integrated logging support
Installation
dotnet add package Nera.Lib.ServiceDiscovery
Quick Start
Consul Service Discovery
- Configure services in your
Program.cs
orStartup.cs
:
using Nera.Lib.ServiceDiscovery;
var builder = WebApplication.CreateBuilder(args);
// Add Consul service discovery
builder.Services.AddConsulServiceDiscovery(builder.Configuration);
var app = builder.Build();
- Configure Consul settings in
appsettings.json
:
{
"Consul": {
"ConsulUrl": "http://localhost:8500",
"DataCenter": "dc1",
"Token": "your-consul-token"
}
}
Kubernetes Service Discovery
- Configure services:
using Nera.Lib.ServiceDiscovery;
var builder = WebApplication.CreateBuilder(args);
// Add Kubernetes service discovery
builder.Services.AddKubernetesServiceDiscovery(builder.Configuration);
var app = builder.Build();
- Configure Kubernetes settings in
appsettings.json
:
{
"Kubernetes": {
"Namespace": "default",
"KubeConfigPath": "/path/to/kubeconfig"
}
}
Usage
Service Registration
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" }
});
}
}
Service Discovery
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");
}
}
Service Deregistration
public async Task DeregisterAsync()
{
await _serviceDiscovery.DeregisterServiceAsync("my-service-001");
}
Load Balancing Strategies
The library supports multiple load balancing strategies:
- Random: Selects a random service instance
- RoundRobin: Cycles through instances in order
- LeastConnections: Selects the instance with the fewest active connections (if supported by backend)
var instance = await _serviceDiscovery.DiscoverServiceInstanceAsync(
"my-service",
LoadBalancingStrategy.Random);
Configuration Options
Consul Options
Property | Description | Default |
---|---|---|
ConsulUrl |
Consul server URL | http://localhost:8500 |
DataCenter |
Consul datacenter | dc1 |
Token |
Consul ACL token | null |
Kubernetes Options
Property | Description | Default |
---|---|---|
Namespace |
Kubernetes namespace | default |
KubeConfigPath |
Path to kubeconfig file | null (uses in-cluster config) |
Service Instance Model
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; }
}
Error Handling
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
}
Health Checks
Service instances are automatically monitored for health when supported by the backend:
- Consul: Uses Consul's built-in health checking
- Kubernetes: Uses pod readiness and liveness probes
Dependencies
- .NET 9.0+
- Consul (for Consul provider)
- KubernetesClient (for Kubernetes provider)
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Hosting
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support and questions, please contact the Nextera Systems development team or create an issue in the repository.
© 2025 Nextera Systems. All rights reserved.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Consul (>= 1.7.14.9)
- KubernetesClient (>= 17.0.14)
- Nera.Lib.Core (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 145 | 9/9/2025 |