web-services - 如何使用 FastAPI 作为 RabbitMQ (RPC) 的消费者

标签 web-services flask rabbitmq fastapi pika

示例 here展示了如何使用远程过程调用 (RPC) 在 python 中创建客户端和服务器。
但我无法想象 FastAPI 服务如何成为使用 pika for RabbitMQ 来消费来自 RCP 客户端的请求的服务器。
将通过显式调用它们来请求任何 Web 服务,但是,我无法想象如何将 RabbitMQ 使用者集成到 Web 服务中。
另一方面,对于客户端来说,这样做很容易,通过显式调用 Web 服务,您可以发布对队列的请求,see this example
请问有什么帮助吗?还是一个好的开始?

最佳答案

您可以使用 aio_pikaRPC模式并执行以下操作:
服务 1(消耗)
循环消费:

# app/__init__.py

from fastapi import FastAPI
from app.rpc import consume

app = FastAPI()

...

@app.on_event('startup')
def startup():
    loop = asyncio.get_event_loop()
    # use the same loop to consume
    asyncio.ensure_future(consume(loop))

...

创建要从另一个服务调用的连接、 channel 和注册远程方法:
# app/rpc.py

from aio_pika import connect_robust
from aio_pika.patterns import RPC

from app.config import config

__all__ = [
    'consume'
]


def remote_method():
    # DO SOMETHING
    # Move this method along with others to another place e.g. app/rpc_methods
    # I put it here for simplicity
    return 'It works!'

async def consume(loop):
    connection = await connect_robust(config.AMQP_URI, loop=loop)
    channel = await connection.channel()
    rpc = await RPC.create(channel)

    # Register your remote method
    await rpc.register('remote_method', remote_method, auto_delete=True)
    return connection

这就是您需要使用和响应的全部内容,现在让我们看看调用此远程方法的第二个服务。
服务 2(调用远程方法)
让我们先创建 RPC 中间件,以便轻松管理和访问 RPC 对象,以便从 API 函数调用我们的远程方法:
# app/utils/rpc_middleware.py

import asyncio

from fastapi import Request, Response

from aio_pika import connect_robust
from aio_pika.patterns import RPC

from app.config import config

__all__ = [
    'get_rpc',
    'rpc_middleware'
]


async def rpc_middleware(request: Request, call_next):
    response = Response("Internal server error", status_code=500)
    try:
        # You can also pass a loop as an argument. Keep it here now for simplicity
        loop = asyncio.get_event_loop()
        connection = await connect_robust(config.AMQP_URI, loop=loop)
        channel = await connection.channel()
        request.state.rpc = await RPC.create(channel)
        response = await call_next(request)
    finally:

        # UPD: just thought that we probably want to keep queue and don't
        # recreate it for each request so we can remove this line and move
        # connection, channel and rpc initialisation out from middleware 
        # and do it once on app start

        # Also based of this: https://github.com/encode/starlette/issues/1029
        # it's better to create ASGI middleware instead of HTTP
        await request.state.rpc.close()
    return response


# Dependency to use rpc inside routes functions
def get_rpc(request: Request):
    rpc = request.state.rpc
    return rpc

应用RPC中间件:
# app/__init__.py

from app.utils import rpc_middleware

...

app.middleware('http')(rpc_middleware)

...

通过 API 函数中的依赖项使用 RPC 对象:
# app/api/whatever.py

from aio_pika.patterns import RPC

from app.utils import get_rpc

...

@router.get('/rpc')
async def rpc_test(rpc: RPC = Depends(get_rpc)):
    response = await rpc.proxy.remote_method()
    ...

添加一些日志记录以跟踪两个服务中发生的情况。此外,您还可以将两个服务的 RPC 逻辑合二为一,以便能够从同一服务中使用和调用远程方法。
希望它有助于获得基本的想法。

关于web-services - 如何使用 FastAPI 作为 RabbitMQ (RPC) 的消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65586853/

相关文章:

java - 从@WebService获取对spring bean的引用

java - 如何使用 JUnit 测试 Java Web 服务?

python - Flask:如何读取流输入

java - 如何向 RabbitMQ 进行身份验证?

java - RabbitMQ (Java) 多消费者性能问题

java - 无法在 Weblogic 12c (12.2.1) 中部署 CXF3.x JAX-RS 服务

PHP json_encode - 无效的 JSON

python - 使用 threaded=True 同时处理 Flask 请求

python - 使用 flask "ImportError: No module named flask"执行 hello world

jms - RabbitMq : Create queue dynamically