centos 7
vi /etc/yum.repos.d/rabbitmq-erlang.repo
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/21/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
yum install erlang socat -y
rpm -Uvh https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.18/rabbitmq-server-3.7.18-1.el7.noarch.rpm
systemctl start rabbitmq-server
systemctl enable rabbitmq-server
systemctl status rabbitmq-server
安装ui插件
rabbitmq-plugins enable rabbitmq_management
chown -R rabbitmq:rabbitmq /var/lib/rabbitmq/
创建账号授权
rabbitmqctl add_user admin rabbitmq
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
systemctl restart rabbitmq-server
关闭防火墙
iptables -F
http://ip:15672 输入账号密码 admin/rabbitmq
生产者
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pika
#连上rabbitMQ
connection=pika.BlockingConnection(pika.ConnectionParameters(‘localhost‘))
channel=connection.channel()
#声明queue
channel.queue_declare(queue=‘hello1‘)
#n RabbitMQ a message can never be sent directly to the queue,it always needs to go through an exchange.
#向队列里发数据
import json
data = json.dumps({"a":1})
channel.basic_publish(exchange=‘‘,
routing_key=‘hello1‘,
body=data)
print("[x]Sent‘HelloWorld!‘")
connection.close()
消费者
import poka
connection = pika.BlockingConnection(pika.ConnectionParameters(host=‘127.0.0.1‘, port=5672))
channel = connection.channel()
channel.queue_declare(queue=‘hello1‘)
#回调函数
def callback(ch, method, properties, body):
def callback(ch, method, properties, body):
channel.basic_consume(queue="hello1",auto_ack=True,on_message_callback=callback)
#监听
channel.start_consuming()
原文:https://www.cnblogs.com/gooooodmorning/p/13492749.html