饭叔的知识整理

rabbitmq

安装

$ brew search rabbitmq
rabbitmq     rabbitmq-c      rabbitmq30
$ brew install rabbitmq

运行

$ cd /usr/local/Cellar/rabbitmq/3.4.4/sbin
$ ./rabbitmq-server 

Hello World

https://www.rabbitmq.com/tutorials/tutorial-one-python.html

  • send.py

      #coding=utf-8
      import pika
    
      connection = pika.BlockingConnection(
          pika.ConnectionParameters('localhost')) #建立连接
      channel = connection.channel()    #建立通道
      channel.queue_declare(queue='hello') #声明队列(不确定队列是否存在时可重复调用)
      channel.basic_publish(exchange='',
                            routing_key='hello',
                            body='Hello World!') #发布消息
      print " [x] Sent 'Hello World!'"
      connection.close() #关闭连接
    
  • receive.py

      #coding=utf-8
      import pika
    
      connection = pika.BlockingConnection(pika.ConnectionParameters(
              host='localhost')) #建立连接
      channel = connection.channel() #建立通道
      channel.queue_declare(queue='hello') #声明队列(不确定队列是否存在时可重复调用)
      print ' [*] Waiting for messages. To exit press CTRL+C'
      def callback(ch, method, properties, body):
          print " [x] Received %r" % (body,) #定义回调
      channel.basic_consume(callback,
                            queue='hello',
                            no_ack=True) #准备接受消息
      channel.start_consuming() #开始接受消息
    
  • 测试

      $ python send.py 
       [x] Sent 'Hello World!'
    
      $ python receive.py 
       [*] Waiting for messages. To exit press CTRL+C
       [x] Received 'Hello World!'
    

查看MessageQueue

$ rabbitmqctl list_queues

参考

https://www.rabbitmq.com/
https://www.rabbitmq.com/getstarted.html
http://rabbitmq-into-chinese.readthedocs.org/zh_CN/latest/