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

[经验分享] RabbitMQ的Python操作

[复制链接]

尚未签到

发表于 2017-7-2 15:21:53 | 显示全部楼层 |阅读模式
  RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
  MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
  一、RabbitMQ安装
  安装配置epel源
$ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
安装erlang
$ yum -y install erlang
安装RabbitMQ
$ yum -y install rabbitmq-server
注意:service rabbitmq-server start/stop
  安装API、
  pip install pika or easy_install pika or源码
  
https://pypi.python.org/pypi/pika
使用API操作RabbitMQ

  基于Queue实现生产者消费者模型
  #!/usr/bin/env python
# -*- coding:utf-8 -*-
import Queue
import threading
  message = Queue.Queue(10)
  def producer(i):
while True:
message.put(i)
  def consumer(i):
while True:
msg = message.get()
  for i in range(12):
t = threading.Thread(target=producer, args=(i,))
t.start()
  for i in range(10):
t = threading.Thread(target=consumer, args=(i,))
t.start()
  对于RabbitMQ来说,生产和消费不再针对内存里的一个Queue对象,而是某台服务器上的RabbitMQ Server实现的消息队列。
#!/usr/bin/env python
import pika

# ######################### 生产者 #########################

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

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
  #!/usr/bin/env python
import pika

# ########################## 消费者 ##########################

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

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
print(" [x] Received %r" % body)

channel.basic_consume(callback,
queue='hello',
no_ack=True)

print('
  • Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
      
    1、acknowledgment 消息不丢失
      no-ack = False,如果消费者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。
      import pika
      connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='10.211.55.4'))
    channel = connection.channel()
      channel.queue_declare(queue='hello')
      def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    import time
    time.sleep(10)
    print 'ok'
    ch.basic_ack(delivery_tag = method.delivery_tag)
      channel.basic_consume(callback,
    queue='hello',
    no_ack=False)
      print('
  • Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
      2、durable 消息不丢失
      #!/usr/bin/env python
    import pika
      connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
    channel = connection.channel()
      # make message persistent
    channel.queue_declare(queue='hello', durable=True)
      channel.basic_publish(exchange='',
    routing_key='hello',
    body='Hello World!',
    properties=pika.BasicProperties(
    delivery_mode=2, # make message persistent
    ))
    print(" [x] Sent 'Hello World!'")
    connection.close()
      
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import pika
      connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
    channel = connection.channel()
      # make message persistent
    channel.queue_declare(queue='hello', durable=True)
      
    def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    import time
    time.sleep(10)
    print 'ok'
    ch.basic_ack(delivery_tag = method.delivery_tag)
      channel.basic_consume(callback,
    queue='hello',
    no_ack=False)
      print('
  • Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
      3、消息获取顺序
      默认消息队列里的数据是按照顺序被消费者拿走,例如:消费者1 去队列中获取 奇数 序列的任务,消费者1去队列中获取 偶数 序列的任务。
      channel.basic_qos(prefetch_count=1) 表示谁来谁取,不再按照奇偶数排列
      
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import pika
      connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
    channel = connection.channel()
      # make message persistent
    channel.queue_declare(queue='hello')
      
    def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    import time
    time.sleep(10)
    print 'ok'
    ch.basic_ack(delivery_tag = method.delivery_tag)
      channel.basic_qos(prefetch_count=1)
      channel.basic_consume(callback,
    queue='hello',
    no_ack=False)
      print('
  • Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
      
    4、发布订阅
      
    发布订阅和简单的消息队列区别在于,发布订阅会将消息发送给所有的订阅者,而消息队列中的数据被消费一次便消失。所以,RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者发布消息时,会将消息放置在所有相关队列中。
      exchange type = fanout
      
    #!/usr/bin/env python
    import pika
    import sys
      connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
      channel.exchange_declare(exchange='logs',
    type='fanout')
      message = ' '.join(sys.argv[1:]) or "info: Hello World!"
    channel.basic_publish(exchange='logs',
    routing_key='',
    body=message)
    print(" [x] Sent %r" % message)
    connection.close()
      
    #!/usr/bin/env python
    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)
    queue_name = result.method.queue
      channel.queue_bind(exchange='logs',
    queue=queue_name)
      print('
  • 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()
      
    5、关键字发送
      exchange type = direct
      之前事例,发送消息时明确指定某个队列并向其中发送消息,RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。
      
    #!/usr/bin/env python
    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()
      
    #!/usr/bin/env python
    import pika
    import sys
      connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel()
      channel.exchange_declare(exchange='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()
      6、模糊匹配
      exchange type = topic
      在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。
      # 表示可以匹配 0 个 或 多个 单词
    * 表示只能匹配 一个 单词
    1
    2
    3
    发送者路由值 队列中
    old.boy.python old.* -- 不匹配
    old.boy.python old.# -- 匹配
      
    #!/usr/bin/env python
    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()
      
    #!/usr/bin/env python
    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()

  • 运维网声明 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-390426-1-1.html 上篇帖子: Py西游攻关之RabbitMQ 下篇帖子: 消息队列系统
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

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

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

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

    扫描微信二维码查看详情

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


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


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


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



    合作伙伴: 青云cloud

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