gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:32 26 4
gpt4 key购买 nike

我正在尝试使用 streadway/amqp 连接到 RabbitMQ 总线Go 的驱动程序。我正在处理重新连接例程,为此,我有一个 rabbitMQConsume 函数调用 rabbitMQConnect 函数。

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
}

我遇到的问题是 chconnrabbitMQconnect 返回到 rabbitMQConsume 时的值nil 并且当我运行 ch.Consume 行时程序出现困惑。我是基于 this example有任何想法吗?谢谢!

最佳答案

您在 amqp.DialConfig 之后的错误检查中有错别字!

err == nil 更改为 err != nil

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

关于go - 使用 Go RabbitMQ streadway/amqp 驱动程序时无法从函数返回 *amqp.Channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com