獲取rabbitmq的exchange列表和queue列表
阿新 • • 發佈:2019-01-02
API
安裝有rabbitmq_management(web管理外掛)訪問web首頁(一般是localhost:15672)時在首頁底部有個httpAPI連結。
API介面
該頁面列出來了很多API介面
看一下其中的exchanges和queues
檢視api是需要登入認證的
紅色框框是延時佇列
例子
有了API就可以抓取其中的資料
我是通過curl抓取的,下面是一個抓取exchanges列表的例子
抓取queues例子
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# author=He
import json
import subprocess
from log import log_base
from config import mq
class Exchanges:
def __new__(cls):
command = 'curl -u %s:%s http://localhost:15672/api/exchanges' % (mq.mq_user, mq.mq_pwd)
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
r = []
name = {}
try :
r = json.loads(result.stdout.readlines()[3].decode('utf-8'))
except IndexError as e:
log_base.Error(e, mark='exchange')
try:
for line in r:
if line['name']:
name[line['name']] = line['name']
except KeyError as e:
log_base.Error(e, mark='exchange')
return name
看一下結果
/usr/bin/python3.5 /home/he/dev/rabbit/rabbit/rabbit_base/exchanges.py
{'amq.direct': 'amq.direct', 'amq.fanout': 'amq.fanout', 'amq.topic': 'amq.topic', 'jit.tests': 'jit.tests', 'amq.headers': 'amq.headers', 'amq.match': 'amq.match', 'amq.rabbitmq.log': 'amq.rabbitmq.log', 'delay.in.test': 'delay.in.test', 'amq.rabbitmq.trace': 'amq.rabbitmq.trace', 'delay.test': 'delay.test'}
Process finished with exit code 0