我正在尝试使用 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 datafunc 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
1 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
之后您的错误检查中有错字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
}
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报
0/150
提交
取消