feat(api, bll): переделал сервис получения статистики из RabbitMQ. StatRabbitMqController - статистика по очередям и соединениям
This commit is contained in:
@@ -215,6 +215,7 @@
|
|||||||
public static class StatRabbitMq
|
public static class StatRabbitMq
|
||||||
{
|
{
|
||||||
public const string GetQueueStats = BaseStat + "/rabbitmq-queues/";
|
public const string GetQueueStats = BaseStat + "/rabbitmq-queues/";
|
||||||
|
public const string GetConnectionStats = BaseStat + "/rabbitmq-connections/";
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
using PARR.DAL.Services.Interfaces;
|
|
||||||
|
|
||||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
|
||||||
{
|
|
||||||
public class StatRabbitMqQueueResponse
|
|
||||||
{
|
|
||||||
public required string Name { get; set; }
|
|
||||||
|
|
||||||
public int Consumers { get; set; }
|
|
||||||
|
|
||||||
public int MessagesTotal { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using PARR.API.Contracts.V1;
|
using PARR.API.Contracts.V1;
|
||||||
using PARR.API.Contracts.V1.Responses.Base;
|
using PARR.API.Contracts.V1.Responses.Base;
|
||||||
using PARR.API.Contracts.V1.Responses.Statistics;
|
|
||||||
using PARR.API.Controllers.V1.Base;
|
using PARR.API.Controllers.V1.Base;
|
||||||
using PARR.API.Settings;
|
using PARR.API.Settings;
|
||||||
using PARR.BLL.Services.Interfaces;
|
using PARR.BLL.Services.Interfaces;
|
||||||
@@ -19,12 +18,14 @@ namespace PARR.API.Controllers.V1.Statistics
|
|||||||
private readonly IMqService mqService;
|
private readonly IMqService mqService;
|
||||||
private readonly MqSettings mqSettings;
|
private readonly MqSettings mqSettings;
|
||||||
private readonly ILogger<StatRabbitMqController> logger;
|
private readonly ILogger<StatRabbitMqController> logger;
|
||||||
|
private readonly IMqAdminService mqAdminService;
|
||||||
|
|
||||||
public StatRabbitMqController(IMqService mqService, MqSettings mqSettings, ILogger<StatRabbitMqController> logger)
|
public StatRabbitMqController(IMqService mqService, MqSettings mqSettings, ILogger<StatRabbitMqController> logger, IMqAdminService mqAdminService)
|
||||||
{
|
{
|
||||||
this.mqService = mqService;
|
this.mqService = mqService;
|
||||||
this.mqSettings = mqSettings;
|
this.mqSettings = mqSettings;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.mqAdminService = mqAdminService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -32,25 +33,48 @@ namespace PARR.API.Controllers.V1.Statistics
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet(ApiRoutes.StatRabbitMq.GetQueueStats)]
|
[HttpGet(ApiRoutes.StatRabbitMq.GetQueueStats)]
|
||||||
public IActionResult GetQueueStats()
|
public async Task<IActionResult> GetQueueStats()
|
||||||
{
|
{
|
||||||
var response = new List<StatRabbitMqQueueResponse>();
|
var result = await mqAdminService.GetQueuesAsync(mqSettings.Statistics.StatMqAuth);
|
||||||
|
|
||||||
// список очередей
|
if (!result.IsSuccess)
|
||||||
foreach (var queue in mqSettings.Statistics.QueueList)
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики из RabbitMQ." } }));
|
||||||
{
|
|
||||||
var result = mqService.GetQueueCount(mqSettings.Statistics.StatMqAuth, queue);
|
|
||||||
|
|
||||||
if (!result.IsSuccess)
|
return Ok(new Response<object?>(result.Response, true));
|
||||||
{
|
|
||||||
logger.LogError(result.Exception, $"Ошибка при запросе статистики из RabbitMQ для очереди: ${queue}");
|
|
||||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"" } }));
|
|
||||||
}
|
|
||||||
|
|
||||||
response.Add(new StatRabbitMqQueueResponse { Name = queue, Consumers = result.ConsumerCount, MessagesTotal = result.Count });
|
//var response = new List<StatRabbitMqQueueResponse>();
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(new Response<List<StatRabbitMqQueueResponse>>(response.OrderBy(t => t.Name).ToList(), true));
|
//// список очередей
|
||||||
|
//foreach (var queue in mqSettings.Statistics.QueueList)
|
||||||
|
//{
|
||||||
|
// var result = mqService.GetQueueCount(mqSettings.Statistics.StatMqAuth, queue);
|
||||||
|
|
||||||
|
// if (!result.IsSuccess)
|
||||||
|
// {
|
||||||
|
// logger.LogError(result.Exception, $"Ошибка при запросе статистики из RabbitMQ для очереди: ${queue}");
|
||||||
|
// return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"" } }));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// response.Add(new StatRabbitMqQueueResponse { Name = queue, Consumers = result.ConsumerCount, MessagesTotal = result.Count });
|
||||||
|
//}
|
||||||
|
|
||||||
|
//return Ok(new Response<List<StatRabbitMqQueueResponse>>(response.OrderBy(t => t.Name).ToList(), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Статистика соединений в RabbitMQ
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet(ApiRoutes.StatRabbitMq.GetConnectionStats)]
|
||||||
|
public async Task<IActionResult> GetConnectionStats()
|
||||||
|
{
|
||||||
|
var result = await mqAdminService.GetConnectionsAsync(mqSettings.Statistics.StatMqAuth);
|
||||||
|
|
||||||
|
if (!result.IsSuccess)
|
||||||
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики из RabbitMQ." } }));
|
||||||
|
|
||||||
|
return Ok(new Response<object?>(result.Response, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ namespace PARR.API.Settings
|
|||||||
|
|
||||||
public class MqStatistics
|
public class MqStatistics
|
||||||
{
|
{
|
||||||
public List<string> QueueList { get; set; } = new List<string>();
|
|
||||||
public StatMqAuth StatMqAuth { get; set; } = new StatMqAuth();
|
public StatMqAuth StatMqAuth { get; set; } = new StatMqAuth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,8 +50,7 @@
|
|||||||
"HostName": "parr-rabbitmq",
|
"HostName": "parr-rabbitmq",
|
||||||
"User": "rabbit_stats",
|
"User": "rabbit_stats",
|
||||||
"Password": "KJHdgfjHFsdy^&$!ff331"
|
"Password": "KJHdgfjHFsdy^&$!ff331"
|
||||||
},
|
}
|
||||||
"QueueList": [ "parr-aihit-data", "parr-espp-schedulers", "parr-espp-templates", "parr-generate-templates", "parr-orders" ]
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"UserCacheSettings": {
|
"UserCacheSettings": {
|
||||||
|
|||||||
14
PARR.BLL/Domain/Mq/MqApiResult.cs
Normal file
14
PARR.BLL/Domain/Mq/MqApiResult.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
namespace PARR.BLL.Domain.Mq
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Ответ от API RabbitMQ
|
||||||
|
/// </summary>
|
||||||
|
public class MqApiResult
|
||||||
|
{
|
||||||
|
public bool IsSuccess { get; set; }
|
||||||
|
|
||||||
|
public object? Response { get; set; }
|
||||||
|
|
||||||
|
public Exception? Exception { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
namespace PARR.BLL.Domain.Mq
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Результат запроса кол-ва сообщений в очереди RabbitMq
|
|
||||||
/// </summary>
|
|
||||||
public class MqQueueCountResult
|
|
||||||
{
|
|
||||||
public bool IsSuccess { get;set; }
|
|
||||||
|
|
||||||
public int Count { get; set; }
|
|
||||||
|
|
||||||
public int ConsumerCount { get; set; }
|
|
||||||
|
|
||||||
public Exception? Exception { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="6.5.0" />
|
<PackageReference Include="RabbitMQ.Client" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace PARR.BLL
|
|||||||
|
|
||||||
services.AddTransient<IFileService, FileService>();
|
services.AddTransient<IFileService, FileService>();
|
||||||
services.AddTransient<IMqService, MqService>();
|
services.AddTransient<IMqService, MqService>();
|
||||||
|
services.AddHttpClient<IMqAdminService, MqAdminService>();
|
||||||
services.AddTransient<ITransformService, TransformService>();
|
services.AddTransient<ITransformService, TransformService>();
|
||||||
services.AddTransient<IIntervalService, IntervalService>();
|
services.AddTransient<IIntervalService, IntervalService>();
|
||||||
}
|
}
|
||||||
|
|||||||
78
PARR.BLL/Services/Implementations/MqAdminService.cs
Normal file
78
PARR.BLL/Services/Implementations/MqAdminService.cs
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using PARR.BLL.Contracts.Interfaces;
|
||||||
|
using PARR.BLL.Domain.Mq;
|
||||||
|
using PARR.BLL.Services.Interfaces;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PARR.BLL.Services.Implementations
|
||||||
|
{
|
||||||
|
internal class MqAdminService : IMqAdminService
|
||||||
|
{
|
||||||
|
private readonly HttpClient httpClient;
|
||||||
|
private readonly ILogger<MqAdminService> logger;
|
||||||
|
|
||||||
|
public MqAdminService(HttpClient httpClient, ILogger<MqAdminService> logger)
|
||||||
|
{
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<MqApiResult> GetConnectionsAsync(IMqSettings mqSettings)
|
||||||
|
{
|
||||||
|
var url = $"http://{mqSettings.HostName}:15672/api/connections";
|
||||||
|
|
||||||
|
return await SendRequestAsync(mqSettings, url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<MqApiResult> GetQueuesAsync(IMqSettings mqSettings)
|
||||||
|
{
|
||||||
|
var url = $"http://{mqSettings.HostName}:15672/api/queues";
|
||||||
|
|
||||||
|
return await SendRequestAsync(mqSettings, url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task<MqApiResult> SendRequestAsync(IMqSettings mqSettings, string url)
|
||||||
|
{
|
||||||
|
SetAuthorizationHeaders(mqSettings);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await httpClient.GetAsync(url);
|
||||||
|
|
||||||
|
if (result.StatusCode == HttpStatusCode.OK)
|
||||||
|
{
|
||||||
|
var content = await result.Content.ReadAsStringAsync();
|
||||||
|
var json = JsonSerializer.Deserialize<object>(content);
|
||||||
|
|
||||||
|
return new MqApiResult { IsSuccess = true, Response = json };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError($"Ошибка при запросе к RabbitMq, url: {url}, StatusCode: {result.StatusCode}");
|
||||||
|
|
||||||
|
return new MqApiResult { IsSuccess = false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, $"Ошибка при запросе к RabbitMQ, url: {url}");
|
||||||
|
return new MqApiResult { IsSuccess = false, Exception = ex };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void SetAuthorizationHeaders(IMqSettings mqSettings)
|
||||||
|
{
|
||||||
|
httpClient.DefaultRequestHeaders.Clear();
|
||||||
|
httpClient.DefaultRequestHeaders.Authorization =
|
||||||
|
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{mqSettings.User}:{mqSettings.Password}")));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -155,34 +155,6 @@ namespace PARR.BLL.Services.Implementations
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName)
|
|
||||||
{
|
|
||||||
var factory = new ConnectionFactory
|
|
||||||
{
|
|
||||||
HostName = mqSettings.HostName,
|
|
||||||
UserName = mqSettings.User,
|
|
||||||
Password = mqSettings.Password,
|
|
||||||
};
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (var connection = factory.CreateConnection())
|
|
||||||
using (var channel = connection.CreateModel())
|
|
||||||
{
|
|
||||||
var messageCount = channel.MessageCount(queueName);
|
|
||||||
var consumerCount = channel.ConsumerCount(queueName);
|
|
||||||
|
|
||||||
return new MqQueueCountResult { Count = (int)messageCount, ConsumerCount = (int)consumerCount, IsSuccess = true };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError(ex, $"Ошибка при запросе кол-ва сообщений в очереди {queueName}");
|
|
||||||
|
|
||||||
return new MqQueueCountResult { Count = 0, IsSuccess = false, Exception = ex };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
channel?.Close();
|
channel?.Close();
|
||||||
|
|||||||
22
PARR.BLL/Services/Interfaces/IMqAdminService.cs
Normal file
22
PARR.BLL/Services/Interfaces/IMqAdminService.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using PARR.BLL.Contracts.Interfaces;
|
||||||
|
using PARR.BLL.Domain.Mq;
|
||||||
|
|
||||||
|
namespace PARR.BLL.Services.Interfaces
|
||||||
|
{
|
||||||
|
public interface IMqAdminService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получить статистику соединений из RabbitMQ
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mqSettings"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<MqApiResult> GetConnectionsAsync(IMqSettings mqSettings);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить статистику по очередям из RabbitMQ
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mqSettings"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<MqApiResult> GetQueuesAsync(IMqSettings mqSettings);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ namespace PARR.BLL.Services.Interfaces
|
|||||||
|
|
||||||
public interface IMqService : IDisposable
|
public interface IMqService : IDisposable
|
||||||
{
|
{
|
||||||
MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName);
|
|
||||||
bool InitConsumer(IMqSettings mqSettings, MqMessageHandlerDelegate messageHandler);
|
bool InitConsumer(IMqSettings mqSettings, MqMessageHandlerDelegate messageHandler);
|
||||||
MqSendResult Send(IMqSettings mqSettings, string[] msgList);
|
MqSendResult Send(IMqSettings mqSettings, string[] msgList);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user