跳到主要内容
版本:4.1

用户资源限制

概述

类似于虚拟主机资源限制,可以限制特定用户可以打开的连接数和通道数。

这些限制可以用作在应用程序不可信任且无法详细监控的环境中的保护措施,例如,当 RabbitMQ 集群作为服务提供时。

可以使用rabbitmqctl set_user_limitsHTTP API 配置这些限制。

最大连接数

要限制用户可以打开的连接数,请将 max-connections 限制设置为正整数

rabbitmqctl set_user_limits user1 '{"max-connections": 10}'

要通过 HTTP API 设置限制,请使用以下端点

PUT /api/user-limits/{username}/{limit}

以及像这样的请求体

{"value": 20}

这是一个使用 curl 的例子

# using the HTTP API
curl -v -u guest:guest -X PUT http://localhost:15672/api/user-limits/user1/max-connections \
-H "content-type: application/json" \
-d @- <<EOF
{
"value": 20
}
EOF

最大通道数

要限制用户总共可以打开的通道数,请将 max-channels 限制设置为正整数

# using CLI tools
rabbitmqctl set_user_limits guest '{"max-connections": 10, "max-channels": 20}'

要通过 HTTP API 设置限制,请使用以下端点

PUT /api/user-limits/{username}/{limit}

以及像这样的请求体

{"value": 20}

这是一个使用 curl 为用户 user1 设置限制的例子

# using the HTTP API
curl -v -u guest:guest -X PUT http://localhost:15672/api/user-limits/user1/max-channels \
-H "content-type: application/json" \
-d @- <<EOF
{
"value": 20
}
EOF

该限制应用于用户打开的所有连接的通道总数。因此,它必须等于或大于前面提到的最大连接数限制。

清除用户限制

要清除用户的所有限制,请使用 rabbitmqctl clear_user_limitsHTTP API

这里有一些清除用户 user1 所有限制的例子

# clears the maximum number of connections limit
rabbitmqctl clear_user_limits user1 'max-connections'

# clears the maximum number of channels limit
rabbitmqctl clear_user_limits user1 'max-channels'

# clears all limits in a single operation
rabbitmqctl clear_user_limits user1 all

要通过 HTTP API 清除限制,请使用以下端点

DELETE /api/user-limits/{username}/{limit}

没有请求体。

这是一个使用 curl 清除用户 user1 所有限制的例子

# using the HTTP API
curl -v -u guest:guest -X DELETE http://localhost:15672/api/user-limits/user1/max-channels

curl -v -u guest:guest -X DELETE http://localhost:15672/api/user-limits/user1/max-connections
© . All rights reserved.