跳至主内容
版本:4.3

rabbitmqadmin v2,一个用于 HTTP API 的命令行工具

rabbitmqadmin v2 是一个使用 HTTP API 的命令行工具。

它支持管理 UI 中可用的许多操作:

请注意,rabbitmqadmin 并不是 rabbitmqctlrabbitmq-plugins 的替代品,因为 HTTP API 有意不公开某些操作。

rabbitmqadmin v2

此版本的 rabbitmqadmin 是一个独立项目,拥有独立于 RabbitMQ 的开发周期。

它以原生二进制文件的形式分发。

下载

适用于 x86-64 Linux、aarch64 Linux、aarch64 macOS 和 x86-64 Windows 的二进制文件通过 GitHub 发布页 分发。

rabbitmqadmin 也可从 Team RabbitMQ 的 Homebrew tap 获取。

brew tap rabbitmq/tap
brew install rabbitmqadmin

RabbitMQ 系列兼容性

rabbitmqadmin 2.26.0 开始,该工具可与以下 RabbitMQ 发布系列配合使用:

  • 4.x
  • 3.13.x
  • 3.12.x

并非所有系列都支持每一个命令和参数,因为旧系列可能没有较新的 HTTP API 端点(或不支持现有端点的某些参数)。

用法

探索可用的命令组和子命令

要探索可用的命令组,请使用

rabbitmqadmin help

这将输出命令组列表

Usage: rabbitmqadmin [OPTIONS] <COMMAND>

Commands:
auth_attempts Authentication attempt statistics
bindings Operations on bindings
channels Operations on channels
close Closes connections
config_file Operations on the local configuration file
connections Operations on connections
declare Creates or declares objects
definitions Operations on definitions (everything except for messages: virtual hosts, queues, streams, exchanges, bindings, users, etc)
delete Deletes objects
deprecated_features Operations on deprecated features
exchanges Operations on exchanges
export See 'definitions export'
feature_flags Operations on feature flags
federation Operations on federation upstreams and links
global_parameters Operations on global runtime parameters
health_check Runs health checks
import See 'definitions import'
list Lists objects
nodes Node operations
operator_policies Operations on operator policies
parameters Operations on runtime parameters
passwords Operations on passwords
permissions Operations on user permissions
plugins List enabled plugins
policies Operations on policies
purge Purges queues
queues Operations on queues
rebalance Rebalancing of leader replicas
show Overview, memory footprint breakdown, and more
shovels Operations on shovels
streams Operations on streams
tanzu Tanzu RabbitMQ-specific commands
users Operations on users
user_limits Operations on per-user (resource) limits
vhosts Virtual host operations
vhost_limits Operations on virtual host (resource) limits
help Print this message or the help of the given subcommand(s)

要探索特定组中的命令,请使用

rabbitmqadmin {group name} help

使用 help--help 探索 CLI

要了解可用的命令组和特定命令,请运行

rabbitmqadmin help

此标志可以附加到命令或子命令后,以获取特定于命令的文档。

rabbitmqadmin queues declare --help
# => creates or declares things
# =>
# => Usage: rabbitmqadmin declare [object]
# => ...

或者,可以将命令名称传递给 help 子命令。这等同于在命令名称后附加 --help

rabbitmqadmin queues help declare
# => creates or declares things
# =>
# => Usage: rabbitmqadmin declare [object]
# => ...

更具体的示例涵盖在下方的“示例”部分。

交互式与脚本使用

与原始版本一样,rabbitmqadmin v2 首先是为人类交互式使用而构建的。许多命令会输出格式化的表格,例如

rabbitmqadmin show overview

将输出一个如下所示的表格

┌───────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Overview │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ key │ value │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Product name │ RabbitMQ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Product version │ 4.2.5 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ RabbitMQ version │ 4.2.5 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Erlang version │ 27.3.4 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Erlang details │ Erlang/OTP 27 [erts-15.2.7] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Connections (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ AMQP 0-9-1 channels (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Queues and streams (total) │ 1 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Consumers (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Messages (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Messages ready for delivery (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Messages delivered but unacknowledged by consumers (total) │ 0 │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Publishing (ingress) rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Publishing confirm rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Consumer delivery (egress) rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Consumer delivery in automatic acknowledgement mode rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Consumer acknowledgement rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Unroutable messages: returned-to-publisher rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Unroutable messages: dropped rate (global) │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Cluster tags │ "core": "8" │
│ │ "env": "development" │
│ │ "machine": "X8Y33L97DQ" │
│ │ │
├───────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Node tags │ "environment": "production" │
│ │ "purpose": "iot_ingress" │
│ │ "region": "ca-central-1" │
│ │ "series": "4.1.x" │
│ │ │
└───────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────┘

显而易见,在脚本中解析此类输出将具有挑战性。

因此,如果传递了 --non-interactive 标志,rabbitmqadmin v2 可以以更适合脚本编写的方式呈现结果。这是一个全局标志,因此必须在命令和子命令名称之前传递。

rabbitmqadmin --non-interactive show overview

上述命令的输出将不包含任何表格边框,因此更容易解析。

key
Product name RabbitMQ
Product version 4.2.5
RabbitMQ version 4.2.5
Erlang version 27.3.4
Erlang details Erlang/OTP 27 [erts-15.2.7] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]

检索基本节点信息

rabbitmqadmin show overview

将以表格形式显示基本节点信息。

检索连接、队列/流、通道流失信息

有助于评估集群中的连接、队列/流、通道 流失指标 (churn metrics)

rabbitmqadmin show churn

列出集群节点

rabbitmqadmin nodes list

列出虚拟主机

rabbitmqadmin vhosts list

列出用户

rabbitmqadmin users list

创建用户

rabbitmqadmin users declare --name "a-user" --password "secure-password" --tags "monitoring,policymaker"
# Create user with password hash (recommended)
# Use 'rabbitmqadmin passwords salt_and_hash "my-password"' to generate the hash
rabbitmqadmin users declare --name "a-user" --password-hash "$2b$12$abcdefghijklmnopqrstuvwxyz" --tags "administrator"

删除用户

rabbitmqadmin users delete --name "a-user"
# Idempotent deletion (does not fail if the user doesn't exist)
rabbitmqadmin users delete --name "a-user" --idempotently

列出用户权限

有关权限的更多信息,请参阅 访问控制指南

# List permissions for all users
rabbitmqadmin permissions list
# Alternative: list all permissions using the 'users permissions' command
rabbitmqadmin users permissions

授予用户权限

有关授予权限的更多信息,请参阅 访问控制指南

# Grant permissions to a user for a virtual host
rabbitmqadmin --vhost "events" permissions declare --username "a-user" --configure ".*" --read ".*" --write ".*"
# Grant limited permissions (read-only for specific queues)
rabbitmqadmin --vhost "events" permissions declare --username "monitoring" --configure "" --read "^metrics\\..*" --write ""

撤销用户权限

有关权限的更多信息,请参阅 访问控制指南

# Revoke all permissions for a user in a virtual host
rabbitmqadmin --vhost "events" permissions delete --username "a-user"
# Idempotent revocation (does not fail if permissions don't exist)
rabbitmqadmin --vhost "events" permissions delete --username "a-user" --idempotently

列出用户连接

# List connections for a specific user
rabbitmqadmin users connections --username "a-user"

列出队列

rabbitmqadmin queues list
rabbitmqadmin --vhost "monitoring" queues list

列出交换器

rabbitmqadmin exchanges list
rabbitmqadmin --vhost "events" exchanges list

列出绑定

rabbitmqadmin bindings list
rabbitmqadmin --vhost "events" bindings list

将队列绑定到交换器

rabbitmqadmin --vhost "events" bindings declare --source "events.topic" --destination-type "queue" --destination "target.queue" --routing-key "events.order.created"

将源交换器绑定到目标交换器

rabbitmqadmin --vhost "events" bindings declare --source "events.topic" --destination-type "exchange" --destination "events.fanout" --routing-key "events.*"

删除绑定

rabbitmqadmin --vhost "events" bindings delete --source "events.topic" --destination-type "queue" --destination "target.queue" --routing-key "events.order.created"

将交换器绑定到队列或其他交换器

exchanges bind 命令是 bindings declare 的替代方案。

rabbitmqadmin --vhost "events" exchanges bind --source "events.topic" --destination-type "queue" --destination "events.processing" --routing-key "user.created"
rabbitmqadmin --vhost "events" exchanges bind --source "events.fanout" --destination-type "exchange" --destination "events.archived" --routing-key ""

取消交换器与队列或其他交换器的绑定

exchanges unbind 命令是 bindings delete 的替代方案。

rabbitmqadmin --vhost "events" exchanges unbind --source "events.topic" --destination-type "queue" --destination "events.processing" --routing-key "user.created"
rabbitmqadmin --vhost "events" exchanges unbind --source "events.fanout" --destination-type "exchange" --destination "events.archived" --routing-key ""

创建虚拟主机

rabbitmqadmin vhosts declare --name "vh-789" --default-queue-type "quorum" --description "Used to reproduce issue #789"

删除虚拟主机

rabbitmqadmin vhosts delete --name "vh-789"
# --idempotently means that 404 Not Found responses will not be considered errors
rabbitmqadmin vhosts delete --name "vh-789" --idempotently

删除多个虚拟主机

危险

这是一项极具破坏性的操作,必须极其小心地使用。

此命令将删除所有符合提供的正则表达式模式的虚拟主机。这些虚拟主机中的所有数据都将永久丢失,包括:

  • 队列、流和分区流
  • 交换器和绑定
  • 消息
  • 用户权限
  • 联邦上游和链接
  • Shovels
  • 策略和操作员策略
  • 运行时参数

在执行实际删除之前,请务必先使用 --dry-run 来验证将要删除的内容。

# ALWAYS run with --dry-run first to see what would be deleted
rabbitmqadmin vhosts delete_multiple --name-pattern "^test-.*" --dry-run
# After verifying with --dry-run, use --approve to perform the actual deletion
# This will delete ALL virtual hosts whose names start with "test-"
rabbitmqadmin vhosts delete_multiple --name-pattern "^test-.*" --approve
注意

默认虚拟主机 (/) 始终会被保留,且永远不会被此命令删除,即使它符合模式。

启用虚拟主机删除保护

虚拟主机删除保护可防止虚拟主机被意外删除。这对于保护关键的生产虚拟主机非常有用。

有关删除保护的更多信息,请参阅 虚拟主机指南

rabbitmqadmin vhosts enable_deletion_protection --name "production"
# Enable deletion protection for multiple critical virtual hosts
rabbitmqadmin vhosts enable_deletion_protection --name "production"
rabbitmqadmin vhosts enable_deletion_protection --name "staging"

禁用虚拟主机删除保护

有关删除保护的更多信息,请参阅 虚拟主机指南

rabbitmqadmin vhosts disable_deletion_protection --name "production"

列出虚拟主机限制

# List limits for all virtual hosts
rabbitmqadmin vhost_limits list
# List limits for a specific virtual host
rabbitmqadmin --vhost "events" vhost_limits list

设置虚拟主机限制

# Set a limit on maximum number of connections
rabbitmqadmin --vhost "events" vhost_limits declare --name "max-connections" --value 100
# Set a limit on maximum number of queues
rabbitmqadmin --vhost "events" vhost_limits declare --name "max-queues" --value 500

删除虚拟主机限制

rabbitmqadmin --vhost "events" vhost_limits delete --name "max-connections"

列出用户限制

# List limits for all users
rabbitmqadmin user_limits list
# List limits for a specific user
rabbitmqadmin user_limits list --username "a-user"
# Alternative using the 'users' command group
rabbitmqadmin users limits --username "a-user"

设置用户限制

# Set a limit on maximum number of connections for a user
rabbitmqadmin user_limits declare --username "a-user" --name "max-connections" --value 10
# Set a limit on maximum number of channels for a user
rabbitmqadmin user_limits declare --username "a-user" --name "max-channels" --value 50

删除用户限制

rabbitmqadmin user_limits delete --username "a-user" --name "max-connections"

声明队列

rabbitmqadmin --vhost "events" queues declare --name "target.quorum.queue.name" --type "quorum" --durable true
rabbitmqadmin --vhost "events" queues declare --name "target.stream.name" --type "stream" --durable true
rabbitmqadmin --vhost "events" queues declare --name "target.classic.queue.name" --type "classic" --durable true --auto-delete false

列出流

rabbitmqadmin streams list
# List streams in a specific virtual host
rabbitmqadmin --vhost "events" streams list

声明流

rabbitmqadmin --vhost "events" streams declare --name "events.stream" --expiration "3D"

删除流

rabbitmqadmin --vhost "events" streams delete --name "events.stream"

显示流详情

rabbitmqadmin --vhost "events" streams show --name "events.stream"
# Show only specific columns
rabbitmqadmin --vhost "events" streams show --name "events.stream" --columns "name,messages,consumers"

清空队列

rabbitmqadmin --vhost "events" queues purge --name "target.queue.name"

删除队列

rabbitmqadmin --vhost "events" queues delete --name "target.queue.name"
# --idempotently means that 404 Not Found responses will not be considered errors
rabbitmqadmin --vhost "events" queues delete --name "target.queue.name" --idempotently

显示队列详情

rabbitmqadmin --vhost "events" queues show --name "target.queue.name"
# Show only specific columns
rabbitmqadmin --vhost "events" queues show --name "target.queue.name" --columns "name,type,messages,consumers"

声明交换器

rabbitmqadmin --vhost "events" exchanges declare --name "events.all_types.topic" --type "topic" --durable true
rabbitmqadmin --vhost "events" exchanges declare --name "events.all_type.uncategorized" --type "fanout" --durable true --auto-delete false
rabbitmqadmin --vhost "events" exchanges declare --name "local.random.c60bda92" --type "x-local-random" --durable true

删除交换器

rabbitmqadmin --vhost "events" exchanges delete --name "target.exchange.name"
# --idempotently means that 404 Not Found responses will not be considered errors
rabbitmqadmin --vhost "events" exchanges delete --name "target.exchange.name" --idempotently

列出连接

rabbitmqadmin connections list
# List connections for a specific virtual host
rabbitmqadmin --vhost "events" connections list
# List connections for a specific user
rabbitmqadmin connections list_of_user --username "a-user"

关闭连接

rabbitmqadmin connections close --name "127.0.0.1:5672 -> 127.0.0.1:59876"
# Alternative using the 'close' command group
rabbitmqadmin close connection --name "127.0.0.1:5672 -> 127.0.0.1:59876"

关闭用户的所有连接

rabbitmqadmin connections close_of_user --username "a-user"
# Alternative using the 'close' command group
rabbitmqadmin close user_connections --username "a-user"

列出通道

rabbitmqadmin channels list
# List channels in a specific virtual host
rabbitmqadmin --vhost "events" channels list

列出消费者

rabbitmqadmin consumers list
# List consumers in a specific virtual host
rabbitmqadmin --vhost "events" consumers list

检查节点内存分布

有两个命令用于分析目标 节点的内存占用情况

# displays a breakdown in bytes
rabbitmqadmin show memory_breakdown_in_bytes --node 'rabbit@hostname'
# displays a breakdown in percent
rabbitmqadmin show memory_breakdown_in_percent --node 'rabbit@hostname'

show memory_breakdown_in_percent 的示例输出

┌────────────────────────────────────────┬────────────┐
│ key │ percentage │
├────────────────────────────────────────┼────────────┤
│ total │ 100% │
├────────────────────────────────────────┼────────────┤
│ Binary heap │ 45.10% │
├────────────────────────────────────────┼────────────┤
│ Allocated but unused │ 23.45% │
├────────────────────────────────────────┼────────────┤
│ Quorum queue ETS tables │ 23.05% │
├────────────────────────────────────────┼────────────┤
│ Other processes │ 5.32% │
├────────────────────────────────────────┼────────────┤
│ Other (used by the runtime) │ 4.98% │
├────────────────────────────────────────┼────────────┤
│ Code │ 4.54% │
├────────────────────────────────────────┼────────────┤
│ Client connections: others processes │ 3.64% │
├────────────────────────────────────────┼────────────┤
│ Management stats database │ 3.48% │
├────────────────────────────────────────┼────────────┤
│ Client connections: reader processes │ 3.22% │
├────────────────────────────────────────┼────────────┤
│ Plugins and their data │ 3.12% │
├────────────────────────────────────────┼────────────┤
│ Other (ETS tables) │ 1.55% │
├────────────────────────────────────────┼────────────┤
│ Metrics data │ 0.66% │
├────────────────────────────────────────┼────────────┤
│ AMQP 0-9-1 channels │ 0.40% │
├────────────────────────────────────────┼────────────┤
│ Message store indices │ 0.27% │
├────────────────────────────────────────┼────────────┤
│ Atom table │ 0.24% │
├────────────────────────────────────────┼────────────┤
│ Client connections: writer processes │ 0.19% │
├────────────────────────────────────────┼────────────┤
│ Quorum queue replica processes │ 0.10% │
├────────────────────────────────────────┼────────────┤
│ Stream replica processes │ 0.07% │
├────────────────────────────────────────┼────────────┤
│ Mnesia │ 0.02% │
├────────────────────────────────────────┼────────────┤
│ Metadata store │ 0.02% │
├────────────────────────────────────────┼────────────┤
│ Stream coordinator processes │ 0.02% │
├────────────────────────────────────────┼────────────┤
│ Classic queue processes │ 0.00% │
├────────────────────────────────────────┼────────────┤
│ Metadata store ETS tables │ 0.00% │
├────────────────────────────────────────┼────────────┤
│ Stream replica reader processes │ 0.00% │
├────────────────────────────────────────┼────────────┤
│ Reserved by the kernel but unallocated │ 0.00% │
└────────────────────────────────────────┴────────────┘

请注意,计算节点内存占用有 两种支持的策略。在计算每个类别的百分比份额时,rabbitmqadmin 将使用较大值作为 100%。

影响百分比数值精度其他因素包括 运行时分配器 (runtime allocator) 的行为细微差别和 内核页缓存 (kernel page cache)

访问内存分布的另一种方法是通过 nodes 命令组:

rabbitmqadmin nodes memory_breakdown_in_bytes --node 'rabbit@hostname'
rabbitmqadmin nodes memory_breakdown_in_percent --node 'rabbit@hostname'

列出功能标志及其状态

rabbitmqadmin feature_flags list

启用功能标志

rabbitmqadmin feature_flags enable --name rabbitmq_4.0.0

启用所有稳定的功能标志

rabbitmqadmin feature_flags enable_all

列出集群中正在使用的已弃用功能

rabbitmqadmin deprecated_features list_used

列出所有已弃用功能

rabbitmqadmin deprecated_features list

导出定义

要将 定义 导出到标准输出,请使用 definitions export --stdout

rabbitmqadmin definitions export --stdout

要将定义导出到文件,请使用 definitions export --file /path/to/definitions.file.json

rabbitmqadmin definitions export --file /path/to/definitions.file.json

导出并转换定义

definitions export 可以转换从目标节点获取的已导出 JSON 定义文件。这是通过对导出的 JSON 文件应用一个或多个转换来完成的。

这对于从 3.13.x 节点源出的定义集中移除经典队列镜像相关键(如 ha-mode),或混淆用户名和密码,或完全排除某些定义文件部分非常有用。

要指定应应用哪些转换,请使用 --transformations 选项,它接收一个逗号分隔的支持的操作名称列表。

下表解释了哪些转换可用以及它们的作用:

转换名称描述
strip_cmq_keys_from_policies从所有导出的策略中删除所有经典队列镜像相关键(如 ha-mode)。

必须后跟 drop_empty_policies,以在移除所有经典队列镜像相关键后删除那些定义已变为空(从而在导入时无效)的策略。
drop_empty_policies应在 strip_cmq_keys_from_policies 之后使用,以删除在移除所有经典队列镜像相关键后定义已变为空(从而在导入时无效)的策略。
obfuscate_usernames用虚拟值替换用户名和密码。

对于用户名,使用的值为:obfuscated-username-1obfuscated-username-2 等。

对于密码,生成的值为:password-1password-2 等。

此转换会一致地更新用户和权限部分。
exclude_users从结果中删除所有用户。通常与 exclude_permissions 一起使用。
exclude_permissions从结果中删除所有权限。通常与 exclude_users 一起使用。
exclude_runtime_parameters从结果中删除所有运行时参数(包括联邦上游、shovels、Tanzu RabbitMQ 中的 WSR 和 SDS 设置)。
exclude_policies从结果中删除所有策略。
no_op不执行任何操作。可用作动态计算转换列表中的默认值,例如在脚本中。

以下命令应用了两个名为 strip_cmq_keys_from_policiesdrop_empty_policies 的转换,它们将删除 RabbitMQ 3.13 节点支持的所有经典队列镜像相关策略键,然后移除定义为空的策略。

# strips classic mirrored queue-related policy keys from the exported definitions, then prints them
# to the standard output stream
rabbitmqadmin definitions export --stdout --transformations strip_cmq_keys_from_policies,drop_empty_policies

以下示例导出了不包含用户和权限的定义。

# removes users and user permissions from the exported definitions, then prints them
# to the standard output stream
rabbitmqadmin definitions export --stdout --transformations exclude_users,exclude_permissions

要导出用户名被替换为虚拟值的定义(用户名:obfuscated-username-1, obfuscated-username-2 等;密码:password-1, password-2 等),请使用 obfuscate_usernames 转换。

rabbitmqadmin definitions export --file /path/to/definitions.file.json --transformations obfuscate_usernames

导入定义

要从标准输入导入定义,请使用 definitions import --stdin

cat /path/to/definitions.file.json | rabbitmqadmin definitions import --stdin

要从文件导入定义,请使用 definitions import --file /path/to/definitions.file.json

rabbitmqadmin definitions import --file /path/to/definitions.file.json

从特定虚拟主机导出定义

要仅导出特定虚拟主机的定义:

rabbitmqadmin --vhost "events" definitions export_from_vhost --stdout
rabbitmqadmin --vhost "events" definitions export_from_vhost --file /path/to/vhost-definitions.json

导入特定于虚拟主机的定义

要将定义导入到特定虚拟主机:

rabbitmqadmin --vhost "events" definitions import_into_vhost --file /path/to/vhost-definitions.json

声明 AMQP 0-9-1 Shovel

要声明一个源和目标都使用 AMQP 0-9-1 的 动态 shovel,请使用 shovels declare_amqp091

rabbitmqadmin shovels declare_amqp091 --name my-amqp091-shovel \
--source-uri amqp://username:s3KrE7@source.hostname:5672 \
--destination-uri amqp://username:s3KrE7@source.hostname:5672 \
--ack-mode "on-confirm" \
--source-queue "src.queue" \
--destination-queue "dest.queue"
# When source and destination queues already exist (predeclared topology)
rabbitmqadmin shovels declare_amqp091 --name my-amqp091-shovel \
--source-uri amqp://username:s3KrE7@source.hostname:5672 \
--destination-uri amqp://username:s3KrE7@source.hostname:5672 \
--ack-mode "on-confirm" \
--source-queue "src.queue" \
--destination-queue "dest.queue" \
--predeclared-source \
--predeclared-destination

声明 AMQP 1.0 Shovel

要声明一个源和目标都使用 AMQP 1.0 的 动态 shovel,请使用 shovels declare_amqp10

请注意

  1. 对于 AMQP 1.0 shovels,URI 中的凭据是强制要求的(没有默认值)。
  2. 对于 AMQP 1.0 shovels,拓扑必须预先声明(等同于 AMQP 0-9-1 shovels 的 --predeclared-source--predeclared-destination 标志)。
  3. AMQP 1.0 shovels 应使用 AMQP 1.0 地址 v2
rabbitmqadmin shovels declare_amqp10 --name my-amqp1.0-shovel \
--source-uri "amqp://username:s3KrE7@source.hostname:5672?hostname=vhost:src-vhost" \
--destination-uri "amqp://username:s3KrE7@source.hostname:5672?hostname=vhost:dest-vhost" \
--ack-mode "on-confirm" \
--source-address "/queues/src.queue" \
--destination-address "/queues/dest.queue"

列出 Shovels

要列出所有虚拟主机上的 shovels,请使用 shovels list_all

rabbitmqadmin shovels list_all

要列出特定虚拟主机上的 shovels:

rabbitmqadmin --vhost "events" shovels list

删除 Shovel

要删除 shovel,请使用 shovels delete --name

rabbitmqadmin shovels delete --name my-amqp091-shovel

管理 Shovels 的 TLS 对等验证

警告

这些命令仅应在紧急情况下使用。禁用 TLS 对等验证会降低安全性。请始终优先使用正确的证书启用验证。

# Disable TLS peer verification for all shovel source URIs (emergency use only)
rabbitmqadmin --vhost "events" shovels disable_tls_peer_verification_for_all_source_uris
# Disable TLS peer verification for all shovel destination URIs (emergency use only)
rabbitmqadmin --vhost "events" shovels disable_tls_peer_verification_for_all_destination_uris
# Enable TLS peer verification for all shovel source URIs with certificate paths
# Note: paths must be accessible on the RabbitMQ node(s), not the local machine
rabbitmqadmin --vhost "events" shovels enable_tls_peer_verification_for_all_source_uris \
--node-local-ca-certificate-bundle-path /etc/rabbitmq/certs/ca-bundle.pem \
--node-local-client-certificate-file-path /etc/rabbitmq/certs/client-cert.pem \
--node-local-client-private-key-file-path /etc/rabbitmq/certs/client-key.pem
# Enable TLS peer verification for all shovel destination URIs with certificate paths
rabbitmqadmin --vhost "events" shovels enable_tls_peer_verification_for_all_destination_uris \
--node-local-ca-certificate-bundle-path /etc/rabbitmq/certs/ca-bundle.pem \
--node-local-client-certificate-file-path /etc/rabbitmq/certs/client-cert.pem \
--node-local-client-private-key-file-path /etc/rabbitmq/certs/client-key.pem

列出联邦上游

要列出所有虚拟主机上的 联邦上游,请使用 federation list_all_upstreams

rabbitmqadmin federation list_all_upstreams

为交换器联邦创建联邦上游

要创建 联邦上游,请使用 federation declare_upstream_for_exchanges。此命令提供了一组精简的选项,仅包含与交换器联邦相关的选项。

rabbitmqadmin --vhost "local-vhost" federation declare_upstream_for_exchanges --name "pollux" \
--uri "amqp://pollux.eng.megacorp.local:5672/remote-vhost" \
--ack-mode 'on-publish' \
--prefetch-count 2000 \
--exchange-name "overridden.name" \
--queue-type quorum \
--bind-using-nowait true

为队列联邦创建联邦上游

要创建 联邦上游,请使用 federation declare_upstream_for_queues。此命令提供了一组精简的选项,仅包含与队列联邦相关的选项。

rabbitmqadmin --vhost "local-vhost" federation declare_upstream_for_queues --name "clusters.sirius" \
--uri "amqp://sirius.eng.megacorp.local:5672/remote-vhost" \
--ack-mode 'on-publish' \
--prefetch-count 2000 \
--queue-name "overridden.name" \
--consumer-tag "overriden.ctag"

创建通用联邦上游

要创建一个将(或可以)用于联邦队列和交换器的 联邦上游,请使用 federation declare_upstream。它结合了 所有联邦选项,即 declare_upstream_for_queuesdeclare_upstream_for_exchanges 的选项。

rabbitmqadmin --vhost "local-vhost" federation declare_upstream --name "pollux" \
--uri "amqp://pollux.eng.megacorp.local:5672/remote-vhost" \
--ack-mode 'on-publish' \
--prefetch-count 2000 \
--queue-name "overridden.name" \
--consumer-tag "overriden.ctag" \
--exchange-name "overridden.name" \
--queue-type quorum \
--bind-using-nowait true

删除联邦上游

要删除 联邦上游,请使用 'federation delete_upstream',它需要一个虚拟主机和一个上游名称。

rabbitmqadmin --vhost "local-vhost" federation delete_upstream --name "upstream.to.delete"

要列出所有虚拟主机上的 联邦链接,请使用 federation list_all_links

rabbitmqadmin federation list_all_links

管理联邦上游的 TLS 对等验证

警告

这些命令仅应在紧急情况下使用。禁用 TLS 对等验证会降低安全性。请始终优先使用正确的证书启用验证。

# Disable TLS peer verification for all federation upstreams (emergency use only)
rabbitmqadmin --vhost "events" federation disable_tls_peer_verification_for_all_upstreams
# Enable TLS peer verification for all federation upstreams with certificate paths
# Note: paths must be accessible on the RabbitMQ node(s), not the local machine
rabbitmqadmin --vhost "events" federation enable_tls_peer_verification_for_all_upstreams \
--node-local-ca-certificate-bundle-path /etc/rabbitmq/certs/ca-bundle.pem \
--node-local-client-certificate-file-path /etc/rabbitmq/certs/client-cert.pem \
--node-local-client-private-key-file-path /etc/rabbitmq/certs/client-key.pem

重新平衡队列领导者

要跨集群节点 重新平衡仲裁队列 (quorum queue) 领导者,请使用 rebalance queues。此操作有助于更均匀地在集群节点间分配队列领导者,这可以更均匀地分担负载并改善资源利用率(具体取决于工作负载)。

# Rebalance queue leaders in the default virtual host (/)
rabbitmqadmin rebalance queues
# Rebalance queue leaders in a specific virtual host
rabbitmqadmin --vhost "production" rebalance queues

此操作是异步的,根据集群中队列的数量,可能需要一些时间才能完成。请监控 RabbitMQ 日志以获取完成状态。

检查目标端点(用于故障排除)

使用 show endpoint 显示计算出的 HTTP API 端点 URI。

rabbitmqadmin show endpoint

此命令有助于验证 rabbitmqadmin 是否指向了正确的 RabbitMQ HTTP API 端点,例如在存在 rabbitmqadmin.conf 文件或设置了环境变量时。

健康检查

rabbitmqadmin 提供了各种 健康检查 命令。

# Check for local resource alarms on the target node
rabbitmqadmin health_check local_alarms
# Check for resource alarms across the entire cluster
rabbitmqadmin health_check cluster_wide_alarms
# Check if node is quorum critical (queues/streams would lose quorum if node shuts down)
rabbitmqadmin health_check node_is_quorum_critical
# Check for deprecated features in use across the cluster
rabbitmqadmin health_check deprecated_features_in_use
# Verify TCP listener is reachable on specific port
rabbitmqadmin health_check port_listener --port 5672
# Verify TCP listener is reachable for specific protocol
rabbitmqadmin health_check protocol_listener --protocol amqp091

这些健康检查对于监控脚本和自动化集群健康验证非常有用。

认证尝试统计信息

要显示特定集群节点的认证尝试统计信息:

rabbitmqadmin auth_attempts stats --node "rabbit@hostname"

列出已启用的插件

# List plugins across all cluster nodes
rabbitmqadmin plugins list_all
# List plugins enabled on a specific node
rabbitmqadmin plugins list_on_node --node "rabbit@hostname"

TLS 配置

rabbitmqadmin 支持连接到对 HTTP API 使用 TLS 的 RabbitMQ 集群。

# Connect using TLS with default settings
rabbitmqadmin --use-tls show overview
# Connect using TLS with custom CA certificate bundle
rabbitmqadmin --use-tls --tls-ca-cert-file /path/to/ca-bundle.pem show overview
# Connect using TLS with client certificate authentication
rabbitmqadmin --use-tls \
--tls-ca-cert-file /path/to/ca-bundle.pem \
--tls-cert-file /path/to/client-cert.pem \
--tls-key-file /path/to/client-key.pem \
show overview
# Disable TLS peer verification (not recommended for production)
rabbitmqadmin --use-tls --disable-tls-peer-verification show overview

密码哈希

rabbitmqadmin 可以生成与 RabbitMQ 的 密码哈希 系统兼容的密码哈希。

# Hash a password using SHA256 (default)
rabbitmqadmin passwords salt_and_hash "my-secret-password"
# Override --hashing-algorithm if RabbitMQ nodes are configured to use SHA512
rabbitmqadmin passwords salt_and_hash "my-secret-password" --hashing-algorithm SHA512

这对于预先计算用户管理脚本的密码哈希,或在使用 RabbitMQ 定义文件时非常有用。

列出策略

rabbitmqadmin policies list
# List policies in a specific virtual host
rabbitmqadmin --vhost "events" policies list_in

创建策略

# Create a policy that sets max-length for all queues
rabbitmqadmin --vhost "events" policies declare --name "queue-limits" --pattern ".*" --definition '{"max-length":10000}' --priority 1 --apply-to "queues"
# Create a TTL policy for temporary queues
rabbitmqadmin --vhost "events" policies declare --name "temp-queues" --pattern "temp\\..*" --definition '{"message-ttl":300000,"expires":600000}' --priority 2 --apply-to "queues"

创建全包策略 (Blanket Policy)

全包策略是一种低优先级策略,用于匹配所有未被任何其他策略匹配的对象。

rabbitmqadmin --vhost "events" policies declare_blanket --name "default-limits" \
--apply-to "queues" --definition '{"max-length":100000}'

创建覆盖策略

覆盖策略是从现有具有更高优先级的策略创建的,它合并了额外的定义键。

rabbitmqadmin --vhost "events" policies declare_override --name "queue-limits" \
--definition '{"max-length-bytes":500000000}'
# Specify a custom name for the override policy
rabbitmqadmin --vhost "events" policies declare_override --name "queue-limits" \
--override-name "queue-limits-extended" --definition '{"max-length-bytes":500000000}'

列出冲突的策略

具有相同优先级且可能匹配相同对象的策略被视为冲突。

# List conflicting policies across all virtual hosts
rabbitmqadmin policies list_conflicting
# List conflicting policies in a specific virtual host
rabbitmqadmin --vhost "events" policies list_conflicting_in

删除策略

rabbitmqadmin --vhost "events" policies delete --name "queue-limits"

列出匹配对象的策略

# Show which policies apply to a specific queue
rabbitmqadmin --vhost "events" policies list_matching_object --name "my.queue" --type "queue"

修补(执行部分更新)策略

# Merge keys into an existing policy's definition (add or update keys)
rabbitmqadmin --vhost "events" policies patch \
--name "queue-limits" \
--definition '{"max-length": 7777777, "max-length-bytes": 3333333333}'

更新策略定义键

rabbitmqadmin --vhost "events" policies update_definition \
--name "queue-limits" \
--definition-key "max-length" \
--new-value "5000000"

更新虚拟主机中所有策略的定义键

# Re-enable federation by re-introducing the federation-upstream-set key
rabbitmqadmin --vhost "events" policies update_definitions_of_all_in \
--definition-key "federation-upstream-set" \
--new-value "all"

更新集群范围内所有策略的定义键

# Re-enable federation across all virtual hosts
rabbitmqadmin policies update_definitions_of_all \
--definition-key "federation-upstream-set" \
--new-value "all"

从策略中删除定义键

rabbitmqadmin --vhost "events" policies delete_definition_keys \
--name "queue-limits" \
--definition-keys max-length-bytes,max-length

从虚拟主机中所有策略删除定义键

# Temporarily disable federation by removing the federation-upstream-set key
rabbitmqadmin --vhost "events" policies delete_definition_keys_from_all_in \
--definition-keys federation-upstream-set

从集群范围内所有策略删除定义键

# Temporarily disable federation across all virtual hosts
rabbitmqadmin policies delete_definition_keys_from_all \
--definition-keys federation-upstream-set

列出操作员策略

rabbitmqadmin operator_policies list
# List operator policies in a specific virtual host
rabbitmqadmin --vhost "events" operator_policies list_in

创建操作员策略

# Create an operator policy to set max queue length
rabbitmqadmin --vhost "events" operator_policies declare --name "queue.max-length" --pattern ".*" --apply-to "queues" --definition '{"max-length":10000}' --priority 1

删除操作员策略

rabbitmqadmin --vhost "events" operator_policies delete --name "queue.max-length"

修补操作员策略的定义

# Merge keys into an existing operator policy's definition (add or update keys)
rabbitmqadmin --vhost "events" operator_policies patch --name "queue.max-length" --definition '{"max-length-bytes":1000000}'

更新操作员策略定义中的键

# Update a specific key in an operator policy definition
rabbitmqadmin --vhost "events" operator_policies update_definition --name "queue.max-length" --definition-key "max-length" --new-value 20000

更新多个操作员策略中的键

# Update a specific key in all operator policies in a virtual host
rabbitmqadmin --vhost "events" operator_policies update_definitions_of_all_in --definition-key "max-length" --new-value 20000

从操作员策略定义中删除键

# Delete specific keys from an operator policy definition
rabbitmqadmin --vhost "events" operator_policies delete_definition_keys --name "queue.max-length" --definition-keys "max-length-bytes"

从多个操作员策略中删除键

# Delete specific keys from all operator policies in a virtual host
rabbitmqadmin --vhost "events" operator_policies delete_definition_keys_from_all_in --definition-keys "max-length-bytes"

列出匹配对象的操作员策略

# List operator policies that match a specific queue
rabbitmqadmin --vhost "events" operator_policies list_matching_object --name "my.queue" --type "queue"

列出运行时参数

rabbitmqadmin parameters list_all
# List parameters in a specific virtual host
rabbitmqadmin --vhost "events" parameters list

设置运行时参数

# Set a runtime parameter.
# Note: for federation upstreams and shovels, use the dedicated 'federation' and 'shovels' command groups instead
rabbitmqadmin --vhost "events" parameters set --name "my-param" --component "my-component" --value '{"key":"value"}'

清除运行时参数

# Set a runtime parameter.
# Note: for federation upstreams and shovels, use the dedicated 'federation' and 'shovels' command groups instead
rabbitmqadmin --vhost "events" parameters clear --name "my-param" --component "my-component"
# Idempotent deletion (does not fail if the parameter doesn't exist)
rabbitmqadmin --vhost "events" parameters clear --name "my-param" --component "my-component" --idempotently

列出全局参数

rabbitmqadmin global_parameters list

设置全局运行时参数

rabbitmqadmin global_parameters set --name "cluster_name" --value '"production-cluster"'

清除全局运行时参数

rabbitmqadmin global_parameters clear --name "cluster_name"
# Idempotent deletion (does not fail if the parameter doesn't exist)
rabbitmqadmin global_parameters clear --name "cluster_name" --idempotently

Tanzu RabbitMQ 命令

注意

这些命令特定于 Tanzu RabbitMQ,在开源 RabbitMQ 中不可用。

模式定义同步 (SDS)

模式定义同步在集群间复制模式定义(队列、交换器、绑定等)。

# Check SDS status on a specific node
rabbitmqadmin tanzu sds status_on_node --node "rabbit@hostname"
# Disable SDS on a specific node
rabbitmqadmin tanzu sds disable_on_node --node "rabbit@hostname"
# Disable SDS cluster-wide
rabbitmqadmin tanzu sds disable_cluster_wide
# Enable SDS on a specific node
rabbitmqadmin tanzu sds enable_on_node --node "rabbit@hostname"
# Enable SDS cluster-wide
rabbitmqadmin tanzu sds enable_cluster_wide

热备份复制 (WSR)

热备份复制通过将数据复制到备用集群提供灾难恢复功能。

# Check WSR status on the target node
rabbitmqadmin tanzu wsr status

子命令和长选项推理

此功能目前仅在 main 分支中可用。

如果 RABBITMQADMIN_NON_INTERACTIVE_MODE 未设置为 true,该工具现在可以推断子命令和 --long-option 名称。

这意味着可以使用其唯一前缀引用子命令,例如:

  • 'queues del' 将被推断为 'queues delete'
  • 'q del --nam "a.queue"' 将被推断为 'queues delete --name "a.queue"'

要启用各项功能,请将以下环境变量设置为 'true':

  • RABBITMQADMIN_INFER_SUBCOMMANDS
  • RABBITMQADMIN_INFER_LONG_OPTIONS

此功能仅适用于交互式使用。对于非交互式使用,允许此功能可能过于危险。

配置文件

rabbitmqadmin v2 支持基于 TOML 的配置文件,将 HTTP API 连接设置组存储在别名(原始 rabbitmqadmin 术语中的“节点名称”)下。

以下是 rabbitmqadmin v2 配置文件的示例:

[local]
hostname = "localhost"
port = 15672
username = "lolz"
password = "lolz"
vhost = '/'

[staging]
hostname = "192.168.20.31"
port = 15672
username = "staging-2387a72329"
password = "staging-1d20cfbd9d"

[production]
hostname = "(redacted)"
port = 15671
username = "user-2ca6bae15ff6b79e92"
password = "user-92ee4c479ae604cc72"

无需在命令行上指定 --hostname--username 来连接到名为 staging 的集群(或特定节点),可以指定 --node 别名。

# will use the settings from the section called [staging]
rabbitmqadmin --node staging show churn

默认配置文件路径为 $HOME/.rabbitmqadmin.conf,与 rabbitmqadmin 的原始版本相同。它可以在命令行上覆盖。

# will use the settings from the section called [staging]
rabbitmqadmin --config $HOME/.configuration/rabbitmqadmin.conf --node staging show churn

显示配置文件路径

rabbitmqadmin config_file show_path

显示配置文件内容

rabbitmqadmin config_file show

向配置文件添加节点条目

# Add a new node entry for a staging cluster
rabbitmqadmin config_file add_node --node staging --host 192.168.20.31 --port 15672 \
--username "staging-user" --password "staging-password"
# Add a node entry with TLS settings
rabbitmqadmin config_file add_node --node production --host prod.example.com --port 15671 \
--username "prod-user" --password "prod-password" --use-tls \
--tls-ca-cert-file /path/to/ca-bundle.pem
# Create the configuration file if it does not exist
rabbitmqadmin config_file add_node --node local --host localhost --port 15672 \
--username "guest" --password "guest" --create-file-if-missing

更新配置文件中的节点条目

# Update an existing node entry (or create one if it does not exist)
rabbitmqadmin config_file update_node --node staging --port 15673 --password "new-password"

从配置文件删除节点条目

rabbitmqadmin config_file delete_node --node staging

与 v1 相比的破坏性或潜在破坏性更改

删除了一些非必要功能

rabbitmqadmin v2 不支持:

  • 结果排序。相反,请使用 --non-interactive 并解析空格分隔的输出。许多处理数据的现代工具可以将其解析为表格、对数据集进行排序、过滤结果等。事实上,这些数据处理功能在 某些 Shell 中 现成可用。
  • 列选择。此功能可能会被重新引入。
  • 任意命令的 JSON 输出(definitions 命令除外)。如果需要使用 JSON,请 直接使用 HTTP API
  • 任意命令的 CSV 输出。此格式可能会被重新引入。

--snake-case 用于命令选项

rabbitmqadmin v1 使用 lower_case 作为命名命令参数,例如:

# Note: auto_delete
rabbitmqadmin-v1 --vhost "vh-2" queues declare name="qq.1" type="quorum" durable=true auto_delete=false

rabbitmqadmin v2 使用更典型的 --snake-case 格式作为相同的参数。

# Note: --auto-delete
rabbitmqadmin --vhost "vh-2" queues declare --name "qq.1" --type "quorum" --durable true --auto-delete false

全局参数优先

rabbitmqadmin v2 中的全局标志必须位于命令类别(如 list)和命令本身之前,即各种 HTTP API 端点选项和 --vhost

rabbitmqadmin --vhost "events" queues declare --name "target.quorum.queue.name" --type "quorum" --durable true

--prefix 覆盖 API 路径前缀

rabbitmqadmin v1 中,--path-prefix 会附加到默认的 API 路径前缀。在此版本中,传递给 --path-prefix 的值将按原样使用,换句话说,它替换了默认前缀 /api

配置文件格式迁移至 TOML

rabbitmqadmin v1 支持 ini 配置文件,允许用户将多个命令行值分组在一个名称下,例如集群或节点昵称。

由于 rabbitmqadmin v1 的“除了 Python 没有其他依赖”的设计目标,此功能实际上并未经过真正测试,并且特定的语法(Python 的 ConfigParser 支持的 ini 文件)缺乏可用的 linting、解析或生成工具。

rabbitmqadmin v2 用 TOML 替换了此格式,这是一种流行的配置标准,拥有 验证和 linting 工具,以及非常成熟且并不特定于 rabbitmqadmin v2 的解析器。

以下是 rabbitmqadmin v2 配置文件的示例:

[local]
hostname = "localhost"
port = 15672
username = "lolz"
password = "lolz"
vhost = '/'

[staging]
hostname = "192.168.20.31"
port = 15672
username = "staging-2387a72329"
password = "staging-1d20cfbd9d"

[production]
hostname = "(redacted)"
port = 15671
username = "user-efe1f4d763f6"
password = "(redacted)"

rabbitmqadmin v1

rabbitmqadmin v1 是 HTTP API 的原始 CLI 工具,历史上作为 管理插件 的一部分开发并随其分发。

它已不再处于活跃开发状态。

获取 rabbitmqadmin v1

重要

请考虑切换到 rabbitmqadmin v2。原始的 rabbitmqadmin 已不再处于活跃开发状态。

可以从任何启用了管理插件的 RabbitMQ 节点下载 rabbitmqadmin v1。导航到 http://{hostname}:15672/cli/rabbitmqadmin 即可下载。该工具需要安装支持的 Python 版本。

或者,也可以 从 GitHub 下载 rabbitmqadmin v1。

© . This site is unofficial and not affiliated with VMware.