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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nera.Lib.ServiceDiscovery" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nera.Lib.ServiceDiscovery" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Nera.Lib.ServiceDiscovery" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nera.Lib.ServiceDiscovery --version 1.0.0
                    
#r "nuget: Nera.Lib.ServiceDiscovery, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nera.Lib.ServiceDiscovery@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nera.Lib.ServiceDiscovery&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Nera.Lib.ServiceDiscovery&version=1.0.0
                    
Install as a Cake Tool

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

  1. Configure services in your 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();
  1. Configure Consul settings in appsettings.json:
{
  "Consul": {
    "ConsulUrl": "http://localhost:8500",
    "DataCenter": "dc1",
    "Token": "your-consul-token"
  }
}

Kubernetes Service Discovery

  1. Configure services:
using Nera.Lib.ServiceDiscovery;

var builder = WebApplication.CreateBuilder(args);

// Add Kubernetes service discovery
builder.Services.AddKubernetesServiceDiscovery(builder.Configuration);

var app = builder.Build();
  1. 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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