一、rabbitMq安装
转载自:https://blog.csdn.net/weixin_39735923/article/details/79288578?utm_source=copy
二、使用python发送mq
前提:已安装python和依赖包rabbitpy
import rabbitpy
def rabbitMqDemo(url,exchangeName,queueName,routingKey,message):
#连接rabbitmq
connection = rabbitpy.Connection(url)
channel= connection.channel()
#声明交换器
exchange = rabbitpy.Exchange(channel,exchangeName)
exchange.declare()
#声明队列
queue = rabbitpy.Queue(channel,queueName)
queue.declare()
#绑定交换器
queue.bind(exchange,routingKey)
#发送消息
message = rabbitpy.Message(channel,message,{'content_type':'text/plain'},opinionated=True)
message.publish(exchange,routingKey)
connection.close()
print("发送mq完毕")
if __name__ =="__main__":
url="amqp://guest:guest@localhost:5672/%2F"
exchangeName= "chapter1"
queueName="queue_example"
routingKey="example-routing-key"
message ="hello mq!"
rabbitMqDemo(url,exchangeName,queueName,routingKey,message)
运行结果
在页面:http://localhost:15672/#/queues,可以看到新增的消息