一、rabbitmq介绍
# 消息队列:MQ:消息队列就是基础数据结构中的“先进先出”的一种数据机构。想一下,生活中买东西,需要排队,先排的人先买消费,就是典型的“先进先出
# mq解决的问题:
-应用解耦
-流量削峰
-消息分发
-异步消息
# mq的比较
-rabbitmq:消息准确性
-kafka:吞吐量
# rabbitmq安装
-Linux
# 安装配置epel源
# 安装erlang
yum -y install erlang
# 安装RabbitMQ
yum -y install rabbitmq-server
-windows:https://blog.csdn.net/tirster/article/details/121938987
-erlang安装包
-rabbitmq安装包
-docker安装
-docker pull rabbitmq:management
-docker run -di --name Myrabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 rabbitmq:management
二、rabbitmq基本使用
# python 操作
# pip3 install pika
import pika
# 没有用户名密码情况
# connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205',port=5672))
# 有密码的情况
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 声明一个消息队列叫hello
channel.queue_declare(queue='hello')
# 往消息队列中放一条消息
# routing_key必须等于队列的名字,body是发送的消息
channel.basic_publish(exchange='', routing_key='hello', body='xxx')
print("Sent 'llnb'")
connection.close()
import pika, sys, os
def main():
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 创建一个hello的对列
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
main()
三、ack机制
# 消费者收到消息后,要回复给消息队列,消息队列收到回复,该消息就删除,如果收不到,就一直放在消息队列中,再起一个消费者,还会消费这个消息
import pika
# 没有用户名密码情况
# connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205',port=5672))
# 有密码的情况
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 声明一个消息队列叫hello
channel.queue_declare(queue='hello')
# 往消息队列中放一条消息
# routing_key必须等于队列的名字,body是发送的消息
channel.basic_publish(exchange='', routing_key='hello', body='xxx')
print("Sent 'llnb'")
connection.close()
import pika, sys, os
def main():
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 创建一个hello的对列
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
# raise Exception('sss')
print(" [x] Received %r" % body)
# 逻辑执行完了,落盘了,在回复ack
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
main()
四、持久化
# 消息丢失:放在内存中,rabbimq服务挂掉,消息就没了,通过持久化,保证队列和消息都不丢失
# 队列持久化要是一个新队列
import pika, sys, os
def main():
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 创建一个hello的对列
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
# raise Exception('sss')
print(" [x] Received %r" % body)
# 逻辑执行完了,落盘了,在回复ack
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
main()
import pika
# 没有用户名密码情况
# connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205',port=5672))
# 有密码的情况
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 声明一个消息队列叫hello,对队列进行持久化
channel.queue_declare(queue='lqz', durable=True)
# 往消息队列中放一条消息
# routing_key必须等于队列的名字,body是发送的消息
channel.basic_publish(exchange='', routing_key='lqz', body='xxx', properties=pika.BasicProperties(
delivery_mode=2, # make message persistent,消息也持久化
))
print("Sent 'llnb'")
connection.close()
五、闲置消费
# 如果消息队列中有多条消息,消费者有两个---》第一个取一个消息消费,第二个消费者取第二个消息消费,第一个消费取第三个消息消费
import pika
# 没有用户名密码情况
# connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205',port=5672))
# 有密码的情况
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 声明一个消息队列叫hello,对队列进行持久化
channel.queue_declare(queue='hello')
# 往消息队列中放一条消息
# routing_key必须等于队列的名字,body是发送的消息
channel.basic_publish(exchange='', routing_key='hello', body='3333')
print("Sent 'xxx'")
connection.close()
import pika, sys, os
def main():
credentials = pika.PlainCredentials("admin", "admin")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.0.0.205', port=5672, credentials=credentials))
channel = connection.channel()
# 创建一个hello的对列
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
# raise Exception('sss')
print(" [x] Received %r" % body)
# 逻辑执行完了,落盘了,在回复ack
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1) # 哪个消费者空闲,就会去消费消息
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
main()
六、发布订阅
七、rpc