每用户资源限制
概述
与每个虚拟主机资源限制类似,可以限制特定用户可以打开的连接和通道数量。
在无法信任并详细监控应用程序的环境中,例如 RabbitMQ 集群作为服务提供时,可以使用这些限制作为保护措施。
可以使用 CLI 工具或HTTP API 来配置这些限制。
最大连接数
要限制用户可以打开的连接数,请将 max-connections 限制设置为正整数
- 使用 rabbitmqctl (bash)
- 使用 rabbitmqadmin (bash)
- 使用 rabbitmqctl (PowerShell)
- 使用 rabbitmqadmin (PowerShell)
- HTTP API
rabbitmqctl set_user_limits user1 '{"max-connections": 10}'
rabbitmqadmin user_limits declare --user user1 --name max-connections --value 10
rabbitmqctl.bat set_user_limits user1 "{""max-connections"": 10}"
rabbitmqadmin.exe user_limits declare --user user1 --name max-connections --value 10
使用 PUT /api/user-limits/{username}/{limit} 端点,请求正文如下
{"value": 20}
这是一个使用 curl 的示例
curl -v -u guest:guest -X PUT https://:15672/api/user-limits/user1/max-connections \
-H "content-type: application/json" \
-d @- <<EOF
{
"value": 20
}
EOF
最大通道数
要限制用户总共可以打开的通道数,请将 max-channels 限制设置为正整数
- 使用 rabbitmqctl (bash)
- 使用 rabbitmqadmin (bash)
- 使用 rabbitmqctl (PowerShell)
- 使用 rabbitmqadmin (PowerShell)
- HTTP API
rabbitmqctl set_user_limits guest '{"max-connections": 10, "max-channels": 20}'
rabbitmqadmin user_limits declare --user guest --name max-channels --value 20
rabbitmqctl.bat set_user_limits guest "{""max-connections"": 10, ""max-channels"": 20}"
rabbitmqadmin.exe user_limits declare --user guest --name max-channels --value 20
使用 PUT /api/user-limits/{username}/{limit} 端点,请求正文如下
{"value": 20}
这是一个使用 curl 为用户 user1 设置限制的示例
curl -v -u guest:guest -X PUT https://:15672/api/user-limits/user1/max-channels \
-H "content-type: application/json" \
-d @- <<EOF
{
"value": 20
}
EOF
此限制应用于用户打开的所有连接的总通道数。因此,它必须等于或大于上述最大连接数限制。
列出用户限制
列出特定用户的限制
- 使用 rabbitmqctl (bash)
- 使用 rabbitmqadmin (bash)
- 使用 rabbitmqctl (PowerShell)
- 使用 rabbitmqadmin (PowerShell)
- HTTP API
rabbitmqctl list_user_limits user1
rabbitmqadmin user_limits list --user user1
rabbitmqctl.bat list_user_limits user1
rabbitmqadmin.exe user_limits list --user user1
curl -u guest:guest -X GET https://:15672/api/user-limits/user1
清除用户限制
要清除用户的限制,请使用 CLI 工具或HTTP API。
- 使用 rabbitmqctl (bash)
- 使用 rabbitmqadmin (bash)
- 使用 rabbitmqctl (PowerShell)
- 使用 rabbitmqadmin (PowerShell)
- HTTP API
# 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
# clears the maximum number of connections limit
rabbitmqadmin user_limits delete --user user1 --name max-connections
# clears the maximum number of channels limit
rabbitmqadmin user_limits delete --user user1 --name max-channels
# clears the maximum number of connections limit
rabbitmqctl.bat clear_user_limits user1 "max-connections"
# clears the maximum number of channels limit
rabbitmqctl.bat clear_user_limits user1 "max-channels"
# clears all limits in a single operation
rabbitmqctl.bat clear_user_limits user1 all
# clears the maximum number of connections limit
rabbitmqadmin.exe user_limits delete --user user1 --name max-connections
# clears the maximum number of channels limit
rabbitmqadmin.exe user_limits delete --user user1 --name max-channels
使用 DELETE /api/user-limits/{username}/{limit} 端点,不带请求正文。
这是一个使用 curl 清除用户 user1 所有限制的示例
curl -v -u guest:guest -X DELETE https://:15672/api/user-limits/user1/max-channels
curl -v -u guest:guest -X DELETE https://:15672/api/user-limits/user1/max-connections