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

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

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...

  1. 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,我只需要翻译这个并且可能还要找出获取虚拟主机的调用):

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "encoding/json"
  6. )
  7. func main() {
  8. type Queue struct {
  9. Name string `json:name`
  10. VHost string `json:vhost`
  11. }
  12. manager := "http://127.0.0.1:15672/api/queues/"
  13. client := &http.Client{}
  14. req, _ := http.NewRequest("GET", manager, nil)
  15. req.SetBasicAuth("guest", "guest")
  16. resp, _ := client.Do(req)
  17. value := make([]Queue, 0)
  18. json.NewDecoder(resp.Body).Decode(&value)
  19. fmt.Println(value)
  20. }

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

  1. [{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):

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "encoding/json"
  6. )
  7. func main() {
  8. type Queue struct {
  9. Name string `json:name`
  10. VHost string `json:vhost`
  11. }
  12. manager := "http://127.0.0.1:15672/api/queues/"
  13. client := &http.Client{}
  14. req, _ := http.NewRequest("GET", manager, nil)
  15. req.SetBasicAuth("guest", "guest")
  16. resp, _ := client.Do(req)
  17. value := make([]Queue, 0)
  18. json.NewDecoder(resp.Body).Decode(&value)
  19. fmt.Println(value)
  20. }

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

  1. [{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:

确定