RabbitMQ队列
官方文档
http://www.rabbitmq.com/install-rpm.html
二、安装rabbitmq 1、下载rabbitmq-server-generic-unix-3.6.5.tar.xz 2、tar xvf rabbitmq-server-generic-unix-3.6.5.tar.xz 3、mv rabbitmq_server-3.6.5/ /usr/local/rabbitmq 4、启动: #启动rabbitmq服务 /usr/local/rabbitmq/sbin/rabbitmq-server #后台启动 /usr/local/rabbitmq/sbin/rabbitmq-server -detached #关闭rabbitmq服务 /usr/local/rabbitmq/sbin/rabbitmqctl stop 或 ps -ef | grep rabbit 和 kill -9 xxx #开启插件管理页面 /usr/local/rabbitmq/sbin/rabbitmq-plugins enable rabbitmq_management #创建用户 /usr/local/rabbitmq/sbin/rabbitmqctl add_user rabbitadmin 123456 usr/local/rabbitmq/sbin/rabbitmqctl set_user_tags rabbitadmin administrator 5、登录 #WEB登录 http://10.10.3.63:15672 用户名:rabbitadmin 密码:123456 授权url /usr/local/rabbitmq/sbin/rabbitmqctl set_permissions -p / rabbitadmin ".*" ".*" ".*"
url访问权限 /usr/local/rabbitmq/sbin/rabbitmqctl set_permssions -p / rabbitadmin ".*"".*"".*"
命令行查看消息 /usr/local/rabbitmq/sbin/rabbitmqctl lixt_queues
开机启动 chkconfig rabbitmq-server on
实现最简单的队列通信
这是一个公平的一次分发,就是每个人轮
send端
import pika credentials=pika.PlainCredentials("rabbitadmin","123456") connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) channel=connection.channel()#建立的rabbit 协议的通道 #声明queue channel.queue_declare(queue='hello')#声明队列 #n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. channel.basic_publish(exchange='', routing_key="hello",#把消息发到这个队列 body='Hello world'#发送的内容 ) print("[x]Sent 'Hello World!") connection.close() #把队列关掉
receive端
import pika import time credentials=pika.PlainCredentials("rabbitadmin","123456") connection=pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) channel=connection.channel() #通道的实例 #You may ask why we declare the queue again ‒ we have already declared it in our previous code. # We could avoid that if we were sure that the queue already exists. For example if send.py program #was run before. But we're not yet sure which program to run first. In such cases it's a good # practice to repeat declaring the queue in both programs. channel.queue_declare(queue='hello') #当消费者先来的时候消费者申明列队
#消费者先来,但包子铺还没有,消费者就先建一个包子铺等包子
#ch=通道的实例 method=服务器发来的头部消息 body=发来的内容 def callback(ch, method, properties, body): print("received msg...start processing....",body) time.sleep(20) print(" [x] Received %r" % body) channel.basic_consume(callback, #拿到消息后调用callback函数 queue="hello", #从这个队列里拿数据 no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
Work Queues
在这种模式下,RabbitMQ会默认把p发的消息依次分发给各个消费者(c),跟负载均衡差不多
假如说处理任务需要花一分钟,处理的时候处理到一半死掉了,怎么办?应该为了安全起见让另外一个消费者处理。不是消费者把包子放回去的,而是包子铺严格的检测看看你吃完之后会不会死,为了防止接受任务没处理完,就让消费者吃完包子给一个回馈说你真吃完了证明安全的完整的处理完了还没死,如果吃了一半没给回复那就认为已经死了。所以这个机制就是:必须让消费者把任务处理完,必须给客户端有一个响应说任务处理完了。包子铺才会真正的在记账本上记一笔账说包子被真正的消费掉了
只要链接断了没给反应就是死了
在RabbitMQ里消息被拿走之后队列里并没有把它删除,虽然没在队列里但是存到了server的其他地方,等消费者给出响应确认消息执行完了才会把消息从RabbitMQ里删掉。如果没有响应就会把消息再放到队列里,其他的客户端就会再收到这条消息了。
消息提供者代码
import pika import time connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = connection.channel()#实例对象 # 声明queue channel.queue_declare(queue='task_queue',durble=True) #durble=True让队列持久化,让队列永久保存 # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. import sys
#把脚本收到的参数当作参数 没有参数就发hello world message = ' '.join(sys.argv[1:]) or "Hello World! %s" % time.time() channel.basic_publish(exchange='', routing_key='task_queue', #往这个队列发 body=message, #发的内容 properties=pika.BasicProperties( delivery_mode=2, # make message persistent #消息持久,保留消息 ) ) print(" [x] Sent %r" % message) connection.close()
消费者代码
import pika, time connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = connection.channel() #实例对象 def callback(ch, method, properties, body): #函数 print(" [x] Received %r" % body) #内容 time.sleep(20) print(" [x] Done") print("method.delivery_tag",method.delivery_tag) ch.basic_ack(delivery_tag=method.delivery_tag)#唯一标记,标识符 no_ack 注掉 #ackownledgement确认的意思 channel.basic_consume(callback, queue='task_queue', no_ack=True #false 没处理完则一直保留在列队 消息持久化 和上面的标识符一起用 #执行完了把标识符返回给客服端确认消息处理完了 ) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
此时,先启动消息生产者,然后再分别启动3个消费者,通过生产者多发送几条消息,你会发现,这几条消息会被依次分配到各个消费者身上
Doing a task can take a few seconds. You may wonder what happens if one of the consumers starts a long task and dies with it only partly done. With our current code once RabbitMQ delivers message to the customer it immediately removes it from memory. In this case, if you kill a worker we will lose the message it was just processing. We'll also lose all the messages that were dispatched to this particular worker but were not yet handled.
But we don't want to lose any tasks. If a worker dies, we'd like the task to be delivered to another worker.
In order to make sure a message is never lost, RabbitMQ supports message acknowledgments. An ack(nowledgement) is sent back from the consumer to tell RabbitMQ that a particular message had been received, processed and that RabbitMQ is free to delete it.
If a consumer dies (its channel is closed, connection is closed, or TCP connection is lost) without sending an ack, RabbitMQ will understand that a message wasn't processed fully and will re-queue it. If there are other consumers online at the same time, it will then quickly redeliver it to another consumer. That way you can be sure that no message is lost, even if the workers occasionally die.
There aren't any message timeouts; RabbitMQ will redeliver the message when the consumer dies. It's fine even if processing a message takes a very, very long time.
Message acknowledgments are turned on by default. In previous examples we explicitly turned them off via the no_ack=True flag. It's time to remove this flag and send a proper acknowledgment from the worker, once we're done with a task.
def callback(ch, method, properties, body): print " [x] Received %r" % (body,) time.sleep( body.count('.') ) print " [x] Done" ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback, queue='hello')
消息持久化
We have learned how to make sure that even if the consumer dies, the task isn't lost(by default, if wanna disable use no_ack=True). But our tasks will still be lost if RabbitMQ server stops.
When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.
First, we need to make sure that RabbitMQ will never lose our queue. In order to do so, we need to declare it as durable:
channel.queue_declare(queue='hello', durable=True)
Although this command is correct by itself, it won't work in our setup. That's because we've already defined a queue called hello which is not durable. RabbitMQ doesn't allow you to redefine an existing queue with different parameters and will return an error to any program that tries to do that. But there is a quick workaround - let's declare a queue with different name, for exampletask_queue:
channel.queue_declare(queue='task_queue', durable=True)
This queue_declare change needs to be applied to both the producer and consumer code.
At that point we're sure that the task_queue queue won't be lost even if RabbitMQ restarts. Now we need to mark our messages as persistent - by supplying a delivery_mode property with a value 2.
channel.basic_publish(exchange='', routing_key="task_queue", body=message, properties=pika.BasicProperties( delivery_mode = 2, # make message persistent ))
消息公平分发
如果Rabbit只管按顺序把消息发到各个消费者身上,不考虑消费者负载的话,很可能出现,一个机器配置不高的消费者那里堆积了很多消息处理不完,同时配置高的消费者却一直很轻松。为解决此问题,可以在各个消费者端,配置perfetch=1,意思就是告诉RabbitMQ在我这个消费者当前消息还没处理完的时候就不要再给我发新消息了。
channel.basic_qos(prefetch_count=1)
带消息持久化+公平分发的完整代码
生产者端
import pika import time credentials=pika.PlainCredentials("rabbitadmin","123456") connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) channel = connection.channel() # 声明queue #durable可以让队列永久保存 channel.queue_declare(queue='task_queue',durable=True) #创建队列 # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. import sys message = ' '.join(sys.argv[1:]) or "Hello World! %s" % time.time() #把脚本的参数拼接起来 channel.basic_publish(exchange='', routing_key='task_queue', #往这个队列发 body=message, #发的内容 properties=pika.BasicProperties( delivery_mode=2, # make message persistent#消息持久,保留消息 ) ) print(" [x] Sent %r" % message) connection.close()
消费者端
import pika, time credentials=pika.PlainCredentials("rabbitadmin","123456") connection=pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) channel = connection.channel() # 声明queue #durable可以让队列永久保存 # channel.queue_declare(queue='task_queue') #创建队列 #如果另一头声明了这这边不能声明了 def callback(ch, method, properties, body): print(" [x] Received %r" % body) time.sleep(20) print(" [x] Done") print("method.delivery_tag", method.delivery_tag) ch.basic_ack(delivery_tag=method.delivery_tag)#唯一标记,标识符 no_ack 注掉 #ackownledgement确认的意思 channel.basic_qos(prefetch_count=1) channel.basic_consume(callback, queue='task_queue', # no_ack=True #false 没处理完则一直保留在列队 和上面的标识符一起用 ) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
直播流程
要确保全国各地都不卡,所有人都跑到北京的机房就瘫了,
中心节点,然后外面围一圈,把直播的视频切片,每几秒钟就切一次片推到外面那一圈上面去,再传到各个地方去,用户连的是各个地方的而不是北京的是视频镜像,镜像不断地同步
Publish\Subscribe(消息发布\订阅)
之前的例子都基本都是1对1的消息发送和接收,即消息只能发送到指定的queue里,但有些时候你想让你的消息被所有的Queue收到,类似广播的效果,这时候就要用到exchange了,
An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues. The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type.
Exchange在定义的时候是有类型的,以决定到底是哪些Queue符合条件,可以接收消息
fanout: 所有bind到此exchange的queue都可以接收消息
direct: 通过routingKey和exchange决定的那个唯一的queue可以接收消息
topic:所有符合routingKey(此时可以是一个表达式)的routingKey所bind的queue可以接收消息
表达式符号说明:#代表一个或多个字符,*代表任何字符
例:#.a会匹配a.a,aa.a,aaa.a等
*.a会匹配a.a,b.a,c.a等
注:使用RoutingKey为#,Exchange Type为topic的时候相当于使用fanout
headers: 通过headers 来决定把消息发给哪些queue
广播
消息publisher
fanout就是广播,不用声明队列,不直接往队列里发消息
import pika import sys credentials=pika.PlainCredentials("rabbitadmin","123456") connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) 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()
消息subscriber
每个人都要声明一个队列,队列的名字不能重复,每次断开Q就会自动删掉
把自己声明的Q绑定到交换机上,才能接受到广播
import pika credentials=pika.PlainCredentials("rabbitadmin","123456") connection=pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='logs',type='fanout')#声明队列,怕另一端没有q result = channel.queue_declare(exclusive=True) # 不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除 queue_name = result.method.queue #Q的名字 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()
有选择的接收消息(exchange type=direct)
RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。
publisher
#组播 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'#严重程度,级别的意思 #python direct_send.py 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()
subscriber
import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.channel() channel.exchange_declare(exchange='direct_logs', type='direct') #生成随机Q 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) #python direct_receive.py info warning error #有几个参数就绑定几个 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()
更细致的消息过滤
Although using the direct exchange improved our system, it still has limitations - it can't do routing based on multiple criteria.
In our logging system we might want to subscribe to not only logs based on severity, but also based on the source which emitted the log. You might know this concept from the syslog unix tool, which routes logs based on both severity (info/warn/crit...) and facility (auth/cron/kern...).
That would give us a lot of flexibility - we may want to listen to just critical errors coming from 'cron' but also all logs from 'kern'.
publisher
#和组播差不多 #但是可以加条件过滤 import pika import sys credentials=pika.PlainCredentials("rabbitadmin","123456") connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128',credentials=credentials)) 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()
subscriber
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()
To receive all the logs run:
python receive_logs_topic.py "#"
To receive all logs from the facility "kern":
python receive_logs_topic.py "kern.*"
Or if you want to hear only about "critical" logs:
python receive_logs_topic.py "*.critical"
You can create multiple bindings:
python receive_logs_topic.py "kern.*" "*.critical"
And to emit a log with a routing key "kern.critical" type:
python emit_log_topic.py "kern.critical" "A critical kernel error"
Remote procedure call (RPC)
To illustrate how an RPC service could be used we're going to create a simple client class. It's going to expose a method named call which sends an RPC request and blocks until the answer is received:
fibonacci_rpc = FibonacciRpcClient() result = fibonacci_rpc.call(4) print("fib(4) is %r" % result)
RPC server
import pika import time connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.channel() channel.queue_declare(queue='rpc_queue') #声明Q 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()
RPC client
import pika import uuid class FibonacciRpcClient(object): def __init__(self): credentials = pika.PlainCredentials("rabbitadmin", "123456") self.connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.31.128', credentials=credentials)) channel = self.connection.channel() self.channel = self.connection.channel() #连上远程的Q result = self.channel.queue_declare(exclusive=True) self.callback_queue = result.method.queue #生成随机一个Q 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: #判断发出去的id和接受的id是否相等 self.response = body #把执行的结果赋给body def call(self, n): self.response = None #标识符,有结果则改变 self.corr_id = str(uuid.uuid4()) #唯一标识符 self.channel.basic_publish(exchange='', routing_key='rpc_queue', #声明一个Q 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 int(self.response) #不为Noen时返回结果 fibonacci_rpc = FibonacciRpcClient() #实例化 print(" [x] Requesting fib(30)") response = fibonacci_rpc.call(30) #执行call #拿到self.response的值 print(" [.] Got %r" % response)