DLeader.Consul
1.0.6
There is a newer version of this package available.
See the version list below for details.
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
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
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.