python-RabbitMQ基础篇
一、RabbitMQ简单介绍
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
RabbitMQ安装:
官网地址:http://www.rabbitmq.com/install-debian.html
方法一:
echo 'deb http://www.rabbitmq.com/debian/ testing main' |
sudo tee /etc/apt/sources.list.d/rabbitmq.list
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc |
sudo apt-key add -
#Run the following command to update the package list:
#更新程序包 sudo apt-get update
Install rabbitmq-server package:
sudo apt-get install rabbitmq-server
启动RabbitMQ服务:
#/etc/init.d/rabbitmq-server
linux安装:
安装配置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
安装过程注意问题:
安装erlang $ yum -y install erlang 系统报错:Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 解决方法: 打开/etc/yum.repos.d/epel.repo 找到: 1.[epel]
2.name=Extra Packages for Enterprise Linux 6 - $basearch3.#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch4.mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch 修改为; 1.[epel]
2.name=Extra Packages for Enterprise Linux 6 - $basearch3.baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch4.#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
参考地址:http://xiedexu.cn/error-cannot-retrieve-metalink-repository-epel-please-verify-path-try.htm
安装API:
pip install pika or easy_install pika or 源码 https://pypi.python.org/pypi/pika
使用API操作RabbitMQ
基于Queue实现生产者消费者模型
import queue,time
import threading
#先进先出
q = queue.Queue(20)
#生产者
def productor(arg):
"""
买票
:param arg:
:return:
"""
q.put(str(arg)+"-买票")
for i in range(30):
t = threading.Thread(target=productor,args=(i,)) #创建买票线程生产者,并将动态参数i传递给productor(arg)
t.start() #执行买票线程
# if i == 30:
# break
#消费者
def consumer(arg):
"""
服务器后台,
:param arg:
:return:
"""
while True:
print(arg,q.get()) #获取队列中存在元素
time.sleep(1)
sk = threading.BoundedSemaphore(5)
for j in range(5):
t = threading.Thread(target=consumer,args=(j,))#创建买票线程生产者,并将动态参数j传递给consumer(arg)
t.start() #执行c线程
1、RabbitMQ简单操作
对于RabbitMQ来说,生产和消费不再针对内存里的一个Queue对象,而是某台服务器上的RabbitMQ Server实现的消息队列。
在服务器端启动RabbitMQ程序:
生产者:
import pika
# ######################### 生产者 #########################
print('--------------------生产者------------------------')
#链接rabbit服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.103'))
channel = connection.channel() #创建频道
channel.queue_declare(queue='nihao001')#创建队列名为xiaoluo001
#向xiaoluo001队列插入数字,routing_key=‘xiaoluo001’为队列名,body后则是插入的数据
channel.basic_publish(exchange='',
routing_key='nihao001',
body='Hello World!123456790')
print(" [x] Sent 'Hello World!'")
#缓冲区已经flush且消息已经 确认发送到rabbitMq,最后关闭连接
connection.close()
消费者:
import pika
# ########################## 消费者 ##########################
print("---------------消费者----------------------")
#链接rabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.103'))
#创建链接频道
channel = connection.channel()
#创建队列名为:xiaoluo001
channel.queue_declare(queue='xiaoluo001')
#callback函数:接收服务器端发送消息,他会被pika调用
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
#从队列中取数据,callback回调函数,如果拿到数据则执行callback函数
channel.basic_consume(callback,
queue='xiaoluo001',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
#循环等待数据处理和callback函数处理的数据
channel.start_consuming()
2、acknowledgment消息不丢失
no-ack = False,如果消费者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。
%r用rper()方法处理对象
%s用str()方法处理对象
1、生产者不需要不需要变化
import pika
# ######################### 生产者 #########################
print('--------------------生产者------------------------')
#链接rabbit服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.152'))
channel = connection.channel() #创建频道
channel.queue_declare(queue='nihao001')#创建队列名为xiaoluo001
#向xiaoluo001队列插入数字,routing_key=‘xiaoluo001’为队列名,body后则是插入的数据
channel.basic_publish(exchange='',
routing_key='nihao001',
body='Hello World!123456790')
print(" [x] Sent 'Hello World!'")
#缓冲区已经flush且消息已经 确认发送到rabbitMq,最后关闭连接
connection.close()
2、消费者
import pika #导入pika模块
import time
#创建并连接rabbitmq
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="192.168.1.152"))
#创建频道
channel = connection.channel()
#如果生产者没队列,那么消费者创建队列
channel.queue_declare(queue='lcj006')
def callback(ch,method,properties,body):
print('[xx] Receivd %r' % body) #%r:%r用rper()方法处理对象
import time
time.sleep(3)
print("ok")
ch.basic_ack(delivery_tag = method.delivery_tag) #此代码主要表示:ch想消息对列传送数据,表示数据已经传送成功,不需再传数据
channel.basic_consume(callback,
queue='lcj006',
no_ack=False) #no_ack=False:服务器挂机,rabbitmq会将此消息放至消息队列中
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming() #取队列中取数据
#当生产者生成一条数据,别消费者接受,消费者中断后如果不超过10秒,连接池队列中的数据依然存在,
# ,否则超过10秒,消费者重新链接服务器,数据丢失,从而消费者等待
3、durable 消息不丢失(持久化)
durable作用:当rabbitmq挂机了,durable将队列中数据持久化,前提需告知生产者发送的数据是需要durable持久化,那么代码中就需要用到delivery_mode=2对数据进行标记
生产者:
import pika
#链接rabbit服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.152'))
#创建频道
channel = connection.channel()
#创建队列名为hello,使用持久化durable=True
# make message persistent
channel.queue_declare(queue='hello001', durable=True) #
#向hello队列插入数字,routing_key=‘Hello World!’,body后则是插入的数据
channel.basic_publish(exchange='', #这个exchange参数就是这个exchange的名字. 空字符串标识默认的或者匿名的exchange:如果存在routing_key, 消息路由到routing_key指定的队列中。
routing_key='hello001',
body='Hello World!',
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
# 给服务器标记此条消息需要持久化,通过delivery_mode=2设置,
#不管mq是否挂机,此条消息都会存在服务器的消息对列中
))
print(" [x] Sent '开始队列!'")
connection.close()
消费者:
#!/usr/bin/env python
#*- coding:utf-8 -*-
import pika
print("----------------------消费者--------------------")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.152'))
channel = connection.channel()
#创建队列名为hello,使用持久化durable=True
# make message persistent
channel.queue_declare(queue='hello001', durable=True)
#此代码主要表示:ch向消息对列传送数据,表示数据已经传送成功,不需再传数据
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='hello001',
no_ack=False)
print(' [*] 等待队列. To exit press CTRL+C')
channel.start_consuming()
注意:标记消息为持久化的并不能完全保证消息不会丢失,尽管告诉RabbitMQ保存消息到磁盘,当RabbitMQ接收到消息还没有保存的时候仍然有一个短暂的时间窗口. RabbitMQ不会对每个消息都执行同步fsync(2) --- 可能只是保存到缓存cache还没有写入到磁盘中,这个持久化保证不是很强,但这比我们简单的任务queue要好很多,如果你想很强的保证你可以使用 publisher confirms
4、消息获取顺序
默认消息队列里的数据是按照顺序被消费者拿走,例如:消费者1 去队列中获取 奇数 序列的任务,消费者1去队列中获取 偶数 序列的任务。
channel.basic_qos(prefetch_count=1) 表示谁来谁取,不再按照奇偶数排列
生产者:
import pika
import sys
#链接rabbit服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.152'))
#创建频道
channel = connection.channel()
#创建队列名为hello,使用持久化durable=True
# make message persistent
channel.queue_declare(queue='hello0001', durable=True) #
message=''.join(sys.argv[1:])or "nihao"
#向hello队列插入数字,routing_key=‘Hello World!’,body后则是插入的数据
channel.basic_publish(exchange='', #这个exchange参数就是这个exchange的名字. 空字符串标识默认的或者匿名的exchange:如果存在routing_key, 消息路由到routing_key指定的队列中。
routing_key='hello0001',
body='Hello World!',
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
# 给服务器标记此条消息需要持久化,通过delivery_mode=2设置,
#不管mq是否挂机,此条消息都会存在服务器的消息对列中
))
print(" [x] Sent '开始队列!'")
connection.close()
消费者:
#!/usr/bin/env python
#*- coding:utf-8 -*-
import pika
print("----------------------消费者--------------------")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.152'))
channel = connection.channel()
#创建队列名为hello001,设置持久化队列durable=True
# make message persistent
channel.queue_declare(queue='hello0001', durable=True)
#此代码主要表示:ch向消息对列传送数据,表示数据已经传送成功,不需再传数据
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='hello0001',
no_ack=False)
print(' [*] 等待队列. To exit press CTRL+C')
channel.start_consuming()
exchange交换
exchange类型可用: direct , topic , headers 和 fanout 。 我们将要对最后一种进行讲解 --- fanout
exchange type = fanout :表示只有跟exhange绑定连接的所有队列都发消息
exchange type = direct :队列绑定关键
exchange type = topic :绑定几个模糊的关键字
5、发布订阅
发布订阅和简单的消息队列区别在于,发布订阅会将消息发送给所有的订阅者,而消息队列中的数据被消费一次便消失。所以,RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者发布消息时,会将消息放置在所有相关队列中。
exchange type = fanout
#fanout :表示只有跟exhange绑定连接的所有队列都发消息
1、订阅者:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pika
#连接rabbirmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.152'))
#建立频道
channel = connection.channel()
#创建exchange=logs,type='fanout表示只要跟exchange绑定连接的所有对列都会收到消息
channel.exchange_declare(exchange='logs',
type='fanout')
#随机创建队列名,每一次创建一个队列
result = channel.queue_declare(exclusive=True) #队列断开后自动删除临时队列
queue_name = result.method.queue #队列名采用服务器端分配的临时队列
#绑定队列queue_name
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)
#通过callback发消息
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
2、发布
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pika
import sys
#简介rabbitmq
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.152'))
#建立频道
channel = connection.channel()
#创建exchange=logs,type='fanout表示只要跟exchange绑定连接的所有对列都会收到消息
channel.exchange_declare(exchange='logs',
type='fanout')
#接收参数
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
#将message参数信息发到exchange里面
channel.basic_publish(exchange='logs',
routing_key='',
body=message)
print(" [x] Sent %r" % message)
connection.close()
6、关键字发送
exchange type = direct
之前事例,发送消息时明确指定某个队列并向其中发送消息,RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。
生产者:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pika
print('-----------------生产者发消息---------------')
import sys
#连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(host="192.168.1.152"))
#创建频道
channel= connection.channel()
##随机创建队列名,每一次创建一个队列
channel.exchange_declare(exchange='direct_logs-test',
type='direct')
# severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
# message = ''.join(sys.argv[2:]) or 'nihao lcj'
severity = 'info'
message = '1234567890'
channel.basic_publish(exchange="direct_logs-test",
routing_key=severity, #关键字
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
2、消费者
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
import pika
import sys
print('---------------------消费者取消息-----------------')
#连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='192.168.1.152'))
#创建频道
channel = connection.channel()
#创建exchange=logs,类型为direct
channel.exchange_declare(exchange='direct_logs-test',
type='direct')
#随机创建队列名,每一次创建一个队列
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
#
# severities = sys.argv[1:]
severities = ['info',]
# if not severities:
# sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
# sys.exit(1)
#绑定 severity为动态参数
for severity in severities: #循环取值
channel.queue_bind(exchange='direct_logs-test',
queue=queue_name,
routing_key=severity) #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()
6、模糊匹配
exchange type = topic
在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。
- # 表示可以匹配 0 个 或 多个 单词
- * 表示只能匹配 一个 单词
发送者路由值 队列中 old.boy.python old.* -- 不匹配 old.boy.python old.# -- 匹配
消费者:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:lcj
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
# -*- coding:utf-8 -*-
# Author:lcj
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()
浙公网安备 33010602011771号