设为首页 收藏本站
查看: 1088|回复: 0

[经验分享] python-rabbitmq

[复制链接]

尚未签到

发表于 2017-7-2 15:30:23 | 显示全部楼层 |阅读模式
  简单producer:



import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters('localhost')
)
channel = connection.channel()
#declar
channel.queue_declare(queue='hello')  #声明一个queue

channel.basic_publish(exchange='',
routing_key='hello',   #routing_key就是声明的queue的名字
body='Hello World!')   #消息内容
print("[x] Send 'hello World!'")
connection.close()

  简单consumer:



import pika
import time
connection = pika.BlockingConnection(
pika.ConnectionParameters('localhost')
)
channel = connection.channel()
'''
#如果确认这个queue已经存在, 可以不写下面语句,但是这里不声明,如果消费者先执行,就会出错。
'''
channel.queue_declare(queue='hello')
def callback(ch,method,properties,body):
'''
:param ch: 管道的内存对象
:param method:
:param properties:
:param body: 消息内容
:return:
'''
print('---->',ch,method,properties,body)
time.sleep(30)   
print("[x] Recevied %r" % body)

channel.basic_consume(callback,  #如果收到消息,就调用callback函数来处理消息
queue='hello', #从哪个队列收消息,收到消息执行callback
#no_ack=True   #True代表不确认,无论callback消息处理失败还是完成,都不会和生产端确认,默认是Flase,代表确认
)
print('
  • Waiting for messages.To exit press CTRL+C')
    channel.start_consuming()  #start 执行命令

      队列以及消息持久化:



    #producer
    import pika
    connection = pika.BlockingConnection(
    pika.ConnectionParameters('localhost')
    )
    channel = connection.channel()
    #declar
    channel.queue_declare(queue='hello3',durable=True)  #声明一个queue,durable参数设定持久化,注意持久化的只是队列,而不是消息内容
    channel.basic_publish(
    exchange='',
    routing_key='hello3',  #routing_key就是声明的queue的名字
    body='Hello World!', #消息内容
    properties=pika.BasicProperties(delivery_mode=2)   #消息持久化参数
    )
    print("[x] Send 'hello World!'")
    connection.close()

    #consumer
    import pika
    import time
    connection = pika.BlockingConnection(
    pika.ConnectionParameters('localhost')
    )
    channel = connection.channel()
    '''
    #如果确认这个queue已经存在, 可以不写下面语句,但是这里不声明,如果消费者先执行,就会出错。
    '''
    channel.queue_declare(queue='hello3',durable=True) #durable参数要与生产端保持一致
    #channel.queue_declare(queue='hello2')
    def callback(ch,method,properties,body):
    '''
    :param ch: 管道的内存对象
    :param method:
    :param properties:
    :param body: 消息内容
    :return:
    '''
    print('---->',ch,method,properties,body)
    time.sleep(5)
    print("[x] Recevied %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)  #配合no_ack参数,处理完成,返回生产端确认

    channel.basic_qos(prefetch_count=1) #表示处理完一条再给我发消息
    channel.basic_consume(callback,  #如果收到消息,就调用callback函数来处理消息
    queue='hello3', #从哪个队列收消息,收到消息执行callback
    #no_ack=True   #True代表不确认,无论callback消息处理失败还是完成,都不会和生产端确认,默认是Flase,代表确认
    )
    print('
  • Waiting for messages.To exit press CTRL+C')
    channel.start_consuming()  #start 执行命令

      rabbitMQ广播之:fanout (订阅发布)



    #producer:
    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host = 'localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='logs',
    type='fanout')  #fanout表示广播
    #message = ' '.join(sys.argv[1:]) or "info: Hello World!"
    message = "info: Hello World!"
    channel.basic_publish(exchange='logs',
    routing_key='',
    body=message)
    print("[x] Sent %r" % message)
    connection.close()

    #consumer:
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='logs',
    type='fanout'
    )
    result = channel.queue_declare(exclusive=True)   #exclusive排他的,唯一的,随机分配一个唯一的名字,消费者断开后自动删除
    queue_name = result.method.queue
    channel.queue_bind(exchange='logs',
    queue=queue_name)
    print('[x] Waiting for logs. To exit press CTRL+C')
    def callback(ch, method, properties, body):
    print("[x] %r" % body)
    channel.basic_consume(callback,
    queue=queue_name,
    no_ack=True)
    channel.start_consuming()

      广播之direct:



    #producer
    #!/usr/bin/python3
    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
    channel.exchange_declare('direct_logs',
    type='direct')
    severity = sys.argv[1] if len(sys.argv)>1 else 'info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='direct_logs',
    routing_key=severity,
    body=message)
    print("[x] Sent %r:%r" % (severity, message))
    connection.close()

    #consumer
    #!/usr/bin/python3
    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='direct_logs',
    type='direct')
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    severities = sys.argv[1:]
    if not severities:
    sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
    sys.exit(1)
    for severity in severities:
    channel.queue_bind(exchange='direct_logs',
    queue=queue_name,
    routing_key=severity)
    print('
  • Waiting for logs. To exit press CTRL+C')
    def callback(ch, method, properties, body):
    print("[x] %r:%r" % (method.routing_key,body))
    channel.basic_consume(callback,
    queue=queue_name,
    no_ack=True)
    channel.start_consuming()

      topic消息过滤广播:



    #producer
    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='topic_logs',
    type='topic')
    routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='topic_logs',
    routing_key=routing_key,
    body=message)
    print(" [x] Sent %r:%r" % (routing_key, message))
    connection.close()
    #consumer
    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='topic_logs',
    type='topic')
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    binding_keys = sys.argv[1:]
    if not binding_keys:
    sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
    sys.exit(1)
    for binding_key in binding_keys:
    channel.queue_bind(exchange='topic_logs',
    queue=queue_name,
    routing_key=binding_key)

    print('
  • Waiting for logs. To exit press CTRL+C')
    def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
    channel.basic_consume(callback,
    queue=queue_name,
    no_ack=True)
    channel.start_consuming()

      rpc:



    #sev:
    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,
    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()

    #cli:
    import pika
    import uuid
    import time

    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: #当接收到对端返回时先判断correlation_id是否与返回的id相同,
    # 确保队列的一致性和唯一性
    self.response = body
    def call(self, n):
    self.response = None
    self.corr_id = str(uuid.uuid4())   #产生一个随机数,赋给correlation_id传给对端,对端返回时再把这个随机数返回
    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() #非阻塞版的start_consumer()
    #print("no msg...")
    #time.sleep(0.5)
    return int(self.response)

    fibonacci_rpc = FibonacciRpcClient()
    print(" [x] Requesting fib(30)")
    while True:
    num = input("input>:").strip()
    if num.isdigit() and int(num) > 0:
    response = fibonacci_rpc.call(str(num))
    print(" [.] Got %r" % response)
    else:
    print("请输入大于0的整数")

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390431-1-1.html 上篇帖子: Leetcode27--->Remove Element(移除数组中给定元素) 下篇帖子: RabbitMQ简述
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表