无法在使用Go RabbitMQ streadway/amqp驱动程序时从函数返回*amqp.Channel。

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

Can't return *amqp.Channel from a function when using the Go RabbitMQ streadway/amqp driver

问题

我正在尝试使用Go语言的streadway/amqp驱动程序连接到RabbitMQ消息总线。我正在编写一个重新连接的例程,在这个例程中,我有一个rabbitMQConsume函数调用一个rabbitMQConnect函数。

问题是,当chconnrabbitMQConnect返回到rabbitMQConsume时,它们的值为nil,当我运行ch.Consume这一行时,程序会发生崩溃。我参考了这个示例。有什么想法吗?谢谢!

英文:

I'm trying to connect to a RabbitMQ bus using the streadway/amqp driver for Go. I'm working on a reconnection routine and, for it, I have a rabbitMQConsume function call a rabbitMQConnect function.

func rabbitMQConnect(cfg objects.GlobalConfig) (*amqp.Connection, *amqp.Channel, error) {
	rabbitConfig := amqp.Config{
		Vhost:     cfg.RabbitVHost,
		Heartbeat: 5,
	}

	//Open connection to Rabbit
	url := fmt.Sprintf("amqp://" + cfg.RabbitUser + ":" + cfg.RabbitPassword + "@" + cfg.RabbitHost + ":" + cfg.RabbitPort + cfg.RabbitVHost)

	conn, err := amqp.DialConfig(url, rabbitConfig)
	if err == nil {
		return nil, nil, err
	}

	ch, err := conn.Channel()
	if err != nil {
		return nil, nil, err
	}
	// Create Exchange if it doesn't exist
	err = ch.ExchangeDeclare(
		"ali",    // name
		"direct", // type
		true,     // durable
		false,    // auto-deleted
		false,    // internal
		false,    // no-wait
		nil,      // arguments
	)
	if err != nil {
		return nil, nil, err
	}

	//Declare queue
	_, err = ch.QueueDeclare(
		cfg.RabbitQueue, // name
		true,            // durable
		false,           // delete when usused
		false,           // exclusive
		false,           // no-wait
		nil,             // arguments
	)
	if err != nil {
		return nil, nil, err
	}

	//Bind queue
	err = ch.QueueBind(
		cfg.RabbitQueue,    // queue name
		cfg.RabbitKey,      // routing key
		cfg.RabbitExchange, // exchange
		false,
		nil,
	)
	if err != nil {
		return nil, nil, err
	}
	return conn, ch, nil
}

//RabbitMQConsume setup the channel/exchange data
func rabbitMQConsume(cfg objects.GlobalConfig) (*amqp.Connection, <-chan amqp.Delivery, error) {
	conn, ch, err := rabbitMQConnect(cfg)
	if err != nil {
		return nil, nil, err
	}

	consumerID, err := helper.GetConsumerID()
	if err != nil {
		return nil, nil, err
	}

	//Start receiving data in the msgs channel
	msgs, err := ch.Consume(
		cfg.RabbitQueue, // queue
		consumerID,      // consumer
		false,           // auto-ack
		false,           // exclusive
		false,           // no-local
		false,           // no-wait
		nil,             // args
	)
	if err != nil {
		return nil, nil, err
	}

	return conn, msgs, nil
}

The problem I'm having is that the value of ch and conn when they're returned to rabbitMQConsume from rabbitMQconnect, are nil and the program panics when I run the ch.Consume line.
I'm losely basing this on this example
Any ideas? Thanks!

答案1

得分: 1

amqp.DialConfig之后的错误检查中,你有一个拼写错误!

err == nil更改为err != nil

conn, err := amqp.DialConfig(url, rabbitConfig)
if err != nil { // 你将其写成了 err == nil
    return nil, nil, err
}

请注意,这是你要翻译的内容。

英文:

You have a typo in your error checking after amqp.DialConfig !

Change the err == nil to err != nil

conn, err := amqp.DialConfig(url, rabbitConfig)
if err != nil { // you typed it as err == nil
    return nil, nil, err
}

huangapple
  • 本文由 发表于 2016年11月11日 02:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/40534860.html
匿名

发表评论

匿名网友

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

确定