Zenith.Extensions.Consul 1.0.0

dotnet add package Zenith.Extensions.Consul --version 1.0.0
                    
NuGet\Install-Package Zenith.Extensions.Consul -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="Zenith.Extensions.Consul" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zenith.Extensions.Consul" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Zenith.Extensions.Consul" />
                    
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 Zenith.Extensions.Consul --version 1.0.0
                    
#r "nuget: Zenith.Extensions.Consul, 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 Zenith.Extensions.Consul@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=Zenith.Extensions.Consul&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Zenith.Extensions.Consul&version=1.0.0
                    
Install as a Cake Tool

.NET Consul Automatic Service Registration Component

Introduction

A high-performance microservice registration and health check component based on .NET 6+ and Consul. This component implements automatic service registration, periodic TTL heartbeat health detection, real-time health status reporting, and graceful service deregistration, providing stable and reliable service discovery capabilities for distributed microservice systems.

Core Features

  • Automatic Service Registration: Auto register microservice instances to Consul on application startup

  • Dynamic IP Adaptation: Priority acquisition of K8s Pod IP, compatible with container and physical machine deployment

  • Integrated Health Check: Link with .NET native health check system, report real-time service running status

  • TTL Heartbeat Mechanism: 5-second periodic heartbeat reporting, 15-second TTL health judgment

  • Automatic Offline Cleanup: Consul automatically clears abnormal offline nodes after 1 minute of critical state

  • Graceful Deregistration: Active logout from Consul during service shutdown to avoid invalid service nodes

  • Configurable Switch: Disable Consul registration automatically when the registry address is empty

Installation & Usage

1. Configuration

Add the ConsulConfig node in appsettings.json:

{
  "ConsulConfig": {
    "RegistryAddress": "http://127.0.0.1:8500",
    "ServiceName": "ServiceCenter",
    "ServiceIP": "",
    "ServicePort": 5008
  }
}

2. Register Service

Inject the Consul registry component in Program/Startup:

builder.Services.AddConsulRegistry(builder.Configuration);

Configuration Explanation

Field Description Rule
RegistryAddress Consul server registry address Empty value will disable the entire Consul registration function
ServiceName Global microservice name Multiple instances share the same name for service discovery & load balancing
ServiceIP Service listening IP Priority: K8s POD_IP env &gt; ServiceIP config &gt; 127.0.0.1 fallback
ServicePort Service listening port Default: 5008

Working Mechanism

1. Startup Registration

After the application starts, the background service automatically generates a unique service ID (service name + machine name), completes service information registration to Consul, and initializes the TTL health check rule.

2. Heartbeat & Health Detection

  • Trigger a full application health check every 5 seconds

  • Report PassTTL to Consul if all internal checks pass (service healthy)

  • Report FailTTL with abnormal reasons if component exceptions are detected (service critical)

  • Consul marks the instance as unhealthy if no heartbeat within 15 seconds

  • Consul automatically deletes the abnormal node after 1 minute of critical state

3. Graceful Shutdown

When the application or K8s Pod stops, the component actively deregisters the current service instance from Consul to ensure the accuracy of the service discovery list and eliminate invalid nodes.

Technical Advantages

  • Adopts .NET 6+ native BackgroundService + PeriodicTimer, avoiding heartbeat overlap and supporting native lifecycle cancellation

  • Dock with .NET official health check system, unified and comprehensive service status detection

  • Perfectly compatible with K8s container orchestration, automatically adapt to Pod dynamic IP

  • Lightweight and non-intrusive, enable/disable functions through simple configuration

  • Complete exception handling and log output, convenient for online troubleshooting

Compatibility

  • .NET Version: .NET 6 / .NET 7 / .NET 8+

  • Deployment Environment: Physical machine, Docker, Kubernetes

  • Consul Version: 1.9+

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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 114 7/11/2026

1. Refactored with BackgroundService + PeriodicTimer to fully adapt to the .NET 6+ asynchronous host lifecycle.
2. Optimized compatibility with K8s container environment, reading the POD_IP environment variable with higher priority.
3. Integrated native HealthCheckService with Consul TTL heartbeat reporting mechanism for linked health status reporting.