面试官:如何用rabbitmqctl来管理和监控RabbitMQ?

概述

MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。

rabbitmq的管理功能最全的就是rabbitmqctl命令了,当然还有HTTP API和UI两种管理手段。


一、rabbitmqctl语法

rabbitmqctl [-n <node>] [-q] <command> [<command>]/<command>/<command>/<node>

-n node 默认node名称是"rabbit@server",如果你的主机明是'server.example.com',那么node名称是'rabbit@server'。

-q 安静输出模式,信息会被禁止输出


二、命令用法

1、基本的管理功能

<code>join_cluster <clusternode> [--ram]      #clusternode表示node名称,--ram表示node以ram node加入集群中。默认node以disc node加入集群,在一个node加入cluster之前,必须先停止该node的rabbitmq应用,即先执行stop_app。cluster_status      #显示cluster中的所有nodechange_cluster_node_type disc | ram      #改变一个cluster中node的模式,该节点在转换前必须先停止,不能把一个集群中唯一的disk node转化为ram nodeforget_cluster_node [--offline]      #远程移除cluster中的一个node,前提是该node必须处于offline状态,如果是online状态,则需要加--offline参数。update_cluster_nodes clusternode       #sync_queue queue      #同步镜像队列cancel_sync_queue queue/<clusternode>/<code>


2、cluster管理

<code>add_user <username> <password>      #在rabbitmq的内部数据库添加用户delete_user <username>      #删除一个用户change_password <username> <newpassword>      #改变用户密码  \\\\改变web管理登陆密码clear_password <username>     #清除用户密码,禁止用户登录set_user_tags <username>  
... #设置用户tagslist_users #列出用户add_vhost <vhostpath> #创建一个vhostsdelete_vhost <vhostpath> #删除一个vhostslist_vhosts [<vhostinfoitem> ...] #列出vhostsset_permissions [-p <vhostpath>] <user> <conf> <write> <read> #针对一个vhosts 给用户赋予相关权限clear_permissions [-p <vhostpath>] <username> #清除一个用户对vhosts的权限list_permissions [-p <vhostpath>] #列出哪些用户可以访问该vhostslist_user_permissions <username> #列出该用户的访问权限set_parameter [-p <vhostpath>] <component> <name> <value> #clear_parameter [-p <vhostpath>] <component> #list_parameters [-p <vhostpath>] #/<vhostpath>/<component>/<vhostpath>/<value>/<name>/<component>/<vhostpath>/<username>/<vhostpath>/<username>/<vhostpath>/<read>/<write>/<conf>/<user>/<vhostpath>/<vhostinfoitem>/<vhostpath>/<vhostpath>/<username>/<username>/<newpassword>/<username>/<username>/<password>/<username>/<code>


3、用户管理

<code>set_policy [-p <vhostpath>] [--priority <priority>] [--apply-to <apply-to>]  <name> <pattern> <definition>        #name 策略名称,pattern  正则表达式,用来匹配资源,符合的就会应用设置的策略,apply-to 表示策略应用到什么类型的地方,一般有queues、exchange和all,默认是all。priority 是个整数优先级,definition 是json格式设置的策略。clear_policy [-p <vhostpath>] <name>      #清除一个策略list_policies [-p <vhostpath>]      #列出已有的策略/<vhostpath>/<name>/<vhostpath>/<definition>/<pattern>/<name>/<apply-to>/<priority>/<vhostpath>/<code>


4、policy管理,策略用来控制和修改queues和exchange在集群中的行为,策略可以应用到vhost

<code>set_policy [-p <vhostpath>] [--priority <priority>] [--apply-to <apply-to>]  <name> <pattern> <definition>        #name 策略名称,pattern  正则表达式,用来匹配资源,符合的就会应用设置的策略,apply-to 表示策略应用到什么类型的地方,一般有queues、exchange和all,默认是all。priority 是个整数优先级,definition 是json格式设置的策略。clear_policy [-p <vhostpath>] <name>      #清除一个策略list_policies [-p <vhostpath>]      #列出已有的策略/<vhostpath>/<name>/<vhostpath>/<definition>/<pattern>/<name>/<apply-to>/<priority>/<vhostpath>/<code>


5、queues && exchange状态信息

<code>list_queues [-p <vhostpath>] [<queueinfoitem> ...]      #返回queue的信息,如果省略了-p参数,则默认显示的是"/"vhosts的信息。list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...]      #返回exchange的信息。list_bindings [-p <vhostpath>] [<bindinginfoitem> ...]     #返回绑定信息。list_connections [<connectioninfoitem> ...]      #返回链接信息。list_channels [<channelinfoitem> ...]      #返回目前所有的channels。list_consumers [-p <vhostpath>]      #返回consumers,status      #显示broker的状态environment      #显示环境参数的信息report      #返回一个服务状态report,eval <expr>/<vhostpath>/<channelinfoitem>/<connectioninfoitem>/<bindinginfoitem>/<vhostpath>/<exchangeinfoitem>/<vhostpath>/<queueinfoitem>/<vhostpath>/<code>



三、常用监控命令

1、查看虚拟主机

rabbitmqctl list_vhosts

2、查看队列

rabbitmqctl list_queues

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


3、查看exchanges

rabbitmqctl list_exchanges

4、查看用户

rabbitmqctl list_users

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


5、查看连接

rabbitmqctl list_connections

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?

6、查看消费者信息

rabbitmqctl list_consumers

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?

7、查看环境变量

rabbitmqctl environment

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


8、查看未被确认的队列

rabbitmqctl list_queues name messages_unacknowledged

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


9、查看单个队列的内存使用

rabbitmqctl list_queues name memory

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


10、查看准备就绪的队列

rabbitmqctl list_queues name messages_ready

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?



觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~

面试官:如何用rabbitmqctl来管理和监控RabbitMQ?


分享到:


相關文章: