DLeader.Consul
1.0.6
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
<PackageReference Include="DLeader.Consul" Version="1.0.6" />
<PackageVersion Include="DLeader.Consul" Version="1.0.6" />
<PackageReference Include="DLeader.Consul" />
paket add DLeader.Consul --version 1.0.6
#r "nuget: DLeader.Consul, 1.0.6"
#:package DLeader.Consul@1.0.6
#addin nuget:?package=DLeader.Consul&version=1.0.6
#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
- Start Consul:
docker run -d -p 8500:8500 consul:latest
- Run multiple instances:
dotnet run --project samples/ConsulLeaderExample
--
You can run
docker compose up --build
Testing
dotnet test
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- 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 | Versions 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. net9.0 was computed. 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. |
-
net8.0
- Consul (>= 1.7.14.3)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DLeader.Consul:
| Package | Downloads |
|---|---|
|
DLeader.Consul.Messaging
Best-effort message fan-out between service instances over the Consul KV store. Split out of DLeader.Consul in 2.0. Not a queue: delivery is best-effort, retention is minutes, and an instance that is down when a message is published never sees it. |
GitHub repositories
This package is not used by any popular GitHub repositories.