RabbitMQ基本使用

一、rabbitmq介绍

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
# 消息队列: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基本使用

复制代码
  • 1
  • 2
# python 操作 # pip3 install pika
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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()
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
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机制

复制代码
  • 1
# 消费者收到消息后,要回复给消息队列,消息队列收到回复,该消息就删除,如果收不到,就一直放在消息队列中,再起一个消费者,还会消费这个消息
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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()
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
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()

四、持久化

复制代码
  • 1
  • 2
# 消息丢失:放在内存中,rabbimq服务挂掉,消息就没了,通过持久化,保证队列和消息都不丢失 # 队列持久化要是一个新队列
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
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()
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
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()

五、闲置消费

复制代码
  • 1
# 如果消息队列中有多条消息,消费者有两个---》第一个取一个消息消费,第二个消费者取第二个消息消费,第一个消费取第三个消息消费
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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()
复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
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

posted @   马氵寿  阅读(69)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开