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一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
1天前
|
5G Python
Windows11搭建Python环境(Anaconda安装与使用)
Windows11搭建Python环境(Anaconda安装与使用)
9 0
|
5天前
|
数据采集 JavaScript 前端开发
使用Python打造爬虫程序之破茧而出:Python爬虫遭遇反爬虫机制及应对策略
【4月更文挑战第19天】本文探讨了Python爬虫应对反爬虫机制的策略。常见的反爬虫机制包括User-Agent检测、IP限制、动态加载内容、验证码验证和Cookie跟踪。应对策略包括设置合理User-Agent、使用代理IP、处理动态加载内容、验证码识别及维护Cookie。此外,还提到高级策略如降低请求频率、模拟人类行为、分布式爬虫和学习网站规则。开发者需不断学习新策略,同时遵守规则和法律法规,确保爬虫的稳定性和合法性。
|
6天前
|
SQL 安全 Go
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
在Python Web开发中,确保应用安全至关重要,主要防范SQL注入、XSS和CSRF攻击。措施包括:使用参数化查询或ORM防止SQL注入;过滤与转义用户输入抵御XSS;添加CSRF令牌抵挡CSRF;启用HTTPS保障数据传输安全;实现强身份验证和授权系统;智能处理错误信息;定期更新及审计以修复漏洞;严格输入验证;并培训开发者提升安全意识。持续关注和改进是保证安全的关键。
14 0
|
9天前
|
消息中间件 供应链 Java
RabbitMQ入门指南(九):消费者可靠性
RabbitMQ是一个高效、可靠的开源消息队列系统,广泛用于软件开发、数据传输、微服务等领域。本文主要介绍了消费者确认机制、失败重试机制、失败处理策略、业务幂等性等内容。
23 0
RabbitMQ入门指南(九):消费者可靠性
|
13天前
|
Ubuntu Python
python3安装clickhouse_sqlalchemy(greenlet) 失败
如果上述方法仍然无法解决问题,建议查阅相关错误信息和官方文档,以获取更详细的帮助。确保你的Python环境和依赖库都在最新版本,有时问题可能会因为版本不兼容而导致安装失败。
18 0
|
15天前
|
数据采集 自然语言处理 搜索推荐
一篇博客带你领略学习Python的第三方库---如何获取和安装第三方库,关于三种常见第三方库的下载和讲解(pyinstall库,jieba库,wordcloud库),更多第三方库的分类介绍
一篇博客带你领略学习Python的第三方库---如何获取和安装第三方库,关于三种常见第三方库的下载和讲解(pyinstall库,jieba库,wordcloud库),更多第三方库的分类介绍
|
17天前
|
数据采集 机器学习/深度学习 人工智能
Python环境搭建—安装Python3解释器
Python环境搭建—安装Python3解释器
33 2
|
17天前
|
数据采集 JavaScript C++
Python搭建编程环境-安装Python3解释器
Python搭建编程环境-安装Python3解释器
24 1
|
18天前
|
Ubuntu Python
WSL ubuntu 20.04安装python3
通过以上步骤,你可以在WSL的Ubuntu 20.04上成功安装Python 3。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
19 1
|
18天前
|
Ubuntu Python
ubuntu安装python3.9
通过以上步骤,你可以在Ubuntu上成功安装Python 3.9。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
22 1