DLeader.Consul 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package DLeader.Consul --version 1.0.6                
NuGet\Install-Package DLeader.Consul -Version 1.0.6                
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="DLeader.Consul" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DLeader.Consul --version 1.0.6                
#r "nuget: DLeader.Consul, 1.0.6"                
#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.
// Install DLeader.Consul as a Cake Addin
#addin nuget:?package=DLeader.Consul&version=1.0.6

// Install DLeader.Consul as a Cake Tool
#tool nuget:?package=DLeader.Consul&version=1.0.6                

DLeader.Consul

A .NET library that provides distributed leader election capabilities using HashiCorp Consul. This library helps you implement leader election patterns in distributed systems with a clean and simple API.

Features

  • 🔄 Automatic leader election using Consul
  • 🔌 Easy service registration and discovery
  • 🏃 Automatic session management and renewal
  • 🎯 Event-driven leadership changes
  • ⚡ High performance and low overhead
  • 🛠️ Built with dependency injection in mind
  • 📝 Extensive logging and diagnostics

Installation

dotnet add package DLeader.Consul

Quick Start

// Register the service
services.AddConsulLeader(options =>
{
    options.ServiceName = "my-service";
    options.Address = "http://consul:8500";
});

// Use in your service
public class MyService : BackgroundService
{
    private readonly ILeaderElection _leaderElection;
    
    public MyService(ILeaderElection leaderElection)
    {
        _leaderElection = leaderElection;
        _leaderElection.OnLeadershipAcquired += HandleLeadershipAcquired;
        _leaderElection.OnLeadershipLost += HandleLeadershipLost;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await _leaderElection.StartLeaderElectionAsync(stoppingToken);
    }
}

Configuration Options

public class ConsulOptions
{
    public string ServiceName { get; set; } = "leadership-service";
    public string Address { get; set; } = "http://localhost:8500";
    public int SessionTTL { get; set; } = 15;
    public int RenewInterval { get; set; } = 5;
    public int LeaderCheckInterval { get; set; } = 1;
}

Advanced Usage

Custom Service Registration

services.AddConsulLeader(options =>
{
    options.ServiceName = "my-service";
    options.Address = "http://consul:8500";
    options.SessionTTL = 15;
    options.RenewInterval = 5;
}, serviceOptions =>
{
    serviceOptions.ServicePort = 5000;
    serviceOptions.HealthCheckEndpoint = "/health";
    serviceOptions.Tags = new[] { "production", "web" };
});

Prerequisites

  • .NET 8.0 or higher
  • Consul server (local or remote)

Dependencies

  • Consul (>= 1.6.10.9)
  • Microsoft.Extensions.DependencyInjection (>= 8.0.0)
  • Microsoft.Extensions.Logging (>= 8.0.0)

Running the Examples

  1. Start Consul:
docker run -d -p 8500:8500 consul:latest
  1. Run multiple instances:
dotnet run --project samples/ConsulLeaderExample

--

You can run

docker compose up --build

Testing

dotnet test

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details

Support

If you need help or have any questions:

  • Open an issue
  • Submit a pull request
  • Contact the maintainers

Maintainers

  • Franco Pachue

Acknowledgments

  • HashiCorp Consul team
  • .NET community

Tags

consul, leader-election, distributed-systems, dotnet, csharp, microservices, service-discovery

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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.9 90 11/5/2024
1.0.7 82 11/4/2024
1.0.6 80 11/4/2024