RabbitMQ 队列

1、RabbitMQ 队列

pip install pika  # python 要连接 rabbitmq 的工具

# rabbitmqctl list_queues 显示当前的队列列表

# rabbitmqctl add_user wenming wenming # rabbitmq 创建用户

# rabbitmqctl set_permissions -p / wenming ".*" ".*" ".*" # 授权用户,允许访问(这条命令授权了所有队列访问权)

创建队列
往队列里发消息
# import pika
#
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# # 声明queue 队列=持久化(防止rabbitmq关闭宕机时导致的队列丢失)
# channel.queue_declare(queue='hello',durable=True)
#
# # 往队列里发消息
# channel.basic_publish(exchange="",
# routing_key='hello', # 路由,告诉exchange,把body内容,路由到 hello队列
# properties=pika.BasicProperties(delivery_mode=2,), # 把消息变成持久化的(如果消息未被正常消费,会重新反至队列中)
# body="Hello World!"
# )
#
# print("[x] Sent 'Hello World!'")
#
# # 关闭 socket 连接
# connection.close()
# # # 注 :如果只有队列持久化,而消息没有持久化,那么服务宕机关闭时,队列同样会丢失


从指定队列里取消息
# import pika
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# # 队列连接通道、请求方法、消息参数、消息内容
# 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='hello', # 从hello队列里取
# no_ack=True) # 消息处理后,是否向 rabbit-server 确认消息已消费完毕(True为不确认。False用来搭配持久化消息使用(不填写默认为确认))
#
# print('[*] Waiting for messages. To exit press CTRL+C')
#
# channel.start_consuming() # 阻塞模式(没消息就一直等,接收后继续等)


广播、组播、规则播 模式

exchange 类型:
fanout 所有bind到此exchange的queue都可以接收消息 # 广播
direct # 组播
topic # 规则播
header

广播: 发布 和 订阅
# import pika,sys
#
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# # 声明广播
# channel.queue_declare(exchange='logs',type='fanout')
#
# message = ' '.join(sys.argv[1:]) or "info:Hello world!"
#
# # 往队列里发消息
# channel.basic_publish(exchange="logs",
# routing_key='', # 路由,告诉exchange,把body内容,路由到 hello队列
# properties=pika.BasicProperties(delivery_mode=2,), # 把消息变成持久化的(如果消息未被正常消费,会重新反至队列中)
# body=message
# )
#
# print("[x] Sent %r"%message)
#
# # 关闭 socket 连接
# connection.close()


订阅端
# import pika
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# channel.exchange_declare(exchange='logs',type='fanout') # 如果队列不存在就创建一个
# queue_obj = channel.queue_declare(exclusive=True) # 不指定 queue名字,rabbit会随机分配一个不重复且唯一的名字,exclusive=True会在使用此queue消费者断开后,自动将生成的queue删除
# queue_name = queue_obj.method.queue # 拿到随机生成queue的名字
# channel.queue_bind(exchange='logs',queue=queue_name) # 绑定队列到exchange
#
# # 队列连接通道、请求方法、消息参数、消息内容
# 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', # 从hello队列里取
# no_ack=True) # 消息处理后,是否向 rabbit-server 确认消息已消费完毕(True为不确认。False用来搭配持久化消息使用(不填写默认为确认))
#
# channel.start_consuming() # 阻塞模式(没消息就一直等,接收后继续等)


组播
# import pika,sys
#
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# # 声明组播
# channel.exchange_declare(exchange='direct_logs',type='direct')
#
# log_level = sys.argv[1] if len(sys.argv) > 1 else 'info'
# message = ' '.join(sys.argv[2:]) or "info:Hello world!"
#
# # 往队列里发消息
# channel.basic_publish(exchange="direct_logs",
# routing_key=log_level, # 路由,告诉exchange,把body内容,路由到 hello队列
# properties=pika.BasicProperties(delivery_mode=2,), # 把消息变成持久化的(如果消息未被正常消费,会重新反至队列中)
# body=message
# )
#
# print("[x] Sent %r"%message)
#
# # 关闭 socket 连接
# connection.close()


组播订阅端
# import pika,sys
# credentials = pika.PlainCredentials('wenming','wenming')
# parameters = pika.ConnectionParameters(host='localhost',credentials=credentials)
#
#
# # 非阻塞连接
# connection = pika.BlockingConnection(parameters)
#
# channel = connection.channel() # 队列连接通道
#
# channel.exchange_declare(exchange='direct_logs',type='direct') # 如果队列不存在就创建一个
# queue_obj = channel.queue_declare(exclusive=True) # 不指定 queue名字,rabbit会随机分配一个不重复且唯一的名字,exclusive=True会在使用此queue消费者断开后,自动将生成的queue删除
# queue_name = queue_obj.method.queue # 拿到随机生成queue的名字
#
# log_levels = sys.argv[1:]
# if not log_levels:
# sys.stderr.write('Usage: %s [info] [warning] [error]\n' % sys.argv[0])
# sys.exit(1)
#
# for level in log_levels:
# channel.queue_bind(exchange='direct_logs',queue=queue_name,routing_key=level) # 绑定队列到exchange
#
# # 队列连接通道、请求方法、消息参数、消息内容
# 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', # 从hello队列里取
# no_ack=True) # 消息处理后,是否向 rabbit-server 确认消息已消费完毕(True为不确认。False用来搭配持久化消息使用(不填写默认为确认))
#
# channel.start_consuming() # 阻塞模式(没消息就一直等,接收后继续等)


规则播
# # 代表所有

 

posted @ 2018-07-03 15:49  G500  阅读(92)  评论(0)    收藏  举报