ActiveMQ在Go Stomp客户端中的故障转移URI

huangapple go评论74阅读模式
英文:

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(&quot;tcp&quot;, &quot;failover:(tcp://10.01.02.03:61613,tcp://10.04.05.06:61613)?startupMaxReconnectAttempts=2&quot;); err != nil {
		panic(fmt.Sprintf(&quot;Could not connect to ActiveMQ using brokerUri %v. Can not continue.&quot;, Config.Broker.URI))
	}

答案1

得分: 1

由于缺乏故障转移的支持,必须编写一些代码来实现所需的结果。

<!-- language : go -->

//使用故障转移方法连接到ActiveMQ
	var err error
	for _, uri := range [&quot;10.01.02.03:61613&quot;,&quot;10.04.05.06:61613&quot;, {
		if err = connect(uri); err == nil {
			break
		}
	}
	if conn == nil {
		panic(fmt.Sprintf(&quot;无法使用brokerUri连接到ActiveMQ。无法继续。&quot;))
	}

func connect(brokerIp string) (err error) {
	log.Printf(&quot;尝试连接到ActiveMQ节点 %v&quot;, brokerIp)
	if conn, err = stomp.Dial(&quot;tcp&quot;,
		brokerIp,
		stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
		log.Printf(&quot;使用 %v 连接到ActiveMQ失败&quot;, brokerIp)
	}
	if err == nil {
		log.Printf(&quot;成功连接到ActiveMQ节点 %v&quot;, 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 [&quot;10.01.02.03:61613&quot;,&quot;10.04.05.06:61613&quot;, {
		if err = connect(uri); err == nil {
			break
		}
	}
	if conn == nil {
		panic(fmt.Sprintf(&quot;Could not connect to ActiveMQ using brokerUri. Can not continue.&quot;))
	}

func connect(brokerIp string) (err error) {
	log.Printf(&quot;Attempting to connect to ActiveMQ node %v&quot;, brokerIp)
	if conn, err = stomp.Dial(&quot;tcp&quot;,
		brokerIp,
		stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
		log.Printf(&quot;Faild to connect to ActiveMQ using %v&quot;, brokerIp)
	}
	if err == nil {
		log.Printf(&quot;Successfully connected to ActiveMQ node %v&quot;, 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

huangapple
  • 本文由 发表于 2016年3月10日 00:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35896944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定