From 8ff2a44649e2edc514f4b816d949374ac03ed0a0 Mon Sep 17 00:00:00 2001 From: acidmikk Date: Tue, 21 Nov 2023 23:45:46 +0400 Subject: [PATCH] ready< not test --- basharin_sevastyan_lab_3/README.md | 71 ++++++++++++ basharin_sevastyan_lab_3/docker-compose.yaml | 58 ++++------ .../message_app/Dockerfile | 8 -- basharin_sevastyan_lab_3/message_app/main.py | 0 .../message_app/requirements.txt | 2 - basharin_sevastyan_lab_3/nginx.conf | 28 ----- basharin_sevastyan_lab_3/nginx/Dockerfile | 4 + basharin_sevastyan_lab_3/nginx/nginx.conf | 19 ++++ .../order_service/Dockerfile | 6 + .../order_service/order_service.py | 60 ++++++++++ .../order_service/requirements.txt | 2 + basharin_sevastyan_lab_3/user_app/Dockerfile | 8 -- basharin_sevastyan_lab_3/user_app/main.py | 6 - .../user_app/requirements.txt | 2 - .../user_service/Dockerfile | 6 + .../user_service/requirements.txt | 2 + .../user_service/user_service.py | 107 ++++++++++++++++++ 17 files changed, 301 insertions(+), 88 deletions(-) create mode 100644 basharin_sevastyan_lab_3/README.md delete mode 100644 basharin_sevastyan_lab_3/message_app/Dockerfile delete mode 100644 basharin_sevastyan_lab_3/message_app/main.py delete mode 100644 basharin_sevastyan_lab_3/message_app/requirements.txt delete mode 100644 basharin_sevastyan_lab_3/nginx.conf create mode 100644 basharin_sevastyan_lab_3/nginx/Dockerfile create mode 100644 basharin_sevastyan_lab_3/nginx/nginx.conf create mode 100644 basharin_sevastyan_lab_3/order_service/Dockerfile create mode 100644 basharin_sevastyan_lab_3/order_service/order_service.py create mode 100644 basharin_sevastyan_lab_3/order_service/requirements.txt delete mode 100644 basharin_sevastyan_lab_3/user_app/Dockerfile delete mode 100644 basharin_sevastyan_lab_3/user_app/main.py delete mode 100644 basharin_sevastyan_lab_3/user_app/requirements.txt create mode 100644 basharin_sevastyan_lab_3/user_service/Dockerfile create mode 100644 basharin_sevastyan_lab_3/user_service/requirements.txt create mode 100644 basharin_sevastyan_lab_3/user_service/user_service.py diff --git a/basharin_sevastyan_lab_3/README.md b/basharin_sevastyan_lab_3/README.md new file mode 100644 index 0000000..e4c6c46 --- /dev/null +++ b/basharin_sevastyan_lab_3/README.md @@ -0,0 +1,71 @@ +``` +basharin_sevastyan_lab_3/ +|-- user_service/ +| |-- Dockerfile +| |-- requirements.txt +| |-- user_service.py +|-- order_service/ +| |-- Dockerfile +| |-- requirements.txt +| |-- order_service.py +|-- nginx/ +| |-- Dockerfile +| |-- nginx.conf +|-- docker-compose.yml +``` + +```yaml +version: '3' + +services: + user-service: + build: + context: ./user_service + ports: + - "5001:5001" + depends_on: + - rabbitmq + + order-service: + build: + context: ./order_service + ports: + - "5002:5002" + depends_on: + - rabbitmq + + rabbitmq: + image: "rabbitmq:management" + ports: + - "5672:5672" + - "15672:15672" + + nginx: + build: + context: ./nginx + ports: + - "80:80" + depends_on: + - user-service + - order-service +``` + +```dockerfile +# user_service/Dockerfile +FROM python:3.11 +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "user_service.py"] +``` + +```dockerfile +# user_service/Dockerfile +FROM python:3.11 +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "order_service.py"] +``` \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/docker-compose.yaml b/basharin_sevastyan_lab_3/docker-compose.yaml index 1140c63..104c23e 100644 --- a/basharin_sevastyan_lab_3/docker-compose.yaml +++ b/basharin_sevastyan_lab_3/docker-compose.yaml @@ -1,43 +1,33 @@ version: '3' services: - nginx: - image: nginx:latest + user-service: + build: + context: ./user_service ports: - - 80:80 - volumes: - - ./nginx.conf:/etc/nginx/nginx.conf - restart: always + - "5001:5001" depends_on: - - product-service - - order-service - networks: - - mynetwork + - rabbitmq - db: - image: postgres:latest + order-service: + build: + context: ./order_service ports: - - 5432:5432 - environment: - POSTGRES_PASSWORD: admin - POSTGRES_USER: admin - POSTGRES_DB: message-api - volumes: - - ./database.sql:/docker-entrypoint-initdb.d/database.sql - restart: always - networks: - - mynetwork + - "5002:5002" + depends_on: + - rabbitmq - user_app: + rabbitmq: + image: "rabbitmq:management" + ports: + - "5672:5672" + - "15672:15672" + + nginx: build: - context: /user_app - dockerfile: Dockerfile - - message_app: - build: - context: /message_app - dockerfile: Dockerfile - -networks: - mynetwork: - driver: bridge \ No newline at end of file + context: ./nginx + ports: + - "80:80" + depends_on: + - user-service + - order-service diff --git a/basharin_sevastyan_lab_3/message_app/Dockerfile b/basharin_sevastyan_lab_3/message_app/Dockerfile deleted file mode 100644 index 77d7b3c..0000000 --- a/basharin_sevastyan_lab_3/message_app/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM python:3.9 -WORKDIR /app -COPY requirements.txt . -RUN pip install -r requirements.txt -COPY . . -COPY var/data /var/data -COPY var/result /var/result -CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/message_app/main.py b/basharin_sevastyan_lab_3/message_app/main.py deleted file mode 100644 index e69de29..0000000 diff --git a/basharin_sevastyan_lab_3/message_app/requirements.txt b/basharin_sevastyan_lab_3/message_app/requirements.txt deleted file mode 100644 index 5beea3b..0000000 --- a/basharin_sevastyan_lab_3/message_app/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Flask==3.0.0 -Werkzeug==3.0.1 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/nginx.conf b/basharin_sevastyan_lab_3/nginx.conf deleted file mode 100644 index 13ebcea..0000000 --- a/basharin_sevastyan_lab_3/nginx.conf +++ /dev/null @@ -1,28 +0,0 @@ -events { - worker_connections 1024; -} - -http { - - upstream user-service { - server product-service:8080; - } - upstream message-service { - server order-service:8081; - } - - server { - - listen 80; - listen [::]:80; - server_name localhost; - - location /user-service/ { - proxy_pass http://user-service/; - } - - location /message-service/ { - proxy_pass http://message-service/; - } - } -} \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/nginx/Dockerfile b/basharin_sevastyan_lab_3/nginx/Dockerfile new file mode 100644 index 0000000..1434a93 --- /dev/null +++ b/basharin_sevastyan_lab_3/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx +COPY nginx.conf /etc/nginx/nginx.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/basharin_sevastyan_lab_3/nginx/nginx.conf b/basharin_sevastyan_lab_3/nginx/nginx.conf new file mode 100644 index 0000000..2f4ae77 --- /dev/null +++ b/basharin_sevastyan_lab_3/nginx/nginx.conf @@ -0,0 +1,19 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; +} \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/order_service/Dockerfile b/basharin_sevastyan_lab_3/order_service/Dockerfile new file mode 100644 index 0000000..46d249e --- /dev/null +++ b/basharin_sevastyan_lab_3/order_service/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.11 +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "order_service.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/order_service/order_service.py b/basharin_sevastyan_lab_3/order_service/order_service.py new file mode 100644 index 0000000..376e354 --- /dev/null +++ b/basharin_sevastyan_lab_3/order_service/order_service.py @@ -0,0 +1,60 @@ +# order_service.py +from flask import Flask, request, jsonify +import pika +import json + +app = Flask(__name__) + +# Конфигурация RabbitMQ +connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) +channel = connection.channel() +channel.exchange_declare(exchange='microservices', exchange_type='direct') + + +# CRUD операции для заказов в order_service оставим здесь, чтобы он мог отправлять сообщения + +# CREATE +@app.route('/orders', methods=['POST']) +def create_order(): + data = request.get_json() + order = {'user_id': data['user_id'], 'product': data['product'], 'action': 'create'} + + # Отправка сообщения о создании заказа + channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) + + return jsonify(order), 201 + + +# READ +@app.route('/orders/', methods=['GET']) +def get_order(order_id): + # Реализация READ операции по желанию + return jsonify({'error': 'Read operation not implemented in order service'}), 501 + + +# UPDATE +@app.route('/orders/', methods=['PUT']) +def update_order(order_id): + data = request.get_json() + order = {'user_id': data['user_id'], 'product': data['product'], 'order_id': order_id, 'action': 'update'} + + # Отправка сообщения об обновлении заказа + channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) + + return jsonify(order) + + +# DELETE +@app.route('/orders/', methods=['DELETE']) +def delete_order(order_id): + user_id = request.args.get('user_id') + order = {'user_id': user_id, 'order_id': order_id, 'action': 'delete'} + + # Отправка сообщения об удалении заказа + channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) + + return jsonify({'result': True}) + + +if __name__ == '__main__': + app.run(port=5002) diff --git a/basharin_sevastyan_lab_3/order_service/requirements.txt b/basharin_sevastyan_lab_3/order_service/requirements.txt new file mode 100644 index 0000000..920c2a7 --- /dev/null +++ b/basharin_sevastyan_lab_3/order_service/requirements.txt @@ -0,0 +1,2 @@ +Flask==3.0.0 +pika==1.3.2 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_app/Dockerfile b/basharin_sevastyan_lab_3/user_app/Dockerfile deleted file mode 100644 index 77d7b3c..0000000 --- a/basharin_sevastyan_lab_3/user_app/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM python:3.9 -WORKDIR /app -COPY requirements.txt . -RUN pip install -r requirements.txt -COPY . . -COPY var/data /var/data -COPY var/result /var/result -CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_app/main.py b/basharin_sevastyan_lab_3/user_app/main.py deleted file mode 100644 index 46d2a8f..0000000 --- a/basharin_sevastyan_lab_3/user_app/main.py +++ /dev/null @@ -1,6 +0,0 @@ -from flask import Flask - -app = Flask(__name__) - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) diff --git a/basharin_sevastyan_lab_3/user_app/requirements.txt b/basharin_sevastyan_lab_3/user_app/requirements.txt deleted file mode 100644 index 5beea3b..0000000 --- a/basharin_sevastyan_lab_3/user_app/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Flask==3.0.0 -Werkzeug==3.0.1 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_service/Dockerfile b/basharin_sevastyan_lab_3/user_service/Dockerfile new file mode 100644 index 0000000..467cb44 --- /dev/null +++ b/basharin_sevastyan_lab_3/user_service/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.11 +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "user_service.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_service/requirements.txt b/basharin_sevastyan_lab_3/user_service/requirements.txt new file mode 100644 index 0000000..920c2a7 --- /dev/null +++ b/basharin_sevastyan_lab_3/user_service/requirements.txt @@ -0,0 +1,2 @@ +Flask==3.0.0 +pika==1.3.2 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_service/user_service.py b/basharin_sevastyan_lab_3/user_service/user_service.py new file mode 100644 index 0000000..cd6e450 --- /dev/null +++ b/basharin_sevastyan_lab_3/user_service/user_service.py @@ -0,0 +1,107 @@ +# user_service.py +from flask import Flask, request, jsonify +import pika +import json + +app = Flask(__name__) + +# Конфигурация RabbitMQ +connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) +channel = connection.channel() +channel.exchange_declare(exchange='microservices', exchange_type='direct') + +users = [] + + +# Обработка сообщений о заказах +def process_order_message(ch, method, properties, body): + data = json.loads(body) + user_id = data.get('user_id') + + if data['action'] == 'create': + # Создание заказа у пользователя + user = next((user for user in users if user['id'] == user_id), None) + if user: + order_id = len(user.get('orders', [])) + 1 + order = {'id': order_id, 'product': data['product']} + user.setdefault('orders', []).append(order) + print(f"Order created for user {user_id}: {order}") + else: + print(f"User not found for order creation: {user_id}") + + elif data['action'] == 'update': + # Обновление заказа у пользователя + user = next((user for user in users if user['id'] == user_id), None) + if user: + order_id = data.get('order_id') + order = next((order for order in user['orders'] if order['id'] == order_id), None) + if order: + order['product'] = data['product'] + print(f"Order updated for user {user_id}: {order}") + else: + print(f"Order not found for update: {order_id}") + else: + print(f"User not found for order update: {user_id}") + + elif data['action'] == 'delete': + # Удаление заказа у пользователя + user = next((user for user in users if user['id'] == user_id), None) + if user: + order_id = data.get('order_id') + user['orders'] = [order for order in user.get('orders', []) if order['id'] != order_id] + print(f"Order deleted for user {user_id}: {order_id}") + else: + print(f"User not found for order deletion: {user_id}") + + +# Начало прослушивания сообщений о заказах +channel.queue_declare(queue='order_service', durable=True) +channel.queue_bind(exchange='microservices', queue='order_service', routing_key='order') +channel.basic_consume(queue='order_service', on_message_callback=process_order_message, auto_ack=True) + +print('User service is waiting for order messages. To exit press CTRL+C') +channel.start_consuming() + + +# CRUD операции для пользователей +# CREATE +@app.route('/users', methods=['POST']) +def create_user(): + data = request.get_json() + user = {'id': len(users) + 1, 'name': data['name']} + users.append(user) + return jsonify(user), 201 + + +# READ +@app.route('/users/', methods=['GET']) +def get_user(user_id): + user = next((user for user in users if user['id'] == user_id), None) + if user: + return jsonify(user) + else: + return jsonify({'error': 'User not found'}), 404 + + +# UPDATE +@app.route('/users/', methods=['PUT']) +def update_user(user_id): + user = next((user for user in users if user['id'] == user_id), None) + if user: + data = request.get_json() + user['name'] = data['name'] + return jsonify(user) + else: + return jsonify({'error': 'User not found'}), 404 + + +# DELETE +@app.route('/users/', methods=['DELETE']) +def delete_user(user_id): + global users + users = [user for user in users if user['id'] != user_id] + return jsonify({'result': True}) + + +if __name__ == '__main__': + app.run(port=5001)