zoukankan      html  css  js  c++  java
  • 构造RPC远程调用

    rpc 客户端 发送一个数字 给 服务端

    rpc 服务端 获取该数子后 + 5  返回给客户端

    相关链接:  https://www.rabbitmq.com/tutorials/tutorial-six-python.html

    rpm_server.py

    #!/usr/bin/env python
    import pika
    
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='localhost'))
    
    channel = connection.channel()
    
    channel.queue_declare(queue='rpc_queue')
    def fib(n):
            return int(n) + 5
    
    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,
                         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_client.py

    #!/usr/bin/env python
    import pika
    import uuid
    
    class FibonacciRpcClient(object):
    
        def __init__(self):
            self.connection = pika.BlockingConnection(
                pika.ConnectionParameters(host='192.168.0.19'))
    
            self.channel = self.connection.channel()
    
            result = self.channel.queue_declare(queue='', 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())
            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()
            return self.response
    
    
    fibonacci_rpc = FibonacciRpcClient()
    
    print(" [x] Requesting fib(30)")
    response = fibonacci_rpc.call(30)
    print(" [.] Got %r" % response)
  • 相关阅读:
    一些Vim使用的小技巧
    virtualbox centos安装增强工具和Centos与VirtualBox共享文件夹设置
    (转) centos7 RPM包之rpm命令
    (转)Navicat_12安装与破解,亲测可用!!!
    (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去
    (转)react 项目构建
    (转)python3:类方法,静态方法和实例方法以及应用场景
    (转)SQLAlchemy入门和进阶
    (转)面向对象(深入)|python描述器详解
    (转)CentOS 7.6 上编译安装httpd 2.4.38
  • 原文地址:https://www.cnblogs.com/jkklearn/p/13792466.html
Copyright © 2011-2022 走看看