distributed-computing/tasks/grenaderova-aa/lab_4/README.md
2023-12-18 16:32:53 +04:00

111 lines
3.0 KiB
Markdown
Raw Permalink 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 Гренадерова А.А.
## Прохождение tutorial
Установила rabbitMQ server, erlang и зашла в брокер под гостем по http://localhost:15672/#/
Туториал 1:
![](screens/4.png)
Туториал 2:
![](screens/5.png)
Туториал 3:
![](screens/6.png)
## Разработка демонстрационных приложений
Предметная область: Городское планирование и архитектура.
Разработала три приложения согласно предметной области.
1. Publisher
```py
import pika
import time
import random
collection = ["Buildings", "Streets", "Parks", "Transport", "Lighting"]
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='Entities', exchange_type='fanout')
while True:
message = f"Message: {random.choice(collection)}"
channel.basic_publish(exchange='Entities', routing_key='', body=message)
time.sleep(1)
connection.close()
```
2. Consumer 1.
```py
import pika
import time
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
time.sleep(3)
print("Сообщение обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Entities', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений')
channel.start_consuming()
```
3. Consumer 2.
```py
import pika
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
print("Сообщение обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Entities', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений')
channel.start_consuming()
```
## Результаты выполнения лабораторной работы
Результат отработки Consumer_1:
![](screens/2.png)
Результат отработки Consumer_2:
![](screens/3.png)
![](screens/1.png)
Вывод: Consumer_2 нагружает меньше памяти, чем Consumer_1 и принимает сообщения гораздо быстрее, тем самым не позволяя очереди накапливать огромное количество сообщений.