fix
This commit is contained in:
parent
6254d041b3
commit
5f1137f521
@ -6,31 +6,29 @@ var factory = new ConnectionFactory { HostName = "localhost" };
|
|||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout);
|
Random rand = new Random();
|
||||||
|
string queueName = $"queue1";
|
||||||
|
|
||||||
channel.QueueDeclare("queue1");
|
channel.QueueDeclare(queue: queueName);
|
||||||
channel.QueueBind(queue: "queue1",
|
channel.QueueBind(queue: queueName,
|
||||||
exchange: "reports",
|
exchange: "reports",
|
||||||
routingKey: string.Empty);
|
routingKey: string.Empty);
|
||||||
|
|
||||||
Console.WriteLine(" [*] Ожидание сообщения");
|
Console.WriteLine(" [*] Waiting for messages.");
|
||||||
|
|
||||||
var consumer = new EventingBasicConsumer(channel);
|
var consumer = new EventingBasicConsumer(channel);
|
||||||
consumer.Received += (model, ea) =>
|
consumer.Received += async (model, ea) =>
|
||||||
{
|
{
|
||||||
byte[] body = ea.Body.ToArray();
|
byte[] body = ea.Body.ToArray();
|
||||||
var message = Encoding.UTF8.GetString(body);
|
var message = Encoding.UTF8.GetString(body);
|
||||||
|
|
||||||
Thread.Sleep(2500);
|
|
||||||
|
|
||||||
var reportNumber = message.Split('#')[1];
|
var reportNumber = message.Split('#')[1];
|
||||||
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
|
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
|
||||||
|
|
||||||
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
||||||
};
|
};
|
||||||
channel.BasicConsume(queue: "queue1",
|
channel.BasicConsume(queue: queueName,
|
||||||
autoAck: true,
|
autoAck: false,
|
||||||
consumer: consumer);
|
consumer: consumer);
|
||||||
|
|
||||||
Console.WriteLine(" Нажмите [enter] чтобы выйти.");
|
Console.WriteLine(" Press [enter] to exit.");
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
@ -6,27 +6,30 @@ var factory = new ConnectionFactory { HostName = "localhost" };
|
|||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
channel.QueueDeclare("queue2");
|
Random rand = new Random();
|
||||||
channel.QueueBind(queue: "queue2",
|
string queueName = $"queue{rand.Next()}";
|
||||||
exchange: "reports",
|
|
||||||
routingKey: string.Empty);
|
|
||||||
|
|
||||||
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);
|
var consumer = new EventingBasicConsumer(channel);
|
||||||
consumer.Received += (model, ea) =>
|
consumer.Received += async (model, ea) =>
|
||||||
{
|
{
|
||||||
byte[] body = ea.Body.ToArray();
|
byte[] body = ea.Body.ToArray();
|
||||||
var message = Encoding.UTF8.GetString(body);
|
var message = Encoding.UTF8.GetString(body);
|
||||||
|
|
||||||
var reportNumber = message.Split('#')[1];
|
var reportNumber = message.Split('#')[1];
|
||||||
|
Thread.Sleep(2500);
|
||||||
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
|
Console.WriteLine($" [x] Отчет #{reportNumber} создан.");
|
||||||
|
|
||||||
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
|
||||||
};
|
};
|
||||||
channel.BasicConsume(queue: "queue2",
|
channel.BasicConsume(queue: queueName,
|
||||||
autoAck: true,
|
autoAck: false,
|
||||||
consumer: consumer);
|
consumer: consumer);
|
||||||
|
|
||||||
Console.WriteLine(" Нажмите [enter] чтобы выйти.");
|
Console.WriteLine(" Press [enter] to exit.");
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
@ -5,17 +5,21 @@ var factory = new ConnectionFactory { HostName = "localhost" };
|
|||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
channel.ExchangeDeclare("reports", ExchangeType.Fanout);
|
channel.ExchangeDeclare(exchange: "reports", type: ExchangeType.Fanout);
|
||||||
|
Random rand = new Random();
|
||||||
var rand = new Random();
|
foreach (var item in Enumerable.Range(0, 1000))
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
var message = $"Необходимо создать отчет #{rand.Next()}";
|
var message = $"Необходимо создать отчет #{rand.Next()}";
|
||||||
|
|
||||||
var body = Encoding.UTF8.GetBytes(message);
|
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}");
|
||||||
Console.WriteLine($" [x] Отправлено \"{message}\"");
|
await Task.Delay(1000);
|
||||||
|
}
|
||||||
|
|
||||||
Thread.Sleep(1000);
|
Console.WriteLine(" Press [enter] to exit.");
|
||||||
}
|
Console.ReadLine();
|
||||||
|
149
tasks/mutriskov-ds/lab_4/README.md
Normal file
149
tasks/mutriskov-ds/lab_4/README.md
Normal 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 раза больше времени
|
BIN
tasks/mutriskov-ds/lab_4/images/c1-1.png
Normal file
BIN
tasks/mutriskov-ds/lab_4/images/c1-1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 218 KiB |
BIN
tasks/mutriskov-ds/lab_4/images/c1-2.png
Normal file
BIN
tasks/mutriskov-ds/lab_4/images/c1-2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 255 KiB |
Loading…
Reference in New Issue
Block a user