【介绍】
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
RabbitMQ是流行的开源消息队列系统,用erlang语言开发
RabbitMQ是AMQP(高级消息队列协议)的标准实现
【安装】
方式:yum/rpm
系统环境
[[email protected]_server scripts]# ifconfig | sed -n ‘s#.*inet addr:\(.*\) B.*#\1#gp‘
192.168.100.20
[[email protected]_server scripts]# cat /etc/issue | grep -i cent
CentOS release 6.4 (Final)
[[email protected]_server scripts]# yum repolist |grep epel
* epel: mirrors.aliyun.com
epel Extra Packages for Enterprise Linux 6 - x86_64 12,244
[[email protected]_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
【验证】
[[email protected]_server scripts]# netstat -tulnp |grep 15672 tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3877/beam.smp [[email protected]_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 [email protected]_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/[email protected]_server.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/[email protected]_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/[email protected]_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/[email protected]_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 [[email protected]_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 [[email protected]_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
[[email protected]_server scripts]# rabbitmqctl list_user_permissions andy
Listing permissions for user "andy" ...
vhost_andy .* .* .*
...done.
【web管理连接】
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 "/"中)
[[email protected]_server scripts]# rabbitmqctl list_queues -p "/"
Listing queues ...
hello 100
...done.
web端查看
【python程序模拟消费者进行消费】
[[email protected]_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
再次查看队列
[[email protected]_server scripts]# rabbitmqctl list_queues -p "/"
Listing queues ...
hello 0
...done.