Python-RabbitMQ消息队列实现rpc

Posted Sch01aR#

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-RabbitMQ消息队列实现rpc相关的知识,希望对你有一定的参考价值。

客户端通过发送命令来调用服务端的某些服务,服务端把结果再返回给客户端

这样使得RabbitMQ的消息发送端和接收端都能发送消息

返回结果的时候需要指定另一个队列

服务器端

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import pika
import os

connection = pika.BlockingConnection(pika.ConnectionParameters(host=\'127.0.0.1\'))
channel = connection.channel()

channel.queue_declare(queue=\'rpc_q\')


def cmd(n):
    cmd_result = os.popen(n)
    cmd_result = cmd_result.read()
    return cmd_result


def on_request(ch, method, props, body):
    body = body.decode()
    print(\'执行命令:\', body)
    response = cmd(body)
    print(response)

    ch.basic_publish(exchange=\'\',
                     routing_key=props.reply_to,  # 把消息发送到用来返回消息的queue
                     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(on_request, queue=\'rpc_q\')

print(\'等待请求\')
channel.start_consuming()

 客户端

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import pika
import uuid
import time


class RpcClient(object):
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=\'127.0.0.1\'))

        self.channel = self.connection.channel()

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue  # 生成随机的queue

        self.channel.basic_consume(self.on_response,  # 一收到消息就调用op_response方法
                                   no_ack=True,
                                   queue=self.callback_queue,
                                   )

    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:  # 判断服务端发送的uuid和客户端发送的uuid是否匹配
            self.response = body

    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange=\'\',
                                   routing_key=\'rpc_q\',
                                   properties=pika.BasicProperties(
                                       reply_to=self.callback_queue,  # 把返回的消息发送到用来返回结果的queue
                                       correlation_id=self.corr_id,
                                   ),
                                   body=n,
                                   )
        while self.response is None:
            self.connection.process_data_events()  # 相当于非阻塞的start_consuming()
            print(\'当前没有消息\')
            time.sleep(3)
        return self.response

while True:
    cmd = input(\'>>>:\').strip()
    print(\'执行命令:\', cmd)
    rpc = RpcClient()
    response = rpc.call(cmd)
    print(response.decode())

 开启一个客户端和一个服务端

 执行结果:

 服务器端

客户端

 

以上是关于Python-RabbitMQ消息队列实现rpc的主要内容,如果未能解决你的问题,请参考以下文章

python-RabbitMQ

Python-RabbitMQ direct广播模式

RPC和消息队列

RabbitMQ RPC 关闭最终消息的响应队列

rabbitmq学习:利用rabbitmq实现远程rpc调用

rocketmq 消息队列 初步了解