163 lines
5.5 KiB
Markdown
163 lines
5.5 KiB
Markdown
|
# Отчёт по лабораторной работе №4
|
|||
|
## Работа с брокером сообщений
|
|||
|
|
|||
|
Выполнила: студентка гр. ИСЭбд-41 Халитова А.М.
|
|||
|
|
|||
|
## Установка брокера сообщений RabbitMQ
|
|||
|
Была выполнена установка RabbitMQ и Erlang:
|
|||
|

|
|||
|

|
|||
|
|
|||
|
И выполнен вход на http://localhost:15672/ в качестве гостя:
|
|||
|

|
|||
|
|
|||
|
## Прохождение tutorial
|
|||
|
|
|||
|
tutorial1:
|
|||
|

|
|||
|

|
|||
|

|
|||
|
|
|||
|
tutorial2:
|
|||
|

|
|||
|

|
|||
|

|
|||
|

|
|||
|
|
|||
|
tutorial3:
|
|||
|

|
|||
|

|
|||
|

|
|||
|
|
|||
|
## Разработка демонстрационных приложений
|
|||
|
|
|||
|
Предметная область - заказ работ на исполнение. Производитель - заказчик работ. Потребитель 1 - исполнитель принимает заказ и выполняет работу. Потребитель 2 - исполнитель откладывает выполнение задачи.
|
|||
|
|
|||
|
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: "job", 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: "job",
|
|||
|
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: "job",
|
|||
|
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: "job",
|
|||
|
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();
|
|||
|
```
|
|||
|
|
|||
|
## Результаты выполнения работ
|
|||
|
|
|||
|
Запуск каждой программы по одному экземпляру:
|
|||
|

|
|||
|

|
|||
|

|
|||
|
|
|||
|
Результаты обработки:
|
|||
|

|
|||
|

|
|||
|
|
|||
|
Потребителю один соответствует очередь accepted, а потребителю два - очередь denied. Как видно, первый потребитель работает без задержки, а очередь второго потребителя переполенена. Из этого следует, что скорость первого в разы выше, и он потребляет намного меньше памяти за счет постоянно пустой очереди.
|
|||
|
|
|||
|
Запуск двух экземпляров потребителя два и по одному экземпляру потребителя один и заказчика:
|
|||
|

|
|||
|

|
|||
|
|
|||
|
Ситуация в очередью потребителя один не изменилась, а экземпляры потребителя два теперь затрачивают в 2 раза больше времени и памяти, их очереди так же переполнены, как и в ситуации выше.
|