go stomp 客户端中 ActiveMQ 的故障转移 URI

failover URI for ActiveMQ in go stomp client

我们如何在 Go 中使用故障转移 stomp 连接 URI 连接到 ActiveMQ? 使用 Go-Stomp 客户端,我尝试了以下代码但无法连接。

if conn, err = stomp.Dial("tcp", "failover:(tcp://10.01.02.03:61613,tcp://10.04.05.06:61613)?startupMaxReconnectAttempts=2"); err != nil {
        panic(fmt.Sprintf("Could not connect to ActiveMQ using brokerUri %v. Can not continue.", Config.Broker.URI))
    }

您收到的错误是什么? 我不相信你的拨号格式是正确的: Go-Stomp Dial 使用底层 net.Dial

func Dial(network, addr string, opts ...func(*Conn) error) (*Conn, error) {

c, err := net.Dial(网络, 地址)

基础 net.Dial 文档说明

对于 TCP 和 UDP 网络,地址的格式为 host:port。如果主机是文字 IPv6 地址,则必须将其括在方括号中,如“[::1]:80”或“[ipv6-host%zone]:80”。函数 JoinHostPort 和 SplitHostPort 以这种形式处理地址。如果主机为空,如“:80”,则假定为本地系统。

没有故障转移:语法

由于不支持故障转移,不得不编写一些代码来实现预期的结果。

//connect to ActiveMQ using failover approach
    var err error
    for _, uri := range ["10.01.02.03:61613","10.04.05.06:61613", {
        if err = connect(uri); err == nil {
            break
        }
    }
    if conn == nil {
        panic(fmt.Sprintf("Could not connect to ActiveMQ using brokerUri. Can not continue."))
    }

func connect(brokerIp string) (err error) {
    log.Printf("Attempting to connect to ActiveMQ node %v", brokerIp)
    if conn, err = stomp.Dial("tcp",
        brokerIp,
        stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
        log.Printf("Faild to connect to ActiveMQ using %v", brokerIp)
    }
    if err == nil {
        log.Printf("Successfully connected to ActiveMQ node %v", brokerIp)
    }
    return
}