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版Serverless系列实例进行获取接入点、创建Topic、创建订阅组、收发消息、查看消息轨迹和仪表盘。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
Linux 计算机视觉 C++
【解决方案】Building wheel for opencv-python:安装卡顿的原因与解决方案
当你安装OpenCV时,命令行停在Building wheel for opencv-python (PEP 517) ... -似乎卡住了。这并非程序假死,而是其编译耗时巨大。本文将揭示原因,并提供优化安装体验的实用方法。
1402 88
|
11月前
|
人工智能 Linux 开发工具
Python从零到一:手把手带你写出第一个实用程序
Python语法简洁易懂,适合编程新手入门。它广泛应用于人工智能、自动化办公、Web开发等领域。学习Python可快速搭建项目,拥有丰富库支持和强大社区资源。通过本教程,你将掌握基础语法、环境搭建、程序逻辑控制及实战项目开发,开启编程之旅。
1407 0
|
10月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
1567 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
10月前
|
设计模式 决策智能 Python
Python条件控制:让程序学会"思考"的魔法
本文深入浅出地讲解Python条件控制,从基础if语句到多分支、嵌套结构,再到简洁的三元表达式与Python 3.10新增的match-case模式匹配,结合电商折扣、会员等级、ATM系统等实战案例,全面掌握程序“智能决策”的核心逻辑。
626 0
|
IDE 开发工具 开发者
手把手教你安装PyCharm 2025:开发者的Python IDE配置全流程+避坑指南
本教程详细介绍了PyCharm 2025版本在Windows系统下的安装流程及配置方法,涵盖AI代码补全与智能调试工具链等新功能。内容包括系统要求、安装步骤、首次运行配置(如主题选择与插件安装)、创建首个Python项目,以及常见问题解决方法。此外,还提供了切换中文界面和延伸学习资源的指导,帮助用户快速上手并高效使用PyCharm进行开发。
6447 61
|
人工智能 数据挖掘 Linux
Centos安装Python3.7(亲测可用)
本指南详细介绍了在基于Linux(以CentOS系统为例,使用yum包管理器)的系统上安装Python 3.7版本的完整流程。Python是一种广泛使用的高级编程语言,在各种领域如软件开发、数据分析、人工智能和区块链开发等都有着重要的应用。
929 2
|
物联网 Linux 开发者
快速部署自己私有MQTT-Broker-下载安装到运行不到一分钟,快速简单且易于集成到自己项目中
本文给物联网开发的朋友推荐的是GMQT,让物联网开发者快速拥有合适自己的MQTT-Broker,本文从下载程序到安装部署手把手教大家安装用上私有化MQTT服务器。
2432 5
|
人工智能 并行计算 开发者
CUDA重大更新:原生Python可直接编写高性能GPU程序
NVIDIA在2025年GTC大会上宣布CUDA并行计算平台正式支持原生Python编程,消除了Python开发者进入GPU加速领域的技术壁垒。这一突破通过重新设计CUDA开发模型,引入CUDA Core、cuPyNumeric、NVMath Python等核心组件,实现了Python与GPU加速的深度集成。开发者可直接用Python语法进行高性能并行计算,显著降低门槛,扩展CUDA生态,推动人工智能、科学计算等领域创新。此更新标志着CUDA向更包容的语言生态系统转型,未来还将支持Rust、Julia等语言。
915 3
CUDA重大更新:原生Python可直接编写高性能GPU程序
|
PyTorch 算法框架/工具 C++
人工智能算法python程序运行环境安装步骤整理
本教程详细介绍Python与AI开发环境的配置步骤,涵盖软件下载、VS2017安装、Anaconda配置、PyCharm设置及组件安装等内容,适用于Windows系统,助你快速搭建开发环境。

推荐镜像

更多