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

简介:

介绍

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]# 

1
yum  install  -y  rabbitmq-server

安装成功后,查看插件列表

1
/usr/lib/rabbitmq/bin/rabbitmq-plugins  list

启动rabbitmq_management插件

1
/usr/lib/rabbitmq/bin/rabbitmq-plugins  enable  rabbitmq_management

启动程序

1
  /etc/init .d /rabbitmq-server  start

wKioL1cm7eCgICsUAACkuf38UZc687.png


验证

1
2
3
4
5
6
7
8
9
10
11
12
[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/ 下可以创建



常见命令

查看列队列表

1
rabbitmqctl list_queues

查看默认 vhost为 "/"下的列队

1
rabbitmqctl list_queues -p  "/"

查看vhost列表

1
rabbitmqctl list_vhosts

增加/删除vhost

1
rabbitmqctl add_vhost vhost_andy
1
rabbitmqctl delete_vhost <VHostPath>

增加用户

1
rabbitmqctl add_user andy  '12qwaszx'

查看用户

1
2
3
4
5
6
rabbitmqctl list_users
 
[root@log_server scripts] # rabbitmqctl  list_users
Listing  users  ...
andy    []
guest   [administrator]

将用户绑定到vhost_andy 这个vhost上,并赋予对<conf> <write> <read>的权限,用户是对某个vhost的管理权限关联起来的

1
rabbitmqctl set_permissions -p vhost_andy andy  ".*"  ".*"  ".*"

查看用户权限

1
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登入


wKiom1cm8APyAnHGAACKr32lUtw375.png




python程序模拟生产者

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


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

wKioL1cm-YvSTP2wAAAiVPxc1zo857.png




cat send.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/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的这个列队中

1
for  in  {1..100}; do  python send.py ; done


命令查看队列:(不指定vhost 列队在默认的vhost "/"中)

[root@log_server scripts]# rabbitmqctl list_queues -p  "/"

Listing queues ...

hello   100

...done.


web端查看

wKiom1cm-dmiPi7mAABezq7IVCk156.png



python程序模拟消费者进行消费

[root@log_server scripts]# cat receive.py 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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 终止

1
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/

本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1769460如需转载请自行联系原作者


cuizhiliang

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
8天前
|
Linux 网络安全 Python
linux centos上安装python3.11.x详细完整教程
这篇文章提供了在CentOS系统上安装Python 3.11.x版本的详细步骤,包括下载、解压、安装依赖、编译配置、解决常见错误以及版本验证。
64 1
linux centos上安装python3.11.x详细完整教程
|
15天前
|
项目管理 Python
如何在Mac上安装多个Python环境
在你的Mac上使用多个Python环境可以对项目管理很有帮助,特别是在同时处理不同Python版本或不同的包需求时。在这篇文章中,我们将向你展示如何在Mac上轻松地安装和管理多个Python环境。
25 5
 如何在Mac上安装多个Python环境
|
8天前
|
NoSQL Linux Redis
linux安装单机版redis详细步骤,及python连接redis案例
这篇文章提供了在Linux系统中安装单机版Redis的详细步骤,并展示了如何配置Redis为systemctl启动,以及使用Python连接Redis进行数据操作的案例。
20 2
|
8天前
|
Linux 编译器 开发工具
快速在linux上配置python3.x的环境以及可能报错的解决方案(python其它版本可同样方式安装)
这篇文章介绍了在Linux系统上配置Python 3.x环境的步骤,包括安装系统依赖、下载和解压Python源码、编译安装、修改环境变量,以及常见安装错误的解决方案。
20 1
|
5天前
|
机器学习/深度学习 Linux 开发者
Python必备工具:pip的安装与管理
Python必备工具:pip的安装与管理
13 0
|
6天前
|
Python
7-14|salt之安装Python模块
7-14|salt之安装Python模块
|
6天前
|
Python
7-9|Python如何安装ruamel.yaml
7-9|Python如何安装ruamel.yaml
|
7天前
|
Python Windows
安装python 以及 安装 pycharm
安装python 以及 安装 pycharm
|
8天前
|
Linux Python
Linux之centos安装clinkhouse以及python如何连接
Linux之centos安装clinkhouse以及python如何连接
|
8天前
|
Linux Python
linux之安装python3
linux之安装python3
下一篇
无影云桌面