在Go amqp客户端中设置连接友好名称

在Go amqp客户端中设置连接友好名称

问题描述:

我在我的应用程序中使用 http://github.com/streadway/amqp 包为了处理到远程RabbitMQ服务器的连接.一切正常,一切正常,但我有一个问题.

I am using the http://github.com/streadway/amqp package in my application in order to handle connections to a remote RabbitMQ server. Everything is ok and works fine, but I have a question.

连接的当前名称是"ip:端口",因此当来自同一IP +端口的多个连接时,它们几乎不可区分.如果您可以为每个连接指定一个名称,那就太好了.

The current name for a connection is "ip:port", so when there are multiple connections from the same IP+port, they become hardly distinguishable. It would be great if you can specify a name for each connection.

有什么办法为每个连接设置不同的友好名称吗?

Is there any way to set a distinct friendly name for each connection?

RabbitMQ 3.6.5添加了用于连接客户端报告友好名称字符串值的功能,以标识出于管理目的的连接.严格来说,这是一个标识符,并且正如客户端报告的那样,除了对连接的识别不力之外,不能依赖它. 发行说明状态:

RabbitMQ 3.6.5 added the facility for the connecting client to report a friendly name string value to identify a connection for management purposes. This is strictly an identifier and, as it is client-reported, it cannot be relied upon for anything other than weak identification of connections. The release notes state:

客户端现在可以提供人类可读的连接名称,该名称将显示在管理UI中.为了使用此功能,请在客户端属性中设置connection_name键.请注意,此名称不必唯一,也不能用作连接标识符,例如,在HTTP API请求中.

Clients now can provide a human-readable connection name that will be displayed in the management UI... In order to use this feature, set the connection_name key in client properties. Note that this name doesn’t have to be unique and cannot be used as a connection identifier, for example, in HTTP API requests.


解决方案

假设您使用的是RabbitMQ的较新版本,则在使用streadway/amqp建立连接时可以通过在进行初始连接时传递amqp.Config的实例来设置此参数. Properties字段允许指定连接的自定义属性.


Solution

Provided you are using a sufficiently new version of RabbitMQ, you can set this parameter when making connections using streadway/amqp by passing an instance of amqp.Config when making the initial connection. The Properties field allows custom properties of the connection to be specified.

下面的示例程序使用环境变量AMQP_URL中提供的AMQP URL打开连接,该URL使用作为第一个命令行参数传递给调用的连接名称进行标识.

The example program below opens a connection using the AMQP URL provided in the environment variable AMQP_URL, identified using the connection name passed as the first command line argument to the invocation.

package main

import (
    "log"
    "os"

    "github.com/streadway/amqp"
)

func main() {
    amqpUrl := os.Getenv("AMQP_URL")

    cfg := amqp.Config{
        Properties: amqp.Table{
            "connection_name": os.Args[1],
        },
    }

    conn, err := amqp.DialConfig(amqpUrl, cfg)
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    <-(chan struct{})(nil)
}

使用以下命令行启动多个实例以连接到本地RabbitMQ实例:

Starting multiple instances to connect to a local RabbitMQ instance using the following command line:

AMQP_URL=amqp://admin:password@localhost:5672 go run ./main.go connX

用数字代替X的结果在RabbitMQ管理Web UI的连接"页面中产生以下输出:

where a numeral is substituted for X yields the following output in the "Connections" page of the RabbitMQ Management web UI:

,各个连接的详细信息页面在客户端提供的名称"详细信息值下方显示该值:

and the individual connection detail pages shows the value under the "Client-provided name" detail value: