distributed-computing/tasks/mikhailov-ys/README.md
2024-01-06 23:45:37 +04:00

150 lines
4.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Отчёт по лабораторной работе №4
Выполнил: студент гр. ИСЭбд-41 Михайлов Ю.С.
## Прохождение туториалов
Установил RabbitMQ, Erlang и зашел в брокер под гостем по адресу http://localhost:15672/#/
Туториал 1:
![](pic/1.PNG)
![](pic/2.PNG)
![](pic/3.PNG)
Туториал 2:
![](pic/4.PNG)
![](pic/5.PNG)
![](pic/6.PNG)
![](pic/7.PNG)
Туториал 3:
![](pic/8.PNG)
![](pic/9.PNG)
![](pic/10.PNG)
## Разработка демонстрационных приложений
Предметная область - оформление заявлений на стипендию в университете. Разработано три приложения согласно предметной области.
1. Publisher:
```
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: "report", type: ExchangeType.Fanout);
Random rand = new Random();
foreach (var item in Enumerable.Range(0, 1000))
{
var message = rand.Next().ToString();
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "report",
routingKey: string.Empty,
basicProperties: null,
body: body);
Console.WriteLine($" [x] Поступило заявление {message}");
await Task.Delay(500);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
static string GetMessage(string[] args)
{
return ((args.Length > 0) ? string.Join(" ", args) : "info: Принято!");
}
```
2. Consumer 1:
```
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();
channel.QueueDeclare(queue: "accepted");
channel.QueueBind(queue: "accepted",
exchange: "report",
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);
string outputText = $"Заявление принято к обработке {message}";
Console.WriteLine($" [x] Done. {outputText}");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "accepted",
autoAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
```
3. Consumer 3:
```
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 = $"denied{rand.Next()}";
channel.QueueDeclare(queue: queueName);
channel.QueueBind(queue: queueName,
exchange: "report",
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);
int waitTime = rand.Next(20, 90);
Thread.Sleep(waitTime * 100);
string outputText = $"Заявление обработано {message} за {waitTime} минут";
Console.WriteLine($" [x] Done. {outputText}");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: queueName,
autoAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
```
## Результаты выполнения работы
Запуск каждой программы:
![](pic/11.PNG)
![](pic/12.PNG)
![](pic/13.PNG)
Результаты обработки:
![](pic/14.PNG)
![](pic/15.PNG)
Вывод: Consumer_2 нагружает меньше памяти, чем Consumer_1 и принимает сообщения гораздо быстрее, тем самым не позволяя очереди накапливать огромное количество сообщений