Rabbitmq -- rpc

一、前言

  MQ本身是基于异步的消息处理,前面的示例中所有的生产者(P)将消息发送到RabbitMQ后不会知道消费者(C)处理成功或者失败(甚至连有没有消费者来处理这条消息都不知道)。
但实际的应用场景中,我们很可能需要一些同步处理,需要同步等待服务端将我的消息处理完成后再进行下一步处理。这相当于RPC(Remote Procedure Call,远程过程调用)。在RabbitMQ中也支持RPC。

  

  

RabbitMQ中实现RPC的机制是:

  • 客户端发送请求(消息)时,在消息的属性(MessageProperties,在AMQP协议中定义了14中properties,这些属性会随着消息一起发送)中设置两个值replyTo(一个Queue名称,用于告诉服务器处理完成后将通知我的消息发送到这个Queue中)和correlationId(此次请求的标识号,服务器处理完成后需要将此属性返还,客户端将根据这个id了解哪条请求被成功执行了或执行失败)
  • 服务器端收到消息并处理
  • 服务器端处理完消息后,将生成一条应答消息到replyTo指定的Queue,同时带上correlationId属性
  • 客户端之前已订阅replyTo指定的Queue,从中收到服务器的应答消息后,根据其中的correlationId属性分析哪条请求被执行了,根据执行结果进行后续业务处理

二、事例代码

  求斐波那契数列

  client: 

import pika
import uuid


class FibonacciRpcClient(object):

    def __init__(self):
        # 初始化时创建连接
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
        # 初始化建立管道
        self.channel = self.connection.channel()
        # 这里是客户端接收服务端的返回
        # 在此要声明一个queue,并且名称随机生成
        result = self.channel.queue_declare(exclusive=True)
        # 该callback_queue 指定了服务端返回时,用哪一个queue
        self.callback_queue = result.method.queue
        # 客户端处理服务端返回的消息,指定获取信息的队列queue 和 回调函数
        self.channel.basic_consume(self.on_response,
                                   no_ack=True,
                                   queue=self.callback_queue)

    # 客户端的回调函数,用来处理服务端返回的数据
    def on_response(self, ch, method, props, body):
        # 客户端回调函数对服务端返回数据的处理
        # 此correlation_id 为服务端返回的id, 用来确保处理的消息为同一条
        if self.corr_id == props.correlation_id:
            # 将返回的信息body 给 response
            self.response = body

    # call函数就是client 最初发送消息的地方
    def call(self, n):
        self.response = None
        # corr_id 其实有客户端最初生成,由最初rpc_queue发送给服务端
        # 服务端接收后会在将这个id 返回,就是上面的correlation_id
        # 如果 corr_id 和 correlation_id 一致,则可确保是同一个消息
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                       # reply_to指定了服务端返回时使用的queue
                                       reply_to=self.callback_queue,
                                       correlation_id=self.corr_id,
                                   ),
                                   body=str(n))
        
        # 客户端发送和接收消息的队列是不一样的
        # 所以需要对接收消息的队列不断查询
        # 如果有消息了就接收
        while self.response is None:
            # 当这里使用 channel.start_consumer() 为阻塞状态
            # 使用connection.process_data_events() 为非阻塞
            self.connection.process_data_events()
        return int(self.response)

if __name__ == '__main__':

    fibonacci_rpc = FibonacciRpcClient()

    while True:
        num = int(input('>>:').strip())
        response = fibonacci_rpc.call(num)
        print(" [.] Got %r" % response)

  server: 

# -*- coding: UTF-8 -*-

import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))

channel = connection.channel()

# 这个是客户端最初发送消息时使用的队列 rpc_queue
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)

    ch.basic_publish(exchange='',
                     # 这个routing_key 定义了返回的队列是哪一个
                     # 就是客户端定义的 reply_to
                     routing_key=props.reply_to,
                     # correlation_id 就是客户端生成的corr_id
                     properties=pika.BasicProperties(
                         correlation_id=props.correlation_id),
                     body=str(response))
    # 消息处理完毕后,主动告知rabbitmq
    ch.basic_ack(delivery_tag=method.delivery_tag)


# channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request,
                      queue='rpc_queue')

print(" [x] Awaiting RPC requests")
channel.start_consuming()

 

posted @ 2018-01-08 14:54  Bigberg  阅读(357)  评论(0编辑  收藏  举报