duandi5328
2016-03-09 16:18
浏览 132
已采纳

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

How do we connect to ActiveMQ using failover stomp connection URI in Go? Using Go-Stomp client, I tried below code and it fails to connect.

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

图片转代码服务由CSDN问答提供 功能建议

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

  如果conn,err = stomp.Dial(“ tcp”,“故障转移:(tcp://10.01.02.03:61613,tcp://10.04.05.06:61613)?startupMaxReconnectAttempts = 2”);  err!= nil {
 panic(fmt.Sprintf(“无法使用brokerUri%v连接到ActiveMQ。无法继续。”,Config.Broker.URI))
} 
    
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshubang7816 2016-03-10 21:16
    已采纳

    Due to lack of support for failover, had to write some code to achieve the desired result.

    //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
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtx9931 2016-03-09 16:56

    What is the err you are receiving? I do not believe the format of your Dial is correct: The Go-Stomp Dial uses the underlying net.Dial

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

    c, err := net.Dial(network, addr)

    The underlying net.Dial documentation states

    For TCP and UDP networks, addresses have the form host:port. If host is a literal IPv6 address it must be enclosed in square brackets as in "[::1]:80" or "[ipv6-host%zone]:80". The functions JoinHostPort and SplitHostPort manipulate addresses in this form. If the host is empty, as in ":80", the local system is assumed.

    There is no failover: syntax

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题