forked from Alexey/DAS_2023_1
worked nginx
This commit is contained in:
parent
8ff2a44649
commit
83528356a1
@ -1,6 +1,15 @@
|
|||||||
version: '3'
|
version: '3.8'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
rabbitmq:
|
||||||
|
image: "rabbitmq:management"
|
||||||
|
ports:
|
||||||
|
- "5672:5672"
|
||||||
|
- "15672:15672"
|
||||||
|
networks:
|
||||||
|
- my_network
|
||||||
|
restart: always
|
||||||
|
|
||||||
user-service:
|
user-service:
|
||||||
build:
|
build:
|
||||||
context: ./user_service
|
context: ./user_service
|
||||||
@ -8,6 +17,8 @@ services:
|
|||||||
- "5001:5001"
|
- "5001:5001"
|
||||||
depends_on:
|
depends_on:
|
||||||
- rabbitmq
|
- rabbitmq
|
||||||
|
networks:
|
||||||
|
- my_network
|
||||||
|
|
||||||
order-service:
|
order-service:
|
||||||
build:
|
build:
|
||||||
@ -16,18 +27,21 @@ services:
|
|||||||
- "5002:5002"
|
- "5002:5002"
|
||||||
depends_on:
|
depends_on:
|
||||||
- rabbitmq
|
- rabbitmq
|
||||||
|
networks:
|
||||||
rabbitmq:
|
- my_network
|
||||||
image: "rabbitmq:management"
|
|
||||||
ports:
|
|
||||||
- "5672:5672"
|
|
||||||
- "15672:15672"
|
|
||||||
|
|
||||||
nginx:
|
nginx:
|
||||||
build:
|
image: nginx:latest
|
||||||
context: ./nginx
|
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
depends_on:
|
depends_on:
|
||||||
- user-service
|
- user-service
|
||||||
- order-service
|
- order-service
|
||||||
|
networks:
|
||||||
|
- my_network
|
||||||
|
volumes:
|
||||||
|
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
|
||||||
|
|
||||||
|
networks:
|
||||||
|
my_network:
|
||||||
|
driver: bridge
|
@ -1,19 +1,27 @@
|
|||||||
worker_processes 1;
|
|
||||||
|
|
||||||
events {
|
events {
|
||||||
worker_connections 1024;
|
worker_connections 1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
http {
|
http {
|
||||||
include /etc/nginx/mime.types;
|
server {
|
||||||
default_type application/octet-stream;
|
listen 80;
|
||||||
|
listen [::]:80;
|
||||||
|
server_name localhost;
|
||||||
|
|
||||||
sendfile on;
|
location /user-service/ {
|
||||||
tcp_nopush on;
|
proxy_pass http://user-service:5001/;
|
||||||
tcp_nodelay on;
|
proxy_set_header Host $host;
|
||||||
keepalive_timeout 65;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
types_hash_max_size 2048;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
}
|
||||||
|
|
||||||
include /etc/nginx/conf.d/*.conf;
|
location /order-service/ {
|
||||||
include /etc/nginx/sites-enabled/*;
|
proxy_pass http://order-service:5002/;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,5 +2,5 @@ FROM python:3.11
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
COPY . .
|
COPY . /app
|
||||||
CMD ["python", "order_service.py"]
|
CMD ["gunicorn", "--bind", "0.0.0.0:5002", "order_service:app"]
|
@ -2,17 +2,17 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
import pika
|
import pika
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Конфигурация RabbitMQ
|
# Конфигурация RabbitMQ
|
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
time.sleep(20)
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
|
||||||
channel = connection.channel()
|
channel = connection.channel()
|
||||||
channel.exchange_declare(exchange='microservices', exchange_type='direct')
|
channel.queue_declare(queue='order_queue', durable=True)
|
||||||
|
|
||||||
|
|
||||||
# CRUD операции для заказов в order_service оставим здесь, чтобы он мог отправлять сообщения
|
|
||||||
|
|
||||||
# CREATE
|
# CREATE
|
||||||
@app.route('/orders', methods=['POST'])
|
@app.route('/orders', methods=['POST'])
|
||||||
def create_order():
|
def create_order():
|
||||||
@ -22,7 +22,7 @@ def create_order():
|
|||||||
# Отправка сообщения о создании заказа
|
# Отправка сообщения о создании заказа
|
||||||
channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order))
|
channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order))
|
||||||
|
|
||||||
return jsonify(order), 201
|
return order, 201
|
||||||
|
|
||||||
|
|
||||||
# READ
|
# READ
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
Flask==3.0.0
|
Flask==3.0.0
|
||||||
pika==1.3.2
|
pika==1.3.2
|
||||||
|
gunicorn==21.2.0
|
@ -2,5 +2,5 @@ FROM python:3.11
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
COPY . .
|
COPY . /app
|
||||||
CMD ["python", "user_service.py"]
|
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "user_service:app"]
|
@ -1,2 +1,3 @@
|
|||||||
Flask==3.0.0
|
Flask==3.0.0
|
||||||
pika==1.3.2
|
pika==1.3.2
|
||||||
|
gunicorn==21.2.0
|
@ -1,16 +1,30 @@
|
|||||||
# user_service.py
|
# user_service.py
|
||||||
|
import time
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
import pika
|
import pika
|
||||||
import json
|
import json
|
||||||
|
import threading
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
users = []
|
||||||
|
|
||||||
|
|
||||||
# Конфигурация RabbitMQ
|
# Конфигурация RabbitMQ
|
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
def connect_to_rabbitmq():
|
||||||
channel = connection.channel()
|
retries = 5
|
||||||
channel.exchange_declare(exchange='microservices', exchange_type='direct')
|
while retries > 0:
|
||||||
|
try:
|
||||||
users = []
|
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
|
||||||
|
channel = connection.channel()
|
||||||
|
channel.queue_declare(queue='order_service', durable=True)
|
||||||
|
#channel.queue_bind(queue='order_service', routing_key='order')
|
||||||
|
channel.basic_consume(queue='order_service', on_message_callback=process_order_message, auto_ack=True)
|
||||||
|
print('Waiting for messages. To exit press CTRL+C')
|
||||||
|
channel.start_consuming()
|
||||||
|
except pika.exceptions.AMQPConnectionError:
|
||||||
|
print("Failed to connect to RabbitMQ. Retrying...")
|
||||||
|
retries -= 1
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
# Обработка сообщений о заказах
|
# Обработка сообщений о заказах
|
||||||
@ -54,23 +68,19 @@ def process_order_message(ch, method, properties, body):
|
|||||||
print(f"User not found for order deletion: {user_id}")
|
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 операции для пользователей
|
# CRUD операции для пользователей
|
||||||
# CREATE
|
# CREATE
|
||||||
@app.route('/users', methods=['POST'])
|
@app.route('/users', methods=['GET'])
|
||||||
def create_user():
|
def show_users():
|
||||||
data = request.get_json()
|
return users
|
||||||
user = {'id': len(users) + 1, 'name': data['name']}
|
|
||||||
users.append(user)
|
|
||||||
return jsonify(user), 201
|
@app.route('/add_users/<string:name>', methods=['POST'])
|
||||||
|
def create_user(name):
|
||||||
|
if request.method == 'POST':
|
||||||
|
user = {'id': len(users) + 1, 'name': name}
|
||||||
|
users.append(user)
|
||||||
|
return jsonify(user)
|
||||||
|
|
||||||
|
|
||||||
# READ
|
# READ
|
||||||
@ -80,19 +90,18 @@ def get_user(user_id):
|
|||||||
if user:
|
if user:
|
||||||
return jsonify(user)
|
return jsonify(user)
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'User not found'}), 404
|
return jsonify({'error': 'User not found'})
|
||||||
|
|
||||||
|
|
||||||
# UPDATE
|
# UPDATE
|
||||||
@app.route('/users/<int:user_id>', methods=['PUT'])
|
@app.route('/users/<int:user_id>_<string:name>', methods=['PUT'])
|
||||||
def update_user(user_id):
|
def update_user(user_id, name):
|
||||||
user = next((user for user in users if user['id'] == user_id), None)
|
user = next((user for user in users if user['id'] == user_id), None)
|
||||||
if user:
|
if user:
|
||||||
data = request.get_json()
|
user['name'] = name
|
||||||
user['name'] = data['name']
|
|
||||||
return jsonify(user)
|
return jsonify(user)
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'User not found'}), 404
|
return jsonify({'error': 'User not found'})
|
||||||
|
|
||||||
|
|
||||||
# DELETE
|
# DELETE
|
||||||
@ -104,4 +113,7 @@ def delete_user(user_id):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
# Запускаем обработчик RabbitMQ в отдельном потоке
|
||||||
|
rabbitmq_thread = threading.Thread(target=connect_to_rabbitmq())
|
||||||
|
rabbitmq_thread.start()
|
||||||
app.run(port=5001)
|
app.run(port=5001)
|
||||||
|
Loading…
Reference in New Issue
Block a user