英文:
failover URI for ActiveMQ in go stomp client
问题
我们如何在Go中使用failover 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))
}
英文:
How do we connect to ActiveMQ using failover stomp connection URI in Go?
Using <a href="http://github.com/go-stomp/stomp">Go-Stomp</a> client, I tried below code and it fails to connect.
<!-- language: go -->
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))
}
答案1
得分: 1
由于缺乏故障转移的支持,必须编写一些代码来实现所需的结果。
<!-- language : go -->
//使用故障转移方法连接到ActiveMQ
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("无法使用brokerUri连接到ActiveMQ。无法继续。"))
}
func connect(brokerIp string) (err error) {
log.Printf("尝试连接到ActiveMQ节点 %v", brokerIp)
if conn, err = stomp.Dial("tcp",
brokerIp,
stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
log.Printf("使用 %v 连接到ActiveMQ失败", brokerIp)
}
if err == nil {
log.Printf("成功连接到ActiveMQ节点 %v", brokerIp)
}
return
}
英文:
Due to lack of support for failover, had to write some code to achieve the desired result.
<!-- language : go -->
//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
}
答案2
得分: 0
你收到的错误是什么?
我不认为你的Dial格式是正确的:
Go-Stomp Dial使用底层的net.Dial函数。
func Dial(network, addr string, opts ...func(*Conn) error) (*Conn, error) {
c, err := net.Dial(network, addr)
底层的net.Dial文档说明:
对于TCP和UDP网络,地址的格式为host:port。如果host是一个字面上的IPv6地址,它必须用方括号括起来,例如"[::1]:80"或"[ipv6-host%zone]:80"。函数JoinHostPort和SplitHostPort可以操作这种形式的地址。如果host为空,例如":80",则假定为本地系统。
没有failover:语法。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论