RabbitMQ
简介
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
安装
# 安装 rabbitMQ 程序 yum -y install rabbitmq-server
启动
# 启动 rabbitMQ systemctl restart rabbitmq-server # 启动管理插件,启动后访问 http://localhost:15672,默认用户名密码 guest/guest rabbitmq-plugins enable rabbitmq-management
使用 API 操作 RabbitMQ
生产者消费者模型是通过队列来实现的
import time import queue import threading q = queue.Queue(10) def producer(msg): while True: time.sleep(1) q.put(msg) def consumer(): while True: time.sleep(0.5) msg = q.get() print(msg) for i in range(5): t = threading.Thread(target=producer, args=(i,)) t.start() for i in range(10): t = threading.Thread(target=consumer) t.start()
RabbitMQ 也是一个队列,也可以实现生产者消费者模型,区别在于对象并非存在内存的 queue 中,而是存在于一台服务器基于 RabbitMQ Server 实现的消息队列中
"""生产者: send.py""" import pika # 创建一个连接 connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建一个队列,队列名为 hello channel.queue_declare(queue='hello') # 向 hello 队列中添加一条消息 'Hello World!' channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print(" [x] Sent 'Hello World!'") connection.close()
"""消费者: receive.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) 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、Message acknowledgment 消息确认
no-ack=False 默认值,如果消费者由于故障(its channel is closed, connection is closed, or TCP connection is lost)为发送消息确认,生产者会将数据再重新添加到队列中
"""消费者: receive.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print(" [x] Received %r" % body) # 消息确认,当未执行时,消息会重新添加回队列 ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume(callback, queue='hello', no_ack=False) # no_ack=False 为默认值 print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
2、Message durability 消息持久
当 RabbitMQ Server 因故重启,希望数据消息不丢失
"""生产者: send.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 为队列添加属性用于保持消息持久: durable=True channel.queue_declare(queue='task_queue', durable=True) channel.basic_publish(exchange='', routing_key='task_queue', body='Hello World!', properties=pika.BasicProperties( delivery_mode=2, # 保持消息持久 )) print(" [x] Sent 'Hello World!'") connection.close()
"""消费者""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # durable=True channel.queue_declare(queue='task_queue', durable=True) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume(callback, queue='task_queue') print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
3、消息获取顺序
消费者在获取消息时默认为平均获取。当有两个消费者获取消息,消费者一处理消息需要2s,消费者二处理消息需要4s,RabbitMQ 按照默认的调度平均分配消息,会导致消费者一很闲,消费者二很忙。修改 basic.qos 为 prefetch_count=1,表示谁来谁去,不在按照顺序调度任务
"""消费者:receive.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag=method.delivery_tag) # 修改调度顺序 channel.basic_qos(prefetch_count=1) channel.basic_consume(callback, queue='task_queue') print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
4、发布与订阅
发布者发送消息,所有的订阅者都能收到消息。
但是队列中的消息消费一次之后就会消失,所有需要给每一个订阅者绑定一个队列,发布者将消息发布到所有的消费者绑定的队列中,所有的订阅者到自己绑定的队列中消费消息。
创建临时队列:
# 创建临时队列,队列名称类似:amq.gen-JzTY20BRgKO-HjmUJj0wLg # exclusive=True 当消费者断开连接时,队列自动删除 result = channel.queue_declare(exclusive=True) # 获取队列名称 queue_name = result.method.queue
exchange:
RabbitMQ 在发送消息时并不是直接发送给队列,而是通过 exchange 发送给队列
# 创建 exchange channel.exchange_declare(exchange='logs',type='fanout') # exchange 绑定队列 channel.queue_bind(exchange='logs', queue=queue_name)
"""发布者: send.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建临时队列并获取队列名称 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.basic_publish(exchange='logs', routing_key='', body='Hello World!', ) print(" [x] Sent 'Hello World!'") connection.close()
"""订阅者""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建临时队列并获取队列名称 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 创建 exchange 并绑定临时队列到 channel.exchange_declare('logs', type='fanout') channel.queue_bind(exchange='logs', queue=queue_name) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback, queue=queue_name) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
RabbitMQ 的 绑定如图:
5、关键字发送(Routing)
为队列绑定关键字,进行路由
创建 exchange:
# 创建 exchange channel.exchange_declare(exchange='logs',type='direct') # 绑定队列 channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key='关键字')
创建生产者与消费者:
"""生产者: send.py""" import sys import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建 exchange 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()
"""消费者: receive.py""" import sys import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建临时队列并获取队列名称 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) # 为队列绑定 exchange 和 routing key for severity in severities: print(severity) channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback, queue=queue_name) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
RabbitMQ 的 绑定如图:
# 启动三个消费者,分别根据不同的关键字获取消息 python receive.py info python receive.py error python receive.py info error # 发布关键字为 info 和 error 的消息 python send.py info info_message python send.py error error_message
6、模糊匹配
当 exchange 的 type 为 topic 时,关键字允许模糊匹配
- * 表示匹配后面的一个单词
- # 表示匹配后面的一个或多个单词
"""生产者: send.py""" import sys import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建 exchange channel.exchange_declare('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()
"""消费者: receive.py""" import sys import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) channel = connection.channel() # 创建 exchange 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) # 为队列绑定 exchange 和 routing key for binding_key in binding_keys: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key) def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume(callback, queue=queue_name) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
观察运行命令:
# 消费者 python receive.py kern.* python receive.py kern.# # 生产者 python send.py kern.info "info message" python send.py kern.error "error message" python send.py kern.error.info "error message"
7、RPC
"""rpc_server.py""" import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) 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()
"""rpc_client.py""" import pika import uuid class FibonacciRpcClient(object): def __init__(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.5')) 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: self.response = body def call(self, n): self.response = None self.corr_id = str(uuid.uuid4()) 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() return int(self.response) fibonacci_rpc = FibonacciRpcClient() print(" [x] Requesting fib(30)") response = fibonacci_rpc.call(30) print(" [.] Got %r" % response)