Appearance
RabbitMQ HTTP API 参考
概述
本文档提供 RabbitMQ HTTP API 的完整参考,包括 API 端点、请求格式和响应示例。
API 基础
基本信息
| 属性 | 值 |
|---|---|
| 基础 URL | http://localhost:15672/api |
| 认证方式 | Basic Auth |
| 默认用户 | guest / guest |
| 数据格式 | JSON |
认证方式
bash
# Basic Auth
curl -u guest:guest http://localhost:15672/api/overview
# 使用 Token(需配置 OAuth2)
curl -H "Authorization: Bearer <token>" http://localhost:15672/api/overview概览 API
GET /api/overview
获取系统概览信息。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/overview响应示例:
json
{
"rabbitmq_version": "3.12.0",
"erlang_version": "25.3",
"cluster_name": "rabbit@localhost",
"management_version": "3.12.0",
"rates_mode": "basic",
"message_stats": {
"publish": 1000,
"deliver": 950,
"ack": 900
},
"queue_totals": {
"messages": 50,
"messages_ready": 30,
"messages_unacknowledged": 20
},
"object_totals": {
"consumers": 10,
"queues": 5,
"exchanges": 8,
"connections": 3
}
}节点 API
GET /api/nodes
获取所有节点信息。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/nodesGET /api/nodes/
获取指定节点信息。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/nodes/rabbit@localhost响应示例:
json
{
"name": "rabbit@localhost",
"type": "disc",
"running": true,
"mem_used": 67108864,
"mem_limit": 4294967296,
"mem_alarm": false,
"disk_free": 107374182400,
"disk_free_limit": 50000000,
"disk_free_alarm": false,
"fd_used": 50,
"fd_total": 1024,
"sockets_used": 10,
"sockets_total": 829,
"proc_used": 200,
"proc_total": 1048576
}连接 API
GET /api/connections
获取所有连接。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/connectionsGET /api/connections/
获取指定连接详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/connections/127.0.0.1:5672 -> 127.0.0.1:12345DELETE /api/connections/
关闭指定连接。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/connections/{name}响应示例:
json
{
"name": "127.0.0.1:5672 -> 127.0.0.1:12345",
"host": "127.0.0.1",
"port": 5672,
"peer_host": "127.0.0.1",
"peer_port": 12345,
"user": "guest",
"vhost": "/",
"protocol": "AMQP 0-9-1",
"channels": 2,
"client_properties": {
"product": "RabbitMQ",
"version": "3.12.0"
}
}通道 API
GET /api/channels
获取所有通道。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/channelsGET /api/channels/
获取指定通道详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/channels/{name}队列 API
GET /api/queues
获取所有队列。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/queuesGET /api/queues/
获取指定虚拟主机的队列。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/queues/%2fGET /api/queues/{vhost}/
获取指定队列详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/queues/%2f/my_queue响应示例:
json
{
"name": "my_queue",
"vhost": "/",
"durable": true,
"auto_delete": false,
"exclusive": false,
"messages": 100,
"messages_ready": 80,
"messages_unacknowledged": 20,
"consumers": 2,
"memory": 10240,
"message_bytes": 5120,
"type": "classic"
}PUT /api/queues/{vhost}/
创建队列。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{"auto_delete":false,"durable":true,"arguments":{}}' \
http://localhost:15672/api/queues/%2f/my_queueDELETE /api/queues/{vhost}/
删除队列。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/queues/%2f/my_queueDELETE /api/queues/{vhost}/{name}/contents
清空队列消息。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/queues/%2f/my_queue/contentsPOST /api/queues/{vhost}/{name}/get
获取队列中的消息。
请求示例:
bash
curl -u guest:guest -X POST \
-H "Content-Type: application/json" \
-d '{"count":5,"ackmode":"ack_requeue_false","encoding":"auto"}' \
http://localhost:15672/api/queues/%2f/my_queue/get响应示例:
json
[
{
"payload_bytes": 12,
"redelivered": false,
"exchange": "",
"routing_key": "my_queue",
"message_count": 99,
"properties": {
"content_type": "text/plain",
"delivery_mode": 2
},
"payload": "Hello World",
"payload_encoding": "string"
}
]交换机 API
GET /api/exchanges
获取所有交换机。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/exchangesGET /api/exchanges/
获取指定虚拟主机的交换机。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/exchanges/%2fGET /api/exchanges/{vhost}/
获取指定交换机详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/exchanges/%2f/my_exchangePUT /api/exchanges/{vhost}/
创建交换机。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{"type":"direct","durable":true,"auto_delete":false,"arguments":{}}' \
http://localhost:15672/api/exchanges/%2f/my_exchangeDELETE /api/exchanges/{vhost}/
删除交换机。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/exchanges/%2f/my_exchangePOST /api/exchanges/{vhost}/{name}/publish
发布消息。
请求示例:
bash
curl -u guest:guest -X POST \
-H "Content-Type: application/json" \
-d '{
"properties": {},
"routing_key": "my_queue",
"payload": "Hello World",
"payload_encoding": "string"
}' \
http://localhost:15672/api/exchanges/%2f/amq.default/publish响应示例:
json
{
"routed": true
}绑定 API
GET /api/bindings
获取所有绑定。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/bindingsGET /api/bindings/
获取指定虚拟主机的绑定。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/bindings/%2fPOST /api/bindings/{vhost}/e/{exchange}/q/
创建交换机到队列的绑定。
请求示例:
bash
curl -u guest:guest -X POST \
-H "Content-Type: application/json" \
-d '{"routing_key":"my_key","arguments":{}}' \
http://localhost:15672/api/bindings/%2f/e/my_exchange/q/my_queueDELETE /api/bindings/{vhost}/e/{exchange}/q/{queue}/
删除绑定。
请求示例:
bash
curl -u guest:guest -X DELETE \
http://localhost:15672/api/bindings/%2f/e/my_exchange/q/my_queue/props用户 API
GET /api/users
获取所有用户。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/usersGET /api/users/
获取指定用户详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/users/adminPUT /api/users/
创建或更新用户。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{"password":"secret","tags":"administrator"}' \
http://localhost:15672/api/users/adminDELETE /api/users/
删除用户。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/users/admin权限 API
GET /api/permissions
获取所有权限。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/permissionsGET /api/permissions/{vhost}/
获取指定权限详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/permissions/%2f/adminPUT /api/permissions/{vhost}/
设置权限。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{"configure":".*","write":".*","read":".*"}' \
http://localhost:15672/api/permissions/%2f/adminDELETE /api/permissions/{vhost}/
删除权限。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/permissions/%2f/admin虚拟主机 API
GET /api/vhosts
获取所有虚拟主机。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/vhostsGET /api/vhosts/
获取指定虚拟主机详情。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/vhosts/%2fPUT /api/vhosts/
创建虚拟主机。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{"description":"Production vhost","tags":"production"}' \
http://localhost:15672/api/vhosts/productionDELETE /api/vhosts/
删除虚拟主机。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/vhosts/production策略 API
GET /api/policies
获取所有策略。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/policiesPUT /api/policies/{vhost}/
创建或更新策略。
请求示例:
bash
curl -u guest:guest -X PUT \
-H "Content-Type: application/json" \
-d '{
"pattern":"^ha\\.",
"definition":{"ha-mode":"all","ha-sync-mode":"automatic"},
"priority":0,
"apply-to":"all"
}' \
http://localhost:15672/api/policies/%2f/ha-allDELETE /api/policies/{vhost}/
删除策略。
请求示例:
bash
curl -u guest:guest -X DELETE http://localhost:15672/api/policies/%2f/ha-all参数 API
GET /api/parameters
获取所有参数。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/parameters健康检查 API
GET /api/aliveness-test/
虚拟主机存活检查。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/aliveness-test/%2f响应示例:
json
{
"status": "ok"
}GET /api/health/checks/alarms
检查告警状态。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/health/checks/alarmsGET /api/health/checks/local-alarms
检查本地告警状态。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/health/checks/local-alarmsGET /api/health/checks/certificate-expiration
检查证书过期。
请求示例:
bash
curl -u guest:guest http://localhost:15672/api/health/checks/certificate-expirationHTTP 状态码
| 状态码 | 说明 |
|---|---|
| 200 | 成功 |
| 201 | 创建成功 |
| 204 | 成功(无返回内容) |
| 400 | 请求参数错误 |
| 401 | 认证失败 |
| 403 | 权限不足 |
| 404 | 资源不存在 |
| 409 | 资源冲突 |
| 500 | 服务器内部错误 |
使用示例
Python 示例
python
import requests
from requests.auth import HTTPBasicAuth
base_url = "http://localhost:15672/api"
auth = HTTPBasicAuth("guest", "guest")
# 获取概览
response = requests.get(f"{base_url}/overview", auth=auth)
print(response.json())
# 创建队列
requests.put(
f"{base_url}/queues/%2f/my_queue",
auth=auth,
json={"durable": True, "auto_delete": False}
)
# 发布消息
requests.post(
f"{base_url}/exchanges/%2f/amq.default/publish",
auth=auth,
json={
"routing_key": "my_queue",
"payload": "Hello World",
"payload_encoding": "string"
}
)JavaScript 示例
javascript
const axios = require('axios');
const api = axios.create({
baseURL: 'http://localhost:15672/api',
auth: {
username: 'guest',
password: 'guest'
}
});
// 获取概览
async function getOverview() {
const response = await api.get('/overview');
return response.data;
}
// 创建队列
async function createQueue(name) {
await api.put(`/queues/%2f/${name}`, {
durable: true,
auto_delete: false
});
}
// 发布消息
async function publishMessage(queue, message) {
await api.post('/exchanges/%2f/amq.default/publish', {
routing_key: queue,
payload: message,
payload_encoding: 'string'
});
}注意事项
- URL 编码: 虚拟主机名需要 URL 编码,如
/编码为%2f - 认证安全: 生产环境不要使用默认用户
- HTTPS: 生产环境建议启用 HTTPS
- API 限流: 注意管理 API 的请求频率限制
