我正在做一个应用程序,后面的部分是flask,前面的部分是angular,具有微服务架构。对于部署,我使用docker,现在的问题是在服务之间进行通信。我读到过最好的选择是rabbit mqtt,但我还没有找到任何教程。
我几乎没有时间,因为这是为了完成我的学位,所以我需要一个教程,让我可以快速创建服务之间的通信。
flask是不稳定的,我使用管理器来创建API-CRUD。
提前感谢
发布于 2019-05-21 11:08:35
下面是我是如何做到的:
from flask import Flask
import pika
import uuid
import threading
app = Flask(__name__)
queue = {}
class FibonacciRpcClient(object):
def __init__(self):
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host='rabbit'))
self.channel = self.connection.channel()
result = self.channel.queue_declare('', exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(
queue=self.callback_queue,
on_message_callback=self.on_response,
auto_ack=True)
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
def call(self, n):
self.response = None
self.corr_id = str(uuid.uuid4())
queue[self.corr_id] = None
self.channel.basic_publish(
exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to=self.callback_queue,
correlation_id=self.corr_id,
),
body=str(n))
while self.response is None:
self.connection.process_data_events()
queue[self.corr_id] = self.response
print(self.response)
return int(self.response)
@app.route("/calculate/<payload>")
def calculate(payload):
n = int(payload)
fibonacci_rpc = FibonacciRpcClient()
threading.Thread(target=fibonacci_rpc.call, args=(n,)).start()
return "sent " + payload
@app.route("/results")
def send_results():
return str(queue.items())import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='rpc_queue')
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
def on_request(ch, method, props, body):
n = int(body)
print(" [.] fib(%s)" % n)
response = fib(n)
print(" [.] calculated (%s)" % response)
ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id=props.correlation_id),
body=str(response))
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='rpc_queue', on_message_callback=on_request)
print(" [x] Awaiting RPC requests")
channel.start_consuming()以上两个是基于RPC的RabbitMQ教程。
FROM python:3
RUN mkdir code
ADD flask_server.py requirements.txt /code/
WORKDIR /code
RUN pip install -r requirements.txt
ENV FLASK_APP flask_server.py
EXPOSE 5000
CMD ["flask", "run", "-h", "0.0.0.0"]services:
web:
build: .
ports:
- "5000:5000"
links: rabbit
volumes:
- .:/code
rabbit:
hostname: rabbit
image: rabbitmq:latest
ports:
- "5672:5672"运行docker-compose up,Flask服务器应该开始与RabbitMQ服务器通信。
发布于 2020-04-30 21:48:08
有许多方法可以编写RabbitMQ服务器、worker和worker文件。
第一个答案显示了它们的很好的例子。
我要强调的是,当worker (在您的例子中是web服务)试图通过访问RabbitMQ服务器时,web服务器可能还没有准备好。
对于该,我建议将 docker-compose.yml 文件写成这样的:
version: "3"
services:
web:
build: .
ports:
- "5000:5000"
restart: on-failure
depends_on:
- rabbitmq
volumes:
- .:/code
rabbit:
image: rabbitmq:latest
expose:
- 5672
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "5672" ]
interval: 3s
timeout: 10s
retries: 3那么,我在这里做了什么?
1)我在web服务中添加了depends_on和restart属性,在兔服务中添加了healthcheck属性。
现在,web服务将自行重新启动,直到rabbit服务变得健康。
2)在兔服务中,我使用了expose属性而不是ports,因为在您的示例中,5672端口需要在容器之间共享,而不需要向主机公开。
从Expose文档中:
在不将端口发布到主机的情况下公开端口-只有链接的服务才能访问这些端口。只能指定内部端口。
3)我删除了links属性,因为(取自here):
启用服务通信不需要
链接-默认情况下,任何服务都可以访问该服务名称下的任何其他服务。
发布于 2020-10-14 13:47:37
https://youtu.be/ZxVpsClqjdw这个视频用代码解释了一切
,并有一个docker-compose as
version: '3'
services:
redis:
image: redis:latest
hostname: redis
rabbit:
hostname: rabbit
image: rabbitmq:latest
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=mypass
web:
build:
context: .
dockerfile: Dockerfile
hostname: web
command: ./scripts/run_web.sh
volumes:
- .:/app
ports:
- "5000:5000"
links:
- rabbit
- redis
worker:
build:
context: .
dockerfile: Dockerfile
command: ./scripts/run_celery.sh
volumes:
- .:/app
links:
- rabbit
- redis
depends_on:
- rabbit并使该连接使用
BROKER_URL = 'amqp://admin:mypass@rabbit//'
CELERY = Celery('tasks',backend=REDIS_URL,broker=BROKER_URL)有关更多解释,请访问https://medium.com/swlh/dockerized-flask-celery-rabbitmq-redis-application-f317825a03b
https://stackoverflow.com/questions/56110148
复制相似问题