How to set connection friendly name

情到浓时终转凉″ 提交于 2020-06-17 12:56:31

问题


I have used 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?

The current name for a connection is "ip:port" , but can become better when you have your chosen name from the same ip.

Is there any way to set my friendly name?


回答1:


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:

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.


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.

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)
}

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

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:



来源:https://stackoverflow.com/questions/52568636/how-to-set-connection-friendly-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!