记一次删除 RabbitMQ 消息的经历

前因

在公司最新的项目上,由咱们技术总监搭了一套框架,其中有使用RabbitMq来监听监听写事件,这也是我第一次使用RabbitMq。由于在开发的过程当中,有些事件会由于代码的不完善消费失败,而后就会一直消费,而后一直报错。当bug修复,把正确的代码上传到开发环境,原先遗留的问题消息任然存在,而且会阻塞队列,让以后的正常的消息没法让系统正常的消费。html

网页删除

想要删除队列中的消息,是能够经过RabbitMq的网页来操做的。登陆上RabbitMQ Management以后,在 Queues 中打开指定的队列:node

clipboard.png

而后在下方的 Delete / purge 中点击 Delete 或者是 purge 均可以达到效果。linux

clipboard.png

可是这种方式存在一个问题,就是当消息多的时候点击,页面会卡死。即便没有卡死也会出现以下对话框:git

clipboard.png

大概意思是找不到这个持久队列,主节点已经关闭或者是没法访问。github

如今只是小白阶段,而后有点搞不懂,网上也没有搜索到什么有用的答案。只找到一句话,来自:https://blog.csdn.net/u011973...web

若是队列的主节点不可用,则非镜像队列的行为取决于其持久性。在节点恢复正常以前,持久化队列将不可用。在集群中其余节点上对主节点不可用的持久化队列进行任何操做都将失败

没有深究服务器

重置RabbitMQ

而后我就去网上百度了一下,如何使用命令操做,而后找到了下面的解决方法:app

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app

执行了一次以后确实是有效的。重启的RabbitMq,而后里面的消息也确实清空了。框架

而后我当上述的状况再次发生,须要清空消息,这样个时候我不想用这样一看就很粗暴的方式来操做,并且这种方式不只会清空消息,还会清空全部配置信息。因此此次我想要先清除指定队列的消息便可。less

删除指定的队列

而后我找到了这个方法:

sudo rabbitmqctl purge_queue ${queue_name}
或
rabbitmqctl purge_queue ${queue_name}
或
rabbitmqctl -p ${vhostpath} purge_queue ${queue_name}

执行报错,说是找不到这个命令,给出的信息以下:

Error: could not recognise command
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>

    join_cluster <clusternode> [--ram]
    cluster_status
    change_cluster_node_type disc | ram
    forget_cluster_node [--offline]
    update_cluster_nodes clusternode
    sync_queue queue
    cancel_sync_queue queue
    set_cluster_name name

    add_user <username> <password>
    delete_user <username>
    change_password <username> <newpassword>
    clear_password <username>
    set_user_tags <username> <tag> ...
    list_users

    add_vhost <vhostpath>
    delete_vhost <vhostpath>
    list_vhosts [<vhostinfoitem> ...]
    set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
    clear_permissions [-p <vhostpath>] <username>
    list_permissions [-p <vhostpath>]
    list_user_permissions <username>

    set_parameter [-p <vhostpath>] <component_name> <name> <value>
    clear_parameter [-p <vhostpath>] <component_name> <key>
    list_parameters [-p <vhostpath>]

    set_policy [-p <vhostpath>] [--priority <priority>] [--apply-to <apply-to>] 
<name> <pattern>  <definition>
    clear_policy [-p <vhostpath>] <name>
    list_policies [-p <vhostpath>]

    list_queues [-p <vhostpath>] [<queueinfoitem> ...]
    list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...]
    list_bindings [-p <vhostpath>] [<bindinginfoitem> ...]
    list_connections [<connectioninfoitem> ...]
    list_channels [<channelinfoitem> ...]
    list_consumers [-p <vhostpath>]
    status
    environment
    report
    eval <expr>

    close_connection <connectionpid> <explanation>
    trace_on [-p <vhost>]
    trace_off [-p <vhost>]
    set_vm_memory_high_watermark <fraction>

<vhostinfoitem> must be a member of the list [name, tracing].

The list_queues, list_exchanges and list_bindings commands accept an optional 
virtual host parameter for which to display results. The default value is "/".

<queueinfoitem> must be a member of the list [name, durable, auto_delete, 
arguments, policy, pid, owner_pid, exclusive_consumer_pid, 
exclusive_consumer_tag, messages_ready, messages_unacknowledged, messages, 
consumers, consumer_utilisation, memory, slave_pids, synchronised_slave_pids, 
status].

<exchangeinfoitem> must be a member of the list [name, type, durable, 
auto_delete, internal, arguments, policy].

<bindinginfoitem> must be a member of the list [source_name, source_kind, 
destination_name, destination_kind, routing_key, arguments].

<connectioninfoitem> must be a member of the list [pid, name, port, host, 
peer_port, peer_host, ssl, ssl_protocol, ssl_key_exchange, ssl_cipher, 
ssl_hash, peer_cert_subject, peer_cert_issuer, peer_cert_validity, state, 
channels, protocol, auth_mechanism, user, vhost, timeout, frame_max, 
channel_max, client_properties, recv_oct, recv_cnt, send_oct, send_cnt, 
send_pend].

<channelinfoitem> must be a member of the list [pid, connection, name, number, 
user, vhost, transactional, confirm, consumer_count, messages_unacknowledged, 
messages_uncommitted, acks_uncommitted, messages_unconfirmed, prefetch_count, 
global_prefetch_count].

实际上使用 rabbitmqctl -help 就能获得这些内容。

能够看的出来,这里面已经 rabbitmqctl 所支持的命令都列了出来。其中并无 purge_queue 这个命令。不知道是否是版本问题仍是怎么回事。

rabbitmqadmin

而后在这里找到了 rabbitmqadmin 这个东西。官网地址:https://www.rabbitmq.com/mana... 。在上面看到了这么一行文字

Alternatively, you can download the version of rabbitmqadmin which corresponds with the management plugin version 3.7.17 from GitHub.

打开GitHub的连接:https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.7.17/bin/rabbitmqadmin 里面好像是 rabbitmqadmin 的代码。看了一下路径,v3.7.17。这个应该是要对应本身版本的。

rabbitmqctl status

查看版本:

[root@iZuf635fwy8k6ubk6r9yonZ ~]# rabbitmqctl status
Status of node rabbit@iZuf635fwy8k6ubk6r9yonZ ...
[{pid,3261},
 {running_applications,
     [{rabbitmq_management,"RabbitMQ Management Console","3.3.5"},
      {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.3.5"},
      {webmachine,"webmachine","1.10.3-rmq3.3.5-gite9359c7"},
      {mochiweb,"MochiMedia Web Server","2.7.0-rmq3.3.5-git680dba8"},
      {rabbitmq_management_agent,"RabbitMQ Management Agent","3.3.5"},
      {rabbit,"RabbitMQ","3.3.5"},
      {mnesia,"MNESIA  CXC 138 12","4.11"},
      {os_mon,"CPO  CXC 138 46","2.2.14"},
      {amqp_client,"RabbitMQ AMQP Client","3.3.5"},
      {inets,"INETS  CXC 138 49","5.9.8"},
      {xmerl,"XML parser","1.3.6"},
      {sasl,"SASL  CXC 138 11","2.3.4"},
      {stdlib,"ERTS  CXC 138 10","1.19.4"},
      {kernel,"ERTS  CXC 138 10","2.16.4"}]},
 {os,{unix,linux}},
 {erlang_version,
     "Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [async-threads:30] [hipe] [kernel-poll:true]\n"},
 {memory,
     [{total,44920360},
      {connection_procs,944512},
      {queue_procs,911352},
      {plugins,406328},
      {other_proc,13412192},
      {mnesia,200144},
      {mgmt_db,391944},
      {msg_index,95648},
      {other_ets,1174432},
      {binary,2676288},
      {code,20229484},
      {atom,711569},
      {other_system,3766467}]},
 {alarms,[]},
 {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
 {vm_memory_high_watermark,0.4},
 {vm_memory_limit,1654662758},
 {disk_free_limit,50000000},
 {disk_free,16424939520},
 {file_descriptors,
     [{total_limit,924},{total_used,9},{sockets_limit,829},{sockets_used,2}]},
 {processes,[{limit,1048576},{used,396}]},
 {run_queue,0},
 {uptime,3716267}]
...done.

结果发现本身的版本居然是3.3.5。修改路径https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.7.17/bin/rabbitmqadminhttps://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.3.5/bin/rabbitmqadmin,访问,显示并无这个页面。大概是版本太老了。网上搜了一下3.3.5版本的消息。发现这个版本大概是2014年左右的东西,时至今日已经五年过去了,也太老了吧。


本文章一次没有写完,在我写的过程当中,项目提交给测试进行部分测试,而后同事在test环境也安装了一个RabbitMq。我登上去看了一下,嗯,是有 purge_queue 这个命令的。看了一下test的版本,3.6.8。果真仍是版本问题。我以前一直操做的是dev服务器的东西,虽然本地也有安装可是在本地进行简单的时候仍是很好处理的,注释掉代码,而后从新运行,消费掉垃圾消息便可。


至于 rabbitmqadmin ,官网上有这么一段话:

Obtaining rabbitmqadmin
With the management plugin installed, browse to http://{hostname}:15672/cli/rabbitmqadmin to download. The tool supports

大概意思就能够经过访问 http://{hostname}:15672/cli/rabbitmqadmin 来下载 rabbitmqadmin 。没有深刻研究。到此为止,后续可能会更新相关内容。


发布文章的时候发现,过长的思否对过长的url省略的后面的部分,就像是这样:https://raw.githubusercontent...。因此在上面就对url稍微处理了一下。

相关文章
相关标签/搜索