zoukankan      html  css  js  c++  java
  • RabbitMQ双向发送(接收端有返回RPC模式)

    remote procedure call

    • 服务端
    import pika
    import time
    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)
    
        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_queue')
    
    print(" [x] Awaiting RPC requests")
    channel.start_consuming()
    
    • 客户端
    import pika
    import uuid
    
    class FibonacciRpcClient(object):
        def __init__(self):
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                    host='localhost'))
    
            self.channel = self.connection.channel()
    
            result = self.channel.queue_declare(exclusive=True)
            self.callback_queue = result.method.queue
    
            self.channel.basic_consume(self.on_response, no_ack=True,
                                      queue=self.callback_queue)
    
        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())
            self.channel.basic_publish(exchange='',
                                      routing_key='rpc_queue',
                                      properties=pika.BasicProperties(
                                            reply_to = self.callback_queue,#标识返回的queue
                                            correlation_id = self.corr_id,#标识命令
                                            ),
                                      body=str(n))
            while self.response is None:
                self.connection.process_data_events()
            return int(self.response)
    
    fibonacci_rpc = FibonacciRpcClient()
    
    print(" [x] Requesting fib(30)")
    response = fibonacci_rpc.call(30)
    print(" [.] Got %r" % response)
    
  • 相关阅读:
    File 类
    RocketMQ4.X 集群
    Java 的基本网络支持
    SpringBoot2.X 整合 RocketMQ4.X
    RocketMQ系列:rocketmq运维控制台使用详解(全网独家)
    RocketMQ系列:docker搭建rocketmq单机环境
    RocketMQ系列:rocketmq运维控制台搭建
    RocketMQ系列:单机快速搭建单broker环境
    Jenkins:Git拉取代码版本不对
    pip3:no acceptable C compiler found in $PATH
  • 原文地址:https://www.cnblogs.com/limich/p/7477206.html
Copyright © 2011-2022 走看看