-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
109 changed files
with
2,448 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Consul; | ||
|
||
namespace Payeh.SharedKernel.Consul; | ||
|
||
public static class ConsulServiceExtensions | ||
{ | ||
// Extension method to add Consul registration background service | ||
public static IServiceCollection AddConsulServiceRegistration( | ||
this IServiceCollection services, | ||
string serviceId, | ||
string serviceName, | ||
string host , // میزبان (Host) پیشفرض | ||
int port ) // پورت پیشفرض | ||
{ | ||
|
||
// افزودن سرویس Consul Registration | ||
services.AddSingleton<IHostedService>(serviceProvider => | ||
new ConsulRegistrationBackgroundService( | ||
serviceProvider.GetRequiredService<IConsulClient>(), // سرویس ConsulClient | ||
serviceProvider.GetRequiredService<ILogger<ConsulRegistrationBackgroundService>>() , | ||
serviceId, | ||
serviceName, | ||
host, | ||
port | ||
)); | ||
|
||
return services; | ||
} | ||
|
||
// Extension method to configure Consul client | ||
public static IServiceCollection AddConsulClient( | ||
this IServiceCollection services, | ||
Action<ConsulClientConfiguration> consulClientConfiguration) | ||
{ | ||
services.AddSingleton<IConsulService, ConsulService>(); | ||
services.AddSingleton<IConsulClient>(provider => | ||
{ | ||
var config = new ConsulClientConfiguration(); | ||
consulClientConfiguration(config); | ||
return new ConsulClient(config); | ||
}); | ||
|
||
return services; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
New/Payeh.SharedKernel/Consul/ConsulRegistrationBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Consul; | ||
|
||
namespace Payeh.SharedKernel.Consul; | ||
|
||
public class ConsulRegistrationBackgroundService : BackgroundService | ||
{ | ||
private readonly IConsulClient _consulClient; | ||
private readonly ILogger<ConsulRegistrationBackgroundService> _logger; | ||
private readonly string _serviceId; | ||
private readonly string _serviceName; | ||
private readonly string _host; | ||
private readonly int _port; | ||
private readonly string _healthCheckUrl; | ||
|
||
public ConsulRegistrationBackgroundService( | ||
IConsulClient consulClient, | ||
ILogger<ConsulRegistrationBackgroundService> logger, | ||
string serviceId, | ||
string serviceName, | ||
string host, | ||
int port, | ||
string healthCheckUrl = "/health") // Default health check endpoint | ||
{ | ||
_consulClient = consulClient; | ||
_logger = logger; | ||
_serviceId = serviceId; | ||
_serviceName = serviceName; | ||
_host = host; | ||
_port = port; | ||
_healthCheckUrl = healthCheckUrl; // Assign health check URL | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
try | ||
{ | ||
// Register the service with Consul along with health check | ||
var registration = new AgentServiceRegistration | ||
{ | ||
ID = _serviceId, | ||
Name = _serviceName, | ||
Address = _host, | ||
Port = _port, | ||
}; | ||
|
||
_logger.LogInformation("Registering service {ServiceName} with ID {ServiceId} at {Host}:{Port}", _serviceName, _serviceId, _host, _port); | ||
await _consulClient.Agent.ServiceRegister(registration, stoppingToken); | ||
|
||
await Task.Delay(Timeout.Infinite, stoppingToken); // Keep the service registered until cancellation | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error occurred while registering service {ServiceName} with ID {ServiceId}.", _serviceName, _serviceId); | ||
} | ||
} | ||
|
||
public override async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Deregistering service {ServiceName} with ID {ServiceId}", _serviceName, _serviceId); | ||
await _consulClient.Agent.ServiceDeregister(_serviceId); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error occurred while deregistering service {ServiceName} with ID {ServiceId}.", _serviceName, _serviceId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Consul; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Payeh.SharedKernel.Consul | ||
{ | ||
public class ConsulService : IConsulService | ||
{ | ||
private readonly IConsulClient _consulClient; | ||
|
||
public ConsulService(IConsulClient consulClient) | ||
{ | ||
_consulClient = consulClient; | ||
} | ||
|
||
// دریافت تمام سرویسهای ثبتشده در Consul | ||
public async Task<IEnumerable<AgentService>> GetAllServicesAsync() | ||
{ | ||
var services = await _consulClient.Agent.Services(); | ||
return services.Response.Values; // تمام سرویسها را برمیگرداند | ||
} | ||
|
||
// دریافت جزئیات یک سرویس خاص با نام آن | ||
public async Task<AgentService> GetServiceDetailsAsync(string serviceName) | ||
{ | ||
var services = await _consulClient.Agent.Services(); | ||
|
||
var service = services.Response.Values.FirstOrDefault(s => s.Service.Equals(serviceName, StringComparison.OrdinalIgnoreCase)); | ||
return service; // اگر سرویس پیدا شد، آن را برمیگرداند | ||
} | ||
|
||
// دریافت وضعیت سلامت یک سرویس خاص | ||
public async Task<ServiceEntry[]> GetServiceHealthAsync(string serviceName) | ||
{ | ||
// گرفتن وضعیت سلامت سرویس خاص از Consul | ||
var healthCheck = await _consulClient.Health.Service(serviceName); | ||
|
||
// HealthServiceResponse شامل اطلاعات وضعیت سلامت میباشد | ||
return healthCheck.Response; // اگر وضعیت سلامت پیدا شد، آن را برمیگرداند | ||
} | ||
|
||
// دریافت وضعیت سلامت تمام سرویسها | ||
|
||
|
||
// دریافت وضعیت سلامت نود کنونی | ||
public async Task<List<ServiceEntry>> GetCurrentNodeHealthAsync() | ||
{ | ||
// دریافت اطلاعات مربوط به نودها از Consul | ||
var ser = await _consulClient.Agent.Services(); | ||
|
||
List<ServiceEntry> healthChecks = new List<ServiceEntry>();; | ||
foreach (var value in ser.Response.Values) | ||
{ | ||
var health = await _consulClient.Health.Service(value.Service); // وضعیت سلامت نود کنونی را میگیریم | ||
|
||
healthChecks.AddRange(health.Response); | ||
|
||
} | ||
|
||
|
||
return healthChecks; // بازگشت وضعیت سلامت سرویسها برای نود | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Consul; | ||
|
||
namespace Payeh.SharedKernel.Consul; | ||
|
||
public interface IConsulService | ||
{ | ||
Task<IEnumerable<AgentService>> GetAllServicesAsync(); // برای دریافت تمام سرویسها | ||
Task<AgentService> GetServiceDetailsAsync(string serviceName); // برای دریافت جزئیات یک سرویس خاص | ||
|
||
Task<ServiceEntry[]> GetServiceHealthAsync(string serviceName); | ||
// دریافت وضعیت سلامت تمام سرویسها | ||
Task<List<ServiceEntry>> GetCurrentNodeHealthAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.