chernyshov_nikita_lab_4 #302

Closed
kitossy wants to merge 3 commits from chernyshov_nikita_lab_4 into main
36 changed files with 622 additions and 0 deletions

View File

@ -0,0 +1,22 @@
## Лабораторная работа №2
**App 1: Программа 4 - Количество символов в именах файлов из каталога /var/data**
- Формирует файл /var/result/data.txt так, что каждая строка файла - количество символов в именах файлов из каталога /var/data.
**App 2: Программа 3 - Количество чисел в последовательности**
- Ищет набольшее число из файла /var/result/data.txt и сохраняет количество таких чисел из последовательности в /var/result/result.txt.
**Структура проекта:**
1. В папках app_1, app_2 лежат выполняемые файлы .py и Dockerfile с нужным кодом.
2. В папке generator_of_data лежат выполняемые файлы для создания данных.
3. В папке data лежат файлы, длину имен которых нужно посчитать.
4. В папке result лежат файлы с результатами выполнения программ. data.txt - результат выполнения main.py (app_1), result.txt - результат выполнения main.py (app_2). Данные в result рассчитываются из данных data.
5. docker-compose.yml - для определения и управления контейнерами Docker.
**Команда для запуска** - docker-compose up
**Ссылка на видео:** https://vkvideo.ru/video286865610_456239225?list=ln-Xqg7SupM1CohXsGRZK

View File

@ -0,0 +1,20 @@
# Используем образ Python 3.9-slim как основу для нашего контейнера.
# slim-версия образа более компактная, что делает контейнер меньше.
FROM python:3.9-slim
# Устанавливаем рабочую директорию в контейнере как /app.
# Все последующие команды будут выполняться в этой директории.
WORKDIR /app
# Копируем файл main.py из текущей директории в директорию /app в контейнере.
COPY . /app
# Определяем команду, которая будет выполняться при запуске контейнера.
# В данном случае запускается Python-скрипт main.py.
CMD ["python", "main.py"]

View File

@ -0,0 +1,38 @@
import os
def get_file_with_most_lines(directory):
max_lines = 0
target_file = ""
for filename in os.listdir(directory):
filepath = os.path.join(directory, filename)
if os.path.isfile(filepath):
with open(filepath, 'r') as f:
line_count = sum(1 for _ in f)
if line_count > max_lines:
max_lines = line_count
target_file = filename
return target_file
def copy_file(src_directory, dest_directory, filename):
src_filepath = os.path.join(src_directory, filename)
dest_filepath = os.path.join(dest_directory, 'data.txt')
os.makedirs(dest_directory, exist_ok=True)
with open(src_filepath, 'r') as src_file:
with open(dest_filepath, 'w') as dest_file:
dest_file.write(src_file.read())
def main():
src_directory = '/var/data'
dest_directory = '/var/result'
target_file = get_file_with_most_lines(src_directory)
if target_file:
copy_file(src_directory, dest_directory, target_file)
print(f"File {target_file} copied to {dest_directory}/data.txt")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,7 @@
FROM python:3.9-slim
WORKDIR /app
COPY . /app
CMD ["python", "main.py"]

View File

@ -0,0 +1,25 @@
import os
def get_largest_number_from_file(filepath):
with open(filepath, 'r') as f:
numbers = [int(line.strip()) for line in f.readlines()]
return max(numbers)
def save_square_of_number(number, output_filepath):
result = number ** 2
with open(output_filepath, 'w') as f:
f.write(str(result))
def main():
input_filepath = '/var/result/data.txt'
output_filepath = '/var/result/result.txt'
if os.path.exists(input_filepath):
largest_number = get_largest_number_from_file(input_filepath)
save_square_of_number(largest_number, output_filepath)
print(f"Largest number squared: {largest_number}^2 saved to {output_filepath}")
else:
print(f"Input file {input_filepath} not found!")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,22 @@
version: '3'
services:
generator_of_data:
build:
context: ./generator_of_data
volumes:
- ./data:/var/data
app_1:
build:
context: ./app_1
volumes:
- ./data:/var/data
- ./result:/var/result
depends_on:
- generator_of_data
app_2:
build:
context: ./app_2
volumes:
- ./result:/var/result
depends_on:
- app_1

View File

@ -0,0 +1,7 @@
FROM python:3.9-slim
WORKDIR /app
COPY . /app
CMD ["python", "generate_data.py"]

View File

@ -0,0 +1,27 @@
import os
import random
def generate_random_files(directory, num_files, num_lines_per_file, min_value, max_value):
os.makedirs(directory, exist_ok=True)
for i in range(num_files):
file_path = os.path.join(directory, f"file_{i + 1}.txt")
with open(file_path, 'w') as f:
for _ in range(num_lines_per_file):
random_number = random.randint(min_value, max_value)
f.write(f"{random_number}\n")
print(f"Generated file: {file_path}")
def main():
data_directory = '/var/data'
num_files = 10
num_lines_per_file = 12
min_value = 1
max_value = 100
generate_random_files(data_directory, num_files, num_lines_per_file, min_value, max_value)
print(f"Generated {num_files} files in {data_directory}")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,36 @@
## Лабораторная работа №3
### Цель:
* Реализовать два микросервиса, которые взаимодействуют друг с другом через синхронный обмен сообщениями (HTTP-запросы). Для доступа к микросервисам используется шлюз Nginx, реализованный с помощью Docker Compose.
### Технологии:
* Python: Язык программирования для реализации микросервисов.
* Flask: Фреймворк Python для создания веб-приложений, использован для создания REST API микросервисов.
* requests: Библиотека Python для отправки HTTP-запросов, использован для синхронного обмена сообщениями между микросервисами.
* flask_cors: Расширение Flask, которое позволяет микросервисам получать доступ к данным из других доменов.
* Docker: Технология контейнеризации для упаковки и запуска микросервисов.
* Docker Compose: Инструмент для определения и управления многоконтейнерными приложениями, использован для запуска микросервисов и шлюза Nginx.
* Nginx: Сетевой прокси-сервер, использован как шлюз для доступа к микросервисам.
### Функциональность:
#### Микросервис games-service:
* Реализует CRUD операции для игр (GET, POST, PUT, DELETE).
* Сохраняет данные о играх в памяти (в словаре games).
* Получает информацию о жанре из микросервиса genres-service через HTTP-запрос.
* Включает информацию о жанре в ответ JSON для игры.
#### Микросервис genres-service:
* Реализует CRUD операции для жанров (GET, POST, PUT, DELETE).
* Сохраняет данные о жанре в памяти (в словаре genres).
#### Шлюз Nginx:
* Перенаправляет HTTP-запросы на соответствующие микросервисы.
* Предоставляет единую точку входа для доступа к микросервисам.
### Запуск программы:
* Запуск команды docker-compose up --build
### Ссылка на видео:
https://vkvideo.ru/video286865610_456239226?list=ln-U577n85GB3sBxUtF34

View File

@ -0,0 +1,26 @@
version: '3.8'
services:
games_service:
build:
context: ./games_service
dockerfile: Dockerfile
ports:
- "5000:5000"
genres_service:
build:
context: ./genres_service
dockerfile: Dockerfile
ports:
- "5001:5001"
nginx:
image: nginx:latest
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
depends_on:
- games_service
- genres_service

View File

@ -0,0 +1,10 @@
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

View File

@ -0,0 +1,53 @@
from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
games = {}
@app.route('/games', methods=['GET'])
def get_games():
return jsonify(list(games.values()))
@app.route('/games/<uuid:game_uuid>', methods=['GET'])
def get_game(game_uuid):
game = games.get(str(game_uuid))
if game:
return jsonify(game)
return jsonify({'error': 'Not found'}), 404
@app.route('/games', methods=['POST'])
def create_game():
data = request.get_json()
game_uuid = str(uuid.uuid4())
game = {
'uuid': game_uuid,
'name': data['name'],
'developer': data['developer'],
'genres': data.get('genres', []) # List of genre UUIDs
}
games[game_uuid] = game
return jsonify(game), 201
@app.route('/games/<uuid:game_uuid>', methods=['PUT'])
def update_game(game_uuid):
game = games.get(str(game_uuid))
if not game:
return jsonify({'error': 'Not found'}), 404
data = request.get_json()
game['name'] = data['name']
game['developer'] = data['developer']
game['genres'] = data.get('genres', [])
return jsonify(game)
@app.route('/games/<uuid:game_uuid>', methods=['DELETE'])
def delete_game(game_uuid):
if str(game_uuid) in games:
del games[str(game_uuid)]
return '', 204
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

View File

@ -0,0 +1 @@
Flask

View File

@ -0,0 +1,10 @@
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

View File

@ -0,0 +1,51 @@
from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
genres = {}
@app.route('/genres', methods=['GET'])
def get_genres():
return jsonify(list(genres.values()))
@app.route('/genres/<uuid:genre_uuid>', methods=['GET'])
def get_genre(genre_uuid):
genre = genres.get(str(genre_uuid))
if genre:
return jsonify(genre)
return jsonify({'error': 'Not found'}), 404
@app.route('/genres', methods=['POST'])
def create_genre():
data = request.get_json()
genre_uuid = str(uuid.uuid4())
genre = {
'uuid': genre_uuid,
'name': data['name'],
'description': data['description']
}
genres[genre_uuid] = genre
return jsonify(genre), 201
@app.route('/genres/<uuid:genre_uuid>', methods=['PUT'])
def update_genre(genre_uuid):
genre = genres.get(str(genre_uuid))
if not genre:
return jsonify({'error': 'Not found'}), 404
data = request.get_json()
genre['name'] = data['name']
genre['description'] = data['description']
return jsonify(genre)
@app.route('/genres/<uuid:genre_uuid>', methods=['DELETE'])
def delete_genre(genre_uuid):
if str(genre_uuid) in genres:
del genres[str(genre_uuid)]
return '', 204
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)

View File

@ -0,0 +1 @@
Flask

View File

@ -0,0 +1,11 @@
server {
listen 80;
location /games {
proxy_pass http://games_service:5000;
}
location /genres {
proxy_pass http://genres_service:5001;
}
}

View File

@ -0,0 +1,30 @@
import pika
import time
def callback(ch, method, properties, body):
print(f'Consumer 1 получил сообщение: {body.decode()}')
# Время задержки по условию
time.sleep(2)
print('Consumer 1 закончил обработку')
def consume_events_1():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
# Создание очереди
channel.queue_declare(queue='consumer1_queue')
# Привязка очереди
channel.queue_bind(exchange='beauty_salon_events', queue='consumer1_queue')
channel.basic_consume(queue='consumer1_queue', on_message_callback=callback, auto_ack=True)
print('Consumer 1 начал ожидать сообщения...')
channel.start_consuming()
if __name__ == "__main__":
consume_events_1()

View File

@ -0,0 +1,28 @@
import pika
def callback(ch, method, properties, body):
print(f'Consumer 2 получил сообщение: {body.decode()}')
# Обработка "нон-стопом"
print('Consumer 2 закончил обработку')
def consume_events_2():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
# Создание очереди
channel.queue_declare(queue='consumer2_queue')
# Привязка очереди
channel.queue_bind(exchange='beauty_salon_events', queue='consumer2_queue')
channel.basic_consume(queue='consumer2_queue', on_message_callback=callback, auto_ack=True)
print('Consumer 2 начал ожидать сообщения...')
channel.start_consuming()
if __name__ == "__main__":
consume_events_2()

View File

@ -0,0 +1,28 @@
import pika
import time
def publish_events():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
# Создание exchange типа fanout
channel.exchange_declare(exchange='beauty_salon_events', exchange_type='fanout')
events = [
"Test1",
"Test2",
"Test3",
"Test4",
"Test5"
]
while True:
event = events[int(time.time()) % len(events)]
channel.basic_publish(exchange='beauty_salon_events', routing_key='', body=event)
print(f'Отправлено: {event}')
time.sleep(1)
if __name__ == "__main__":
publish_events()

View File

@ -0,0 +1,56 @@
### Лабораторная работа №4
#### Задание
1. Установить брокер сообщений RabbitMQ.
2. Пройти уроки 1, 2 и 3 из RabbitMQ Tutorials на любом языке программирования.
3. Продемонстрировать работу брокера сообщений.
#### Описание работы программы:
- **Класс Publisher** успешно осуществляет отправку сообщений своим клиентам.
- **Класс Consumer1** принимает и обрабатывает сообщения с задержкой в 3 секунды, что можно заметить на видео.
- **Класс Consumer2** мгновенно принимает и обрабатывает сообщения.
#### Уроки
1. lesson1
![lesson1.png](lesson1.png)
2. lesson2
![lesson2.png](lesson2.png)
3. lesson3
![lesson3.png](lesson3.png)
## Работа с RabbitMQ Management UI
![img1.png](img1.png)
## Показания очереди queue_1 при одном запущенном экземпляре Consumer_1
![img2.png](img2.png)
## Показания очереди queue_2
![img3.png](img3.png)
## Показания очереди queue_1 при двух запущенных экземплярах Consumer_1
![img4.png](img4.png)
## Показания очереди queue_1 при трех запущенных экземплярах Consumer_1
![img5.png](img5.png)
## Диспетчер задач
![img6.png](img6.png)
## Видео
https://vkvideo.ru/video286865610_456239227?list=ln-EUw7H8FIzc8ZwX8rG7

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

View File

@ -0,0 +1,25 @@
import pika, sys, os
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)

View File

@ -0,0 +1,11 @@
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@ -0,0 +1,19 @@
import pika
import sys
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(
exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode=pika.DeliveryMode.Persistent
))
print(f" [x] Sent {message}")
connection.close()

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(f" [x] Received {body.decode()}")
time.sleep(body.count(b'.'))
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
channel.start_consuming()

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

View File

@ -0,0 +1,13 @@
import pika
import sys
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(f" [x] Sent {message}")
connection.close()

View File

@ -0,0 +1,22 @@
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs', queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(f" [x] {body}")
channel.basic_consume(
queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()