basharin_sevastyan_lab_3 #25
71
basharin_sevastyan_lab_3/README.md
Normal file
71
basharin_sevastyan_lab_3/README.md
Normal file
@ -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"]
|
||||
```
|
@ -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
|
||||
context: ./nginx
|
||||
ports:
|
||||
- "80:80"
|
||||
depends_on:
|
||||
- user-service
|
||||
- order-service
|
||||
|
@ -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"]
|
@ -1,2 +0,0 @@
|
||||
Flask==3.0.0
|
||||
Werkzeug==3.0.1
|
@ -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/;
|
||||
}
|
||||
}
|
||||
}
|
4
basharin_sevastyan_lab_3/nginx/Dockerfile
Normal file
4
basharin_sevastyan_lab_3/nginx/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM nginx
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
19
basharin_sevastyan_lab_3/nginx/nginx.conf
Normal file
19
basharin_sevastyan_lab_3/nginx/nginx.conf
Normal file
@ -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/*;
|
||||
}
|
6
basharin_sevastyan_lab_3/order_service/Dockerfile
Normal file
6
basharin_sevastyan_lab_3/order_service/Dockerfile
Normal file
@ -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"]
|
60
basharin_sevastyan_lab_3/order_service/order_service.py
Normal file
60
basharin_sevastyan_lab_3/order_service/order_service.py
Normal file
@ -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/<int:order_id>', methods=['GET'])
|
||||
def get_order(order_id):
|
||||
# Реализация READ операции по желанию
|
||||
return jsonify({'error': 'Read operation not implemented in order service'}), 501
|
||||
|
||||
|
||||
# UPDATE
|
||||
@app.route('/orders/<int:order_id>', 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/<int:order_id>', 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)
|
2
basharin_sevastyan_lab_3/order_service/requirements.txt
Normal file
2
basharin_sevastyan_lab_3/order_service/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Flask==3.0.0
|
||||
pika==1.3.2
|
@ -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"]
|
@ -1,6 +0,0 @@
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
@ -1,2 +0,0 @@
|
||||
Flask==3.0.0
|
||||
Werkzeug==3.0.1
|
6
basharin_sevastyan_lab_3/user_service/Dockerfile
Normal file
6
basharin_sevastyan_lab_3/user_service/Dockerfile
Normal file
@ -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"]
|
2
basharin_sevastyan_lab_3/user_service/requirements.txt
Normal file
2
basharin_sevastyan_lab_3/user_service/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Flask==3.0.0
|
||||
pika==1.3.2
|
107
basharin_sevastyan_lab_3/user_service/user_service.py
Normal file
107
basharin_sevastyan_lab_3/user_service/user_service.py
Normal file
@ -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/<int:user_id>', 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/<int:user_id>', 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/<int:user_id>', 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)
|
Loading…
Reference in New Issue
Block a user