Go amqp方法列出当前声明的所有队列的方式是什么?

huangapple go评论74阅读模式
英文:

Go amqp method to list all currently declared queues?

问题

我正在使用streadway/amqp库将RabbitMQ与我们的警报系统进行连接。我需要一个方法,可以返回当前声明的所有队列的列表(如果能返回交换机也不错!),这样我就可以遍历并获取所有消息的计数。

我正在查阅这里的API文档...

http://godoc.org/github.com/streadway/amqp#Queue

...但是我似乎找不到我要找的内容。我们目前使用一个bash调用'rabbitmqctl list_queues'来获取这些信息,但这是一种笨拙的方式,需要自定义sudo设置,并且每天会触发数百条日志记录到安全日志中。

编辑:方法指的是“获取这个信息的方式”,而不是一个实际的调用,尽管一个调用会很好,但我不认为这样的调用存在。

英文:

I'm using streadway/amqp to do a tie in from rabbitmq to our alert system. I need a method that can return a list of all the currently declared queues (exchanges would be nice too!) so that I can go through and get all the message counts.

I'm digging through the api documentation here...

http://godoc.org/github.com/streadway/amqp#Queue

...but I don't seem to be finding what I'm looking for. We're currently using a bash call to 'rabbitmqctl list_queues' but that's a kludge way to get this information, requires a custom sudo setting, and fires off hundreds of log entries a day to the secure log.

edit: method meaning, 'a way to get this piece of information' as opposed to an actual call, though a call would be great I don't believe it exists.

答案1

得分: 11

回答了自己的问题。没有办法!AMQP规范没有一种标准的方法来找到这个信息,这对我来说似乎是一个明显的疏忽。然而,由于我的后端是带有管理插件的RabbitMQ,我可以调用该插件来获取这些信息。

从https://stackoverflow.com/a/21286370/5076297(使用Python,我只需要翻译这个并且可能还要找出获取虚拟主机的调用):

import requests

def rest_queue_list(user='guest', password='guest', host='localhost', port=15672, virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, password))
    queues = [q['name'] for q in response.json()]
    return queues

在Go语言中(这让我头疼了一阵,因为我多年没有使用结构体了):

package main

import (
	"fmt"
	"net/http"
	"encoding/json"
)

func main() {
	
	type Queue struct {
		Name string `json:name`
		VHost string `json:vhost`
	}
	
	manager := "http://127.0.0.1:15672/api/queues/"
	client := &http.Client{}
	req, _ := http.NewRequest("GET", manager, nil)
	req.SetBasicAuth("guest", "guest")
	resp, _ := client.Do(req)
	
	value := make([]Queue, 0)
	json.NewDecoder(resp.Body).Decode(&value)
	fmt.Println(value)
}

输出结果如下(我有两个名为hello和test的队列):

[{hello /} {test /}]
英文:

Answered my own question. There isn't a way! The amqp spec doesn't have a standard way of finding this out which seems like a glaring oversight to me. However, since my backend is rabbitmq with the management plugin, I can make a call to that to get this information.

from https://stackoverflow.com/a/21286370/5076297 (in python, I'll just have to translate this and probably also figure out the call to get vhosts):

import requests

def rest_queue_list(user='guest', password='guest', host='localhost', port=15672, virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, password))
    queues = [q['name'] for q in response.json()]
    return queues

edit: In golang (this was a headache to figure out as I haven't done anything with structures in years)

package main

import (
	"fmt"
	"net/http"
	"encoding/json"
)

func main() {
	
	type Queue struct {
		Name string `json:name`
		VHost string `json:vhost`
	}
	
	manager := "http://127.0.0.1:15672/api/queues/"
	client := &http.Client{}
	req, _ := http.NewRequest("GET", manager, nil)
	req.SetBasicAuth("guest", "guest")
	resp, _ := client.Do(req)
	
	value := make([]Queue, 0)
	json.NewDecoder(resp.Body).Decode(&value)
	fmt.Println(value)
}

Output looks like this (I have two queues named hello and test)

[{hello /} {test /}]

huangapple
  • 本文由 发表于 2015年8月11日 08:19:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/31931215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定