记一次删除-RabbitMQ-消息的经历

56次阅读

共计 6825 个字符,预计需要花费 18 分钟才能阅读完成。

前因

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

网页删除

想要删除队列中的消息,是可以通过 RabbitMq 的网页来操作的。登录上 RabbitMQ Management 之后, 在 Queues 中打开指定的队列:

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

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

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

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

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

没有深究

重置 RabbitMQ

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

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app

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

然后我当上述的情况再次发生,需要清空消息,这样个时候我不想用这样一看就很粗暴的方式来操作,而且这种方式不仅会清空消息,还会清空所有配置信息。所以这次我想要先清除指定队列的消息即可。

删除指定的队列

然后我找到了这个方法:

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)  [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 稍微处理了一下。

正文完
 0