设为首页 加入收藏

TOP

python之RabbitMQ(一)
2017-09-30 17:11:30 】 浏览:5719
Tags:python RabbitMQ
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
 
安装RabbitMQ:
安装配置epel源:(详见http://www.cnblogs.com/ernest-zhang/p/5714434.html)
安装erlang:
yum -y install erlang
注:安装erlang的时候碰到
    Error: Package: erlang-erts-R14B-04.3.el6.i686 (epel)
           Requires: libz.so.1(ZLIB_1.2.2)
[root@localhost ~]# yum whatprovides libz.so.1
Loaded plugins: rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
zlib-1.2.3-25.el6.i686 : The zlib compression and decompression library #提供压缩与解压缩库
Repo        : local
Matched from:
Other       : libz.so.1
检查发现应该是zlib的版本太老了,从网上下载最新的zlib-1.2.8-10.fc24.i686,然后使用RPM安装后解决。
下载地址:http://www.zlib.net/  #zlib官网
http://rpmfind.net/linux/rpm2html/search.php?query=zlib    #zlib下载网站
安装rabbitMQ:
yum -y install rabbitmq-server
service rabbitmq-server start/stop 启动和停止rabbitmq
安装API,然后可以基于API操作rabbitmq
pip install pika
or
easy_install pika
or
源码
 
https://pypi.python.org/pypi/pika
Python 操作RabbitMQ
发布端:
import pika
connection=pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.74'))    #服务器地址
channel=connection.channel()
channel.queue_declare(queue='Hi')   #如果有队列,略过;如果没有,创建队列
channel.basic_publish(exchange='',routing_key='cc',body='hello!world!!!')
print("[x] sent 'hello,world!'")
connection.close()

接收端:

import pika
#创建一个连接对象,绑定rabbitmq的IP
connection=pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.74'))
#创建一个频道对象
channel=connection.channel()
#频道中声明指定queue,如果MQ中没有指定queue就创建,如果有,则略过
channel.queue_declare(queue='Hi')
#定义回调函数
def callback(ch,method,properties,body):
    print('[x] Recieved %r'%body)
    # channel.close()
#no_ack=Fales:表示消费完以后不主动把状态通知rabbitmq,callback:回调函数,queue:指定队列
channel.basic_consume(callback,queue='Hi',no_ack=True)
# channel.basic_consume(callback,queue='cc')
print('[*] Waiting for msg')
channel.start_consuming()

1、acknowledgment 消息不丢失

no-ack = False,如果消费者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。

  • 回调函数中的ch.basic_ack(delivery_tag=method.delivery_tag)
  • basic_comsume中的no_ack=False
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.74'))
channel = connection.channel()
channel.queue_declare(queue='Hi')
# 定义回调函数
def callback(ch, method, properties, body):
    print('[x] Recieved %r' % body)
    # channel.close()
    ch.basic_ack(delivery_tag=method.delivery_tag)
# no_ack=Fales:表示消费完以后不主动把状态通知rabbitmq
channel.basic_consume(callback, queue='Hi',
                      no_ack=False)  
print('[*] Waiting for msg')
channel.start_consuming()
View Code

durable 消息不丢失

消息生产者端发送消息时挂掉了,消费者接消息时挂掉了,以下方法会让RabbitMQ重新将该消息添加到队列中:

  • 回调函数中的ch.basic_ack(delivery_tag=method.delivery_tag),消费端需要做的
  • basic_comsume中的no_ack=False,消费端需要做的
  • 发布消息端的basic_publish添加参数properties=pika.BasicProperties(delivery_mode=2),生产者端需要做的
import pika
connection = pika.Blockin
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python编码格式的指定方式 下一篇Python操作MySQL之SQLAlchemy

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目