This commit is contained in:
2025-12-08 23:35:04 +04:00
parent 3ab10ab9d2
commit afe292cde4
9 changed files with 412 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
# Лабораторная работа №3 - REST API, Gateway и синхронный обмен между микросервисами
Цель: изучение шаблона проектирования gateway, построения синхронного обмена между микросервисами и архитектурного стиля RESTful API.
Задачи:
1. Создать 2 микросервиса, реализующих CRUD на связанных сущностях.
2. Реализовать механизм синхронного обмена сообщениями между микросервисами.
3. Реализовать шлюз на основе прозрачного прокси-сервера nginx.
## Предметная область:
В качестве предметной области использовался пример 19-го варианта. Имеются 2 сущности: одна сущность - книга в библиотеке, вторая - тематика.
Поля тематики: УИД (уникальный идентификатор) тематики, номер, название. Поля книги: УИД книги, Автор, Название, Год издания, УИД тематики, к которой принадлежит книга.
## Запуск ЛР:
Введем в терминале команду:
```
docker compose up --build
```
Эта команда сначала выполнит сборку, а затем запустит контейнеры.
Также нужно убедиться в том,что 80 порт свободен и тогда вся система запустится.
Запускаем Postman и запускаем следующие запросы:
![](![alt text](lab3.png) "")
Для каждой сущности были реализованы CRUD-операции.
Theme сервис:
GET /themes — получить все темы
POST /themes — создать новую тему
GET /themes/{id} — получить тему по ID
GET /themes/with-books/{themeId} — получить все книги, которые относятся к themeId
PUT /themes/{id} — обновить тему по ID
DELETE /themes/{id} — удалить тему по ID
Book сервис:
GET /books — получить все книги
POST /books — создать новую книгу
GET /books/{id} — получить книгу по ID
PUT /books/{id} — обновить книгу по ID
DELETE /books/{id} — удалить книгу по ID
# Видео
https://disk.yandex.ru/i/0_Qnv_Swm490eQ

View File

@@ -0,0 +1,16 @@
FROM python:latest
# Устанавливаю рабочую директорию внутри контейнера
WORKDIR /app
# Копирую файл requirements.txt в контейнер
COPY requirements.txt .
# Устанавливаю зависимости
RUN pip install --no-cache-dir -r requirements.txt
# Копирую все файлы в контейнер
COPY book_service/book_service.py .
# Команда для запуска Python-скрипта
CMD ["python", "book_service.py"]

View File

@@ -0,0 +1,150 @@
from flask import Flask, jsonify, request
import requests
from uuid import uuid4
import uuid
class Book:
def __init__(self, title, year, author, uuid_: uuid, theme_id: uuid):
if uuid_ is None:
self.uuid_: uuid = uuid4()
else:
self.uuid_: uuid = uuid.UUID(uuid_)
self.title: str = title
self.year: int = year
self.author: str = author
self.theme_id: uuid = uuid.UUID(theme_id)
def to_dict(self):
return {
'title': self.title,
'year': self.year,
'author': self.author,
'theme_id': str(self.theme_id),
'uuid': str(self.uuid_)
}
def to_dict_for_themes(self):
return {
'title': self.title,
'year': self.year,
'author': self.author,
'uuid': str(self.uuid_)
}
app = Flask(__name__)
books: list[Book] = [
Book(title='Mac', year=1977, author='Jane', uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19',
theme_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'),
Book(title='Portrait', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', author='Mark',
theme_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'),
Book(title='Chamomile', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, author='Laura',
theme_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30')
]
# Исправленный URL - убрал "http://" если это внутренний сервис в Docker
themes_url = 'http://theme_service:20001' # Убрал слеш в конце
def list_jsonify():
return jsonify([book.to_dict() for book in books])
# получить список книг
@app.route('/', methods=['GET'])
def get_all():
return list_jsonify(), 200
# получение списка книг по идентификатору темы (для theme_service)
@app.route('/by-theme/<uuid:theme_uuid>', methods=['GET'])
def get_by_theme_id(theme_uuid):
return jsonify([book.to_dict_for_themes() for book in books if book.theme_id == theme_uuid]), 200
# получение книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['GET'])
def get_one(uuid_):
for book in books:
if book.uuid_ == uuid_:
return jsonify(book.to_dict()), 200
return 'Книга с таким uuid не была найдена', 404
# создание новой книги
@app.route('/', methods=['POST'])
def create():
try:
data = request.json
if not data:
return 'Отсутствуют данные', 400
title = data.get('title')
year = data.get('year')
author = data.get('author')
theme_id = data.get('theme_id')
# Проверка обязательных полей
if not all([title, year, author, theme_id]):
return 'Не все обязательные поля заполнены', 400
# Проверка существования темы
try:
checking = requests.get(f'{themes_url}/check/{theme_id}', timeout=5)
print(f"Status code: {checking.status_code}")
except requests.exceptions.ConnectionError:
return 'Сервис тем недоступен', 503
except requests.exceptions.Timeout:
return 'Таймаут при подключении к сервису тем', 504
if checking.status_code == 200:
new_book = Book(title, year, author, None, theme_id)
books.append(new_book)
return jsonify(new_book.to_dict()), 201
elif checking.status_code == 404:
return 'Тема с таким uuid не существует', 404
else:
return f'Ошибка сервиса тем: {checking.status_code}', 500
except Exception as e:
print(f"Error: {e}")
return 'Внутренняя ошибка сервера', 500
# изменение книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['PUT'])
def update_by_id(uuid_):
data = request.json
new_title = data.get('title')
new_year = data.get('year')
new_author = data.get('author')
for book in books:
if book.uuid_ == uuid_:
if new_title is not None:
book.title = new_title
if new_year is not None:
book.year = new_year
if new_author is not None:
book.author = new_author
return jsonify(book.to_dict()), 200
return 'Книга с таким uuid не была найдена', 404
# удаление книги по идентификатору
@app.route('/<uuid:uuid_>', methods=['DELETE'])
def delete(uuid_):
for book in books:
if book.uuid_ == uuid_:
books.remove(book)
return 'Книга успешно удалена', 200
return 'Книга с таким uuid не была найдена', 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=20002, debug=True)

View File

@@ -0,0 +1,29 @@
services:
book_service:
container_name: book_service
build:
context: .
dockerfile: ./book_service/Dockerfile
expose:
- 20001
theme_service:
container_name: theme_service
build:
context: .
dockerfile: ./theme_service/Dockerfile
expose:
- 20002
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- book_service
- theme_service

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@@ -0,0 +1,26 @@
events { worker_connections 1024; }
http {
server {
listen 80;
listen [::]:80;
server_name localhost;
location /theme_service/ {
proxy_pass http://theme_service:20001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /book_service/ {
proxy_pass http://book_service:20002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

View File

@@ -0,0 +1,3 @@
Flask==3.0.3
requests==2.32.3

View File

@@ -0,0 +1,16 @@
FROM python:latest
# Устанавливаю рабочую директорию внутри контейнера
WORKDIR /app
# Копирую файл requirements.txt в контейнер
COPY requirements.txt .
# Устанавливаю зависимости
RUN pip install --no-cache-dir -r requirements.txt
# Копирую все файлы в контейнер
COPY theme_service/theme_service.py .
# Команда для запуска Python-скрипта
CMD ["python", "theme_service.py"]

View File

@@ -0,0 +1,118 @@
from flask import Flask, jsonify, request
from uuid import uuid4
import uuid
import requests
class Theme:
def __init__(self, number, name, uuid_: uuid):
if uuid_ is None:
self.uuid_: uuid = uuid4()
else:
self.uuid_: uuid = uuid.UUID(uuid_)
self.number: str = number
self.name: str = name
def to_dict(self):
return {
"uuid": self.uuid_,
"number": self.number,
"name": self.name
}
def to_dict_with_books(self, books: list):
return {
"uuid": self.uuid_,
"number": self.number,
"name": self.name,
"books": books
}
app = Flask(__name__)
themes: list[Theme] = [
Theme(name='Flowers', number='1', uuid_='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'),
Theme(name='Art', number='2', uuid_='694827e4-0f93-45a5-8f75-bad7ef2d21fe')
]
books_url = 'http://book_service:20002/'
def list_jsonify():
return jsonify([theme.to_dict() for theme in themes])
# получение списка всех тем
@app.route('/', methods=['GET'])
def get_all():
return list_jsonify(), 200
# получение темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['GET'])
def get_one(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
return theme.to_dict(), 200
return 'Тема с таким uuid не была найдена', 404
# получение темы со списком книг по ней
@app.route('/with-books/<uuid:uuid_>', methods=['GET'])
def get_one_with_books(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
response = requests.get(books_url + f'by-theme/{uuid_}')
print(response.json())
return theme.to_dict_with_books(response.json()), 200
return 'Тема с таким uuid не была найдена', 404
# создание темы
@app.route('/', methods=['POST'])
def create():
data = request.json
number = data.get('number', None)
name = data.get('name', None)
if name is None or number is None:
return 'Недостаточно полей для создания новой темы', 404
new_theme = Theme(number, name, None)
themes.append(new_theme)
return get_one(new_theme.uuid_)
# изменение темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['PUT'])
def update_by_id(uuid_):
data = request.json
new_number = data.get('number', None)
new_name = data.get('name', None)
for theme in themes:
if theme.uuid_ == uuid_:
if new_number is not None:
theme.number = new_number
if new_name is not None:
theme.name = new_name
return get_one(theme.uuid_)
return 'Тема с таким uuid не была найдена', 404
# удаление темы по идентификатору
@app.route('/<uuid:uuid_>', methods=['DELETE'])
def delete(uuid_):
for theme in themes:
if theme.uuid_ == uuid_:
themes.remove(theme)
return 'Тема успешно удалена', 200
return 'Тема с таким uuid не была найдена', 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=20001, debug=True)