我们如何在 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))
}
最佳答案
由于缺乏对故障转移的支持,不得不编写一些代码来达到预期的结果。
//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
}
关于go - go stomp 客户端中 ActiveMQ 的故障转移 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896944/