Python学习之RabbitMQ队列(一)
RabbitMQ是流行的开源消息队列系统,用erlang语言开发。[*] 安装(Windows):
Windows:
首先需要安装 Erlang环境
官网:
http://www.erlang.org/
Windows版下载地址:http://www.erlang.org/download/otp_win64_17.3.exe
Linux版: 使用yum安装
安装模块 pip install pika
Linux(centos7):
安装都是需要Erlang环境的。
wget
rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
rpm --import http://packages.erlang-solutions.com/rpm/erlang_solutions.asc
yum install erlang
erlang 安装完成后,开始安装rabbitmq。
从官网下载(http://www.rabbitmq.com)rpm包;
rpm -ivh rabbitmq-server-3.4.1-1.noarch.rpm#安装
service rabbitmq-server start#启动
chkconfig rabbitmq-server on #开机自启动
#编辑配置文件
cp /usr/share/doc/rabbitmq-server-3.4.1/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
vim /etc/rabbitmq/rabbitmq.config
%% The default "guest" user is only permitted to access the server
%% via a loopback interface (e.g. localhost).
%% {loopback_users, [<<"guest">>]},
%%
%% Uncomment the following line if you want to allow access to the
%% guest user from anywhere on the network.
{loopback_users, []}#将改行取消注释,即允许远程用户访问。
开启web界面管理工具
rabbitmq-plugins enable rabbitmq_management#开启web界面管理工具
systemctl restartrabbitmq-server.service #重启服务
界面如图所示,默认端口为15672.
默认用户名和密码均为 guest
默认网页是不允许访问的,需要增加一个用户修改一下权限,代码如下:
添加用户:rabbitmqctl add_user tanx tanx
添加权限:rabbitmqctl set_permissions -p "/" tanx ".*" ".*" ".*"
修改用户角色rabbitmqctl set_user_tags tanx administrator
然后就可以远程访问了,然后可直接配置用户权限等信息。
set_permissions [-p vhost] {user} {conf} {write} {read}
vhost
The name of the virtual host to which to grant the user access, defaulting to /.
user
The name of the user to grant access to the specified virtual host.
conf
A regular expression matching resource names for which the user is granted configure permissions.
write
A regular expression matching resource names for which the user is granted write permissions.
read
A regular expression matching resource names for which the user is granted read permissions.
rabbitmq常用命令
add_user <UserName> <Password>
delete_user <UserName>
change_password <UserName> <NewPassword>
list_users
add_vhost <VHostPath>
delete_vhost <VHostPath>
list_vhostsset_permissions [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>
clear_permissions [-p <VHostPath>] <UserName>
list_permissions[-p <VHostPath>]
list_user_permissions <UserName>
list_queues [-p <VHostPath>] [<QueueInfoItem> ...]
list_exchanges [-p <VHostPath>] [<ExchangeInfoItem> ...]
list_bindings[-p <VHostPath>]
list_connections [<ConnectionInfoItem> ...]
2.python代码
先来一段简单的代码实现
producter端
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
#声明queue
channel.queue_declare(queue='hello')
#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='hello',
body='Hello World!')
print(" Sent 'Hello World!'")
connection.close()
customer端
#_*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
#You may ask why we declare the queue againwe 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='hello')
def callback(ch, method, properties, body):
print(" Received %r" % body)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
print('
[*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
页:
[1]