一 、安装:
参考:
执行sudo apt-get install rabbitmq-server报错:The following packages have unmet dependencies: linux-generic : Depends: linux-headers-generic (= 4.4.0.127.133) but 4.4.0.109.114 is to be installed rabbitmq-server : Depends: erlang-nox (>= 1:13.b.3) but it is not going to be installed or esl-erlang but it is not installable#### 执行sudo apt-get install -f sudo apt-get install erlang-noxsudo apt-get install rabbitmq-server
二、注册用户和管理:
Ubuntu上安装和使用RabbitMQ1. 安装RabbitMQ服务软件包输入以下命令进行安装#apt install rabbitmq-server 2.安装完成后在rabbitMQ中添加用户命令:#rabbitmqctl add_user username password将用户设置为管理员(只有管理员才能远程登录)命令:#rabbitmqctl set_user_tags username administrator同时为用户设置读写等权限命令:#rabbitmqctl set_permissions -p / username ".*" ".*" ".*" 3.安装RabbitMQ监控管理插件进行RabbitMQ的管理命令:#rabbitmq-plugins enable rabbitmq_management插件rabbitmq_management启动成功后就可以通过web页面进行RabbitMQ的监控和管理 4.使用rabbitmq_management插件进行监控和管理使用firefox浏览器登录:http://localhost:15672在登录页面使用 guest/guest用户名和密码登录RabbitMQ管理系统,在系统中可以对RabbitMQ服务进行channel,queue,用户等的管理 PS:Guest账号不能远程登录。如果还不能远程访问或远程登录检查是不是5672, 15672端口没有开放!!!!!!
三、简单使用:
参考:
生产者:# !/usr/bin/env pythonimport pikacredentials = pika.PlainCredentials('admin','123456')connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.56.19',5672,'/',credentials))channel = connection.channel()# 声明queuechannel.queue_declare(queue='balance')# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.channel.basic_publish(exchange='', routing_key='balance', body='Hello World!')print(" [x] Sent 'Hello World!'")connection.close()消费者:# _*_coding:utf-8_*___author__ = 'Alex Li'import pikacredentials = pika.PlainCredentials('admin','123456')connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.56.19',5672,'/',credentials))channel = connection.channel()# You may ask why we declare the queue again ‒ we have already declared it in our previous code.# We could avoid that if we were sure that the queue already exists. For example if send.py program# was run before. But we're not yet sure which program to run first. In such cases it's a good# practice to repeat declaring the queue in both programs.channel.queue_declare(queue='balance')def callback(ch, method, properties, body): print(" [x] Received %r" % body)channel.basic_consume(callback, queue='balance', no_ack=True)print(' [*] Waiting for messages. To exit press CTRL+C')channel.start_consuming()