我正在尝试使用 Go 的streadway/amqp驱动程序连接到 RabbitMQ 总线。我正在研究一个重新连接例程,为此,我有一个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
}
问题是,当我运行该行时,它们的值ch和返回到from时的值和程序会出现恐慌。我迷失地基于这个例子有 什么想法吗?谢谢!connrabbitMQConsumerabbitMQconnectnilch.Consume
幕布斯6054654
相关分类