RabbitMQ

1 安装

 启动服务器 

 rabbitmq-server

你也可以添加 -detached 属性来让它在后台运行(注意:只有一个破折号)

sudo rabbitmq-server -detached

永远不要用 kill 停止 RabbitMQ 服务器,而是应该用 rabbitmqctl 命令:

sudo rabbitmqctl stop

 2 安装 控制台插件 web  页面详情

rabbitmq-plugins enable  rabbitmq_management

4 列出有哪些队列,有哪些消息在等待中

rabbitmqctl list_queues

简单的hello world

1 生产者发送 hello world 消息队列中

2 消费者获取消息并打印

[root@jinkang-e2elog rabbitmq]# cat send.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
#创建一个名称为hello的队列
channel.queue_declare(queue='hello')

for i in range(10):
        channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello ' + str(i) + ' World')
#recive.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
#创建一个名称为hello的队列
channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
channel.basic_consume(on_message_callback=callback,queue='hello',auto_ack=True)
channel.start_consuming()  #进程会一直执行

默认来说,RabbitMQ会按顺序得把消息发送给每个消费者(consumer)。
平均每个消费者都会收到同等数量得消息。这种发送消息得方式叫做——轮询(round-robin)

以上面的 hello world demo 来说, 生产者 发送了 10个消息  range(10)

我们在两个窗口 执行两个 recive.py ,构造两个 worker

 

rabbitmq 会均匀的将消息发送给两个 woker。

原文地址:https://www.cnblogs.com/jkklearn/p/13772008.html