您的位置:首页 > 编程语言 > Python开发

rabbitmq的安装和命令介绍及python程序模拟生产者和消费者

2016-05-02 14:09 711 查看
介绍
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议
RabbitMQ是流行的开源消息队列系统,用erlang语言开发
RabbitMQ是AMQP(高级消息队列协议)的标准实现
官网:http://www.rabbitmq.com/

安装
方式:yum/rpm
系统环境
[root@log_server scripts]# ifconfig | sed -n 's#.*inet addr:\(.*\) B.*#\1#gp'
192.168.100.20
[root@log_server scripts]# cat /etc/issue | grep -i cent
CentOS release 6.4 (Final)
[root@log_server scripts]# yum repolist |grep epel
* epel: mirrors.aliyun.com
epel Extra Packages for Enterprise Linux 6 - x86_64 12,244
[root@log_server scripts]#
yum install -y  rabbitmq-server
安装成功后,查看插件列表
/usr/lib/rabbitmq/bin/rabbitmq-plugins list
启动rabbitmq_management插件
/usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
启动程序

/etc/init.d/rabbitmq-server start




验证
[root@log_server scripts]# netstat -tulnp |grep 15672
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3877/beam.smp
[root@log_server scripts]# ps -ef |grep rabbit
root 3837 1 0 11:30 pts/2 00:00:00 /bin/sh/etc/init.d/rabbitmq-server start
root 3868 3837 0 11:30 pts/2 00:00:00 /bin/bash -c ulimit -S -c 0 >/dev/null 2>&1 ; /usr/sbin/rabbitmq-server
root 3869 3868 0 11:30 pts/2 00:00:00 /bin/sh /usr/sbin/rabbitmq-server
root 3876 3869 0 11:30 pts/2 00:00:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 3877 3876 0 11:30 ? 00:00:55 /usr/lib64/erlang/erts-5.8.5/bin/beam.smp -W w -K true -A30 -P 1048576 -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.1.5/sbin/../ebin -noshell -noinput -s rabbit boot -sname rabbit@log_server -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@log_server.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@log_server-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.1.5/sbin/../plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@log_server-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@log_server"
rabbitmq 3951 3877 0 11:30 ? 00:00:00 inet_gethost 4
rabbitmq 3952 3951 0 11:30 ? 00:00:00 inet_gethost 4
root 19143 1770 0 14:02 pts/2 00:00:00 grep rabbit
[root@log_server scripts]#


配置
默认配置不需要配置

如果有需要/etc/rabbitmq/ 下可以创建

常见命令
查看列队列表
rabbitmqctl list_queues
查看默认 vhost为 "/"下的列队
rabbitmqctl list_queues -p "/"
查看vhost列表
rabbitmqctl list_vhosts
增加/删除vhost
rabbitmqctl add_vhost vhost_andy
rabbitmqctl delete_vhost <VHostPath>
增加用户
rabbitmqctl add_user andy '12qwaszx'
查看用户

rabbitmqctl list_users

[root@log_server scripts]# rabbitmqctl  list_users
Listing users ...
andy    []
guest   [administrator]
将用户绑定到vhost_andy 这个vhost上,并赋予对<conf> <write> <read>的权限,用户是对某个vhost的管理权限关联起来的
rabbitmqctl set_permissions -p vhost_andy andy ".*" ".*" ".*"
查看用户权限
rabbitmqctl  list_user_permissions andy
[root@log_server scripts]# rabbitmqctl list_user_permissions andy
Listing permissions for user "andy" ...
vhost_andy .* .* .*
...done.

web管理连接
http://192.168.100.20:15672/
guest/guest登入





python程序模拟生产者

参考官网:http://www.rabbitmq.com/tutorials/tutorial-one-python.html

来自最简单的模型P-mq--C




cat send.py
#!/usr/bin/env python
#encoding==utf-8
#http://cuidehua.blog.51cto.com
#from http://www.rabbitmq.com/tutorials/tutorial-one-python.html import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

模拟发送100条消息到queue=hello的这个列队中
for i in {1..100};do python send.py ;done


命令查看队列:(不指定vhost 列队在默认的vhost "/"中)
[root@log_server scripts]# rabbitmqctl list_queues -p "/"
Listing queues ...
hello 100
...done.

web端查看




python程序模拟消费者进行消费
[root@log_server scripts]# cat receive.py
#!/usr/bin/env python
#encoding=utf-8
#http://cuidehua.blog.51cto.com
#from http://www.rabbitmq.com/tutorials/tutorial-one-python.html 
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

#定义回调函数
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)

channel.basic_consume(callback,
queue='hello',
no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()


执行消费,并等待messages! 直到ctrl+c 终止
python receive.py


再次查看队列
[root@log_server scripts]# rabbitmqctl list_queues -p "/"
Listing queues ...
hello 0
...done.

【常见启动报错】

/etc/init.d/rabbitmq-server 脚本调用的是 rabbitmqctl

常见问题:
root@log_server scripts]# /etc/init.d/rabbitmq-server stop
Stopping rabbitmq-server: RabbitMQ is not running
rabbitmq-server.
[root@log_server scripts]# rabbitmqctl stop
Stopping and halting node rabbit@log_server ...
Error: unable to connect to node rabbit@log_server: nodedown

DIAGNOSTICS
===========

nodes in question: [rabbit@log_server]

hosts, their running nodes and ports:
- unable to connect to epmd on log_server: address (cannot connect to host/port)

current node details:
- node name: rabbitmqctl23829@log_server
- home dir: /var/lib/rabbitmq
- cookie hash: UtT55njEKHItAnmjDeoV+A==

解释原因:
分析 rabbitmqctl --help 中的第一段
Usage:
rabbitmqctl [-n <node>] [-q] <command> [<command options>]

Options:
-n node
-q

Default node is "rabbit@server", where server is the local host. On a host
named "server.example.com", the node name of the RabbitMQ Erlang node will
usually be rabbit@server (unless RABBITMQ_NODENAME has been set to some
non-default value at broker startup time). The output of hostname -s is usually
the correct suffix to use after the "@" sign. See rabbitmq-server(1) for
details of configuring the RabbitMQ broker.

Quiet output mode is selected with the "-q" flag. Informational messages are
suppressed when quiet mode is in effect.

Commands:
stop [<pid_file>]
stop_app
start_app
wait <pid_file>
reset
force_reset
rotate_logs <suffix>

原因是不能解析 本主机,原因是 默认作为解析的节点和 本机的hostname -s 结果一致

解决方法:只要把hostname -s的结果 加入到/etc/hosts中即可
[root@log_server ~]# hostname -s
log_server

正常停止的结果:

[root@log_server ~]# rabbitmqctl stop
Stopping and halting node rabbit@log_server ...
...done.

正常启动的结果:
[root@log_server ~]#/etc/init.d/rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.

可以查看的日志
/var/log/rabbit/
本文出自 “崔德华运维打工从业路” 博客,请务必保留此出处http://cuidehua.blog.51cto.com/5449828/1769460
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: