[Л/Р 4] Мутрисков Данила #192

Merged
v.moiseev merged 3 commits from Hlop4ik4/distributed-computing:mutriskov-ds-lab-4 into main 2024-01-09 20:21:39 +04:00
6 changed files with 188 additions and 34 deletions
Showing only changes of commit 5f1137f521 - Show all commits

View File

@ -6,31 +6,29 @@ var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout);
Random rand = new Random();
string queueName = $"queue1";
channel.QueueDeclare("queue1");
channel.QueueBind(queue: "queue1",
exchange: "reports",
routingKey: string.Empty);
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "reports",
routingKey: string.Empty);
Console.WriteLine(" [*] Ожидание сообщения");
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
consumer.Received += async (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Thread.Sleep(2500);
var reportNumber = message.Split('#')[1];
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "queue1",
autoAck: true,
consumer: consumer);
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Нажмите [enter] чтобы выйти.");
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();

View File

@ -6,27 +6,30 @@ var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare("queue2");
channel.QueueBind(queue: "queue2",
exchange: "reports",
routingKey: string.Empty);
Random rand = new Random();
string queueName = $"queue{rand.Next()}";
Console.WriteLine(" [*] Ожидание сообщения");
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "reports",
routingKey: string.Empty);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
consumer.Received += async (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var reportNumber = message.Split('#')[1];
Thread.Sleep(2500);
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "queue2",
autoAck: true,
consumer: consumer);
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Нажмите [enter] чтобы выйти.");
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();

View File

@ -5,17 +5,21 @@ var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare("reports", ExchangeType.Fanout);
var rand = new Random();
while (true)
channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout);
Random rand = new Random();
foreach (var item in Enumerable.Range(0, 1000))
{
var message = $"Необходимо создать отчет #{rand.Next()}";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "reports",
routingKey: string.Empty,
basicProperties: null,
body: body);
channel.BasicPublish("reports", string.Empty, null, body);
Console.WriteLine($" [x] Отправлено \"{message}\"");
Thread.Sleep(1000);
Console.WriteLine($" [x] Поступила работа {message}");
await Task.Delay(1000);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();

View File

@ -0,0 +1,149 @@
# Отчет по лабораторной работе №4
Выполнил студент гр. ИСЭбд-41 Мутрисков Д.С.
## Прохождение tutorial
Установил rabbitMQ server, erlang и зашел в брокер под гостем по http://localhost:15672/#/
Туториал 1:
![](images/tut1.png)
Туториал 2:
![](images/tut2.png)
Туториал 3:
![](images/tut3.png)
## Разработка демонстрационных приложений
Предметная область: Офис. Производитель - начальник, который посылает задачу на составление отчета, Потребители - работники офиса.
1. Publisher
```cs
using System.Text;
using RabbitMQ.Client;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout);
Random rand = new Random();
foreach (var item in Enumerable.Range(0, 1000))
{
var message = $"Необходимо создать отчет #{rand.Next()}";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "reports",
routingKey: string.Empty,
basicProperties: null,
body: body);
Console.WriteLine($" [x] Поступила работа {message}");
await Task.Delay(1000);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
```
2. Consumer 1.
```cs
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
Random rand = new Random();
string queueName = $"queue1";
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "reports",
routingKey: string.Empty);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var reportNumber = message.Split('#')[1];
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
```
3. Consumer 2.
```cs
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
Random rand = new Random();
string queueName = $"queue{rand.Next()}";
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "reports",
routingKey: string.Empty);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var reportNumber = message.Split('#')[1];
Thread.Sleep(2500);
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
```
## Результаты выполнения лабораторной работы
Выполнение с одним потребителем с задержкой в выполении работы.
![](images/c1-1.png)
Очередь с работником без задержки в выполенении работы обрабатывается моментально и несет минимальную нагрузку.
Очередь работника с задержкой в выполении работы довольно быстро загружается.
Пример с двумя потребителями-курьерами
![](images/c1-2.png)
Очередь самовывоза не изменилась.
На очереди курьеров закономерно тратится в 2 раза больше времени

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 KiB