作者介绍:简历上没有一个精通的运维工程师。请点击上方的蓝色《运维小路》关注我,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。
中间件,我给它的定义就是为了实现某系业务功能依赖的软件,包括如下部分:
Web服务器
代理服务器
ZooKeeper
Kafka
RabbitMQ(本章节)
RabbitMQ Management HTTP API 终极指南
RabbitMQ 提供的 RESTful 接口,用于无需登录Web控制台即可管理集群、监控状态、操作资源。适合自动化运维、集成监控系统(如Prometheus/Zabbix)或自定义管理工具。
1.启用与认证
启用插件,是因为启用了插件才有暴露的http接口。
rabbitmq-plugins enable rabbitmq_management
2. 认证方式
启用web插件,我们前面就介绍过就涉及到账号密码的问题
所有 API 请求需添加 Basic Auth 头,当然也可以是其他定义的管理员账号。
#当然这个操作只能在本机执行
curl -u guest:guest http://localhost:15672/api/overview
3.主要功能
3.1 集群管理
端点 |
方法 |
功能描述 |
---|---|---|
/api/nodes |
GET |
获取所有节点信息 |
/api/nodes/{name} |
GET |
获取特定节点详细信息 |
/api/cluster-name |
GET |
获取集群名称 |
/api/healthchecks/node |
GET |
节点健康检查 |
3.2 虚拟主机(vhost)管理
端点 |
方法 |
功能描述 |
---|---|---|
/api/vhosts |
GET |
列出所有虚拟主机 |
/api/vhosts/{vhost} |
PUT |
创建虚拟主机 |
/api/vhosts/{vhost} |
DELETE |
删除虚拟主机 |
3.3 队列(queues)操作
端点 |
方法 |
功能描述 |
---|---|---|
/api/queues |
GET |
获取所有队列 |
/api/queues/{vhost}/{queue} |
PUT |
声明队列 |
/api/queues/{vhost}/{queue}/contents |
DELETE |
清除队列消息 |
/api/queues/{vhost}/{queue}/bindings |
GET |
获取队列绑定关系 |
3.4 交换机(exchanges)管理
端点 |
方法 |
功能描述 |
---|---|---|
/api/exchanges |
GET |
列出所有交换机 |
/api/exchanges/{vhost}/{exchange} |
PUT |
创建交换机 |
/api/exchanges/{vhost}/{exchange} |
DELETE |
删除交换机 |
3.5 消息操作
端点 |
方法 |
功能描述 |
---|---|---|
/api/queues/{vhost}/{queue}/get |
POST |
消费消息(拉取模式) |
/api/exchanges/{vhost}/{exchange}/publish |
POST |
发布消息 |
4.关键使用示例
4.1 创建虚拟主机
curl -u "guest:guest" \
-X PUT \
-H "Content-Type: application/json" \
-d '{"description": "My custom VHost"}' \
http://localhost:15672/api/vhosts/my_vhost
4.2 创建交换机
curl -u "guest:guest" \
-X PUT \
-H "Content-Type: application/json" \
-d '{
"type": "direct",
"durable": true
}' \
http://localhost:15672/api/exchanges/my_vhost/orders.direct
4.3 创建队列
curl -u "guest:guest" \
-X PUT \
-H "Content-Type: application/json" \
-d '{
"auto_delete": false,
"durable": true
}' \
http://localhost:15672/api/queues/my_vhost/my_queue
4.4 绑定
curl -u "guest:guest" \
-X POST \
-H "Content-Type: application/json" \
-d '{
"routing_key": "order.created",
"arguments": {}
}' \
http://localhost:15672/api/bindings/my_vhost/e/orders.direct/q/my_queue
4.5 发布消息
curl -u "guest:guest" \
-X POST \
-H "Content-Type: application/json" \
-d '{
"properties": {},
"routing_key": "order.created",
"payload": "Hello World",
"payload_encoding": "string"
}' \
http://localhost:15672/api/exchanges/my_vhost/orders.direct/publish
4.6 消费消息
curl -u "guest:guest" \
-X POST \
-H "Content-Type: application/json" \
-d '{
"count": 5,
"ackmode": "ack_requeue_true",
"encoding": "auto",
"truncate": 50000
}' \
http://localhost:15672/api/queues/my_vhost/my_queue/get
这里未发ACK确认。当然这里只列举部分功能,更多的功能希望大家多去尝试。
运维小路
一个不会开发的运维!一个要学开发的运维!一个学不会开发的运维!欢迎大家骚扰的运维!
关注微信公众号《运维小路》获取更多内容。