1.服务器安装
参考链接:http://www.cnblogs.com/pangguoping/p/5720134.html
# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
yum install epel-release -y
yum install socat -y
yum install erlang -y
rpm -ivh rabbitmq-server-3.6.10-1.el7.noarch.rpm
systemctl start rabbitmq-server
systemctl restart rabbitmq-server
systemctl stop rabbitmq-server
firewall-cmd --add-port=15672/tcp --permanent #打开15672端口
firewall-cmd --add-port=5672/tcp --permanent #打开5672端口
@H_404_41@2.设置用户
参考链接:http://www.cnblogs.com/zhen-rh/p/6862350.html
rabbitmqctl add_user admin admin #用户名密码
rabbitmqctl set_user_tags admin administrator #权限
rabbitmqctl list_users
rabbitmq-plugins enable rabbitmq_management #网络管理
rabbitmqctl set_permissions -p '/' admin ".*" ".*" ".*" #vhost
http://192.168.154.153:15672 #网页访问
3.Mac编译安装python 2.7.13(windows/linux略)
3.1安装python2.7.13 –with-brewed-openssl
参考链接:https://github.com/Homebrew/legacy-homebrew/issues/22816
brew install openssl
brew link openssl --force
brew uninstall python
brew install python --with-brewed-openssl
brew link --overwrite python
3.2修改pip配置
参考链接:https://www.zhihu.com/question/30941329/answer/151221304
编辑环境变量:
vi ~/.bash_profile
添加新行:
alias pip="/usr/local/bin/python /usr/local/lib/python2.7/site-packages/pip"
立即生效:
source ~/.bash_profile
4.Python测试
参考链接:http://www.cnblogs.com/pangguoping/p/5720134.html
pip install pika
# coding:utf8
import pika
# ############################## 生产者 ##############################
credentials = pika.PlainCredentials('admin','admin')
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.154.153',5672,'/',credentials))
# 创建频道
channel = connection.channel()
# 声明消息队列,消息将在这个队列中进行传递。如果将消息发送到不存在的队列,rabbitmq将会自动清除这些消息。如果队列不存在,则创建
channel.queue_declare(queue='hello')
# exchange -- 它使我们能够确切地指定消息应该到哪个队列去。
# 向队列插入数值 routing_key是队列名 body是要插入的内容
i=0
while i<20:
i+=1
channel.basic_publish(exchange='',routing_key='hello',body='Hello World! i={}'.format(i))
print("开始队列")
# 缓冲区已经flush而且消息已经确认发送到了RabbitMQ中,关闭链接
connection.close()
# coding:utf8
import pika
import time
# ########################### 消费者 ###########################
credentials = pika.PlainCredentials('admin',credentials))
channel = connection.channel()
# 声明消息队列,消息将在这个队列中进行传递。如果队列不存在,则创建
# channel.queue_declare(queue='test')
# 定义一个回调函数来处理,这边的回调函数就是将信息打印出来。
def callback(ch,method,properties,body):
time.sleep(0.5) ##休眠0.5s
print(" [x] Received %r" % body)
# 告诉rabbitmq使用callback来接收信息
channel.basic_consume(callback,queue='hello',no_ack=True)
# no_ack=True表示在回调函数中不需要发送确认标识
print(' [*] Waiting for messages. To exit press CTRL+C')
# 开始接收信息,并进入阻塞状态,队列里有信息才会调用callback进行处理。按ctrl+c退出。
channel.start_consuming()