Golang – RabbitMq: 通道/连接未打开

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

Golang - RabbitMq : channel/connection is not open

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习Go语言,我想重构我的代码,将RabbitMQ的初始化放在main函数之外的另一个函数中。所以我使用了一个结构体指针(包含了所有初始化的RabbitMQ信息),并将其传递给发送消息的函数。但是它告诉我:发布消息失败:异常(504)原因:"channel/connection is not open"。

结构体:

type RbmqConfig struct {
    q      amqp.Queue
    ch     *amqp.Channel
    conn   *amqp.Connection
    rbmqErr error
}

初始化函数:

func initRabbitMq() *RbmqConfig {
    config := &RbmqConfig{}

    config.conn, config.rbmqErr = amqp.Dial("amqp://guest:guest@localhost:5672/")
    failOnError(config.rbmqErr, "Failed to connect to RabbitMQ")
    defer config.conn.Close()

    config.ch, config.rbmqErr = config.conn.Channel()
    failOnError(config.rbmqErr, "Failed to open a channel")
    defer config.ch.Close()

    config.q, config.rbmqErr = config.ch.QueueDeclare(
        "<my_queue_name>",
        true,   // durable
        false,  // delete when unused
        false,  // exclusive
        false,  // no-wait
        nil,    // arguments
    )
    failOnError(config.rbmqErr, "Failed to declare a queue")

    return config
}

主函数:

config := initRabbitMq()

fmt.Println("queue name: ", config.q.Name)

sendMessage(config, <message_to_send>)

发送消息的函数:

func sendMessage(config *RbmqConfig, <message_to_send>) {
    config.rbmqErr = config.ch.Publish(
        "",                 // exchange
        config.q.Name,      // routing key
        false,              // mandatory
        false,
        amqp.Publishing{
            DeliveryMode: amqp.Persistent,
            ContentType:  "text/plain",
            Body:         []byte(<message_to_send>),
        })
    failOnError(config.rbmqErr, "Failed to publish a message")
}

如果有人有任何想法,那将非常有帮助。提前谢谢你!

英文:

I'm new to golang, and I would like to refactorate my code so that the rabbitmq initialization is in another function that main. So I use a struct pointer (containing all the rabbitmq infos initilized) and pass it to the send function, but it tells me : Failed to publish a message: Exception (504) Reason: "channel/connection is not open"

struct :

type RbmqConfig struct {
    q amqp.Queue
    ch *amqp.Channel
    conn *amqp.Connection
    rbmqErr error
}

the init function :

func initRabbitMq() *RbmqConfig {

    config := &amp;RbmqConfig{}

    config.conn, config.rbmqErr = amqp.Dial(&quot;amqp://guest:guest@localhost:5672/&quot;)
    failOnError(config.rbmqErr, &quot;Failed to connect to RabbitMQ&quot;)
    defer config.conn.Close()

    config.ch, config.rbmqErr = config.conn.Channel()
    failOnError(config.rbmqErr, &quot;Failed to open a channel&quot;)
    defer config.ch.Close()

    config.q, config.rbmqErr = config.ch.QueueDeclare(
	    &quot;&lt;my_queue_name&gt;&quot;,
	    true,   // durable
	    false,   // delete when unused
	    false,   // exclusive
	    false,   // no-wait
	    nil,     // arguments
    )
    failOnError(config.rbmqErr, &quot;Failed to declare a queue&quot;)

    return config
}

main :

config := initRabbitMq()

fmt.Println(&quot;queue name : &quot;, config.q.Name)

sendMessage(config, &lt;message_to_send&gt;)

in send message :

func sendMessage(config *RbmqConfig, &lt;message_to_send&gt;) {

    config.rbmqErr = config.ch.Publish(
	    &quot;&quot;,           // exchange
	    config.q.Name,       // routing key
	    false,        // mandatory
	    false,
	    amqp.Publishing{
		    DeliveryMode: amqp.Persistent,
		    ContentType:  &quot;text/plain&quot;,
		    Body:         []byte(&lt;message_to_send&gt;),
	    })
    failOnError(config.rbmqErr, &quot;Failed to publish a message&quot;)

If someone has any idea, that would be very helpful. Thank you in advance

答案1

得分: 15

在你的init函数中,你写了defer config.conn.Close(),这意味着当函数返回时会执行该语句。也就是说,无论何时init函数完成,你的连接都会被关闭,这导致了未打开的连接。

你需要在main函数或者你希望关闭连接的其他地方延迟关闭连接。

英文:

Inside your init, you wrote defer config.conn.Close(), which will be executed when the function return. That is to say, whenever init finished, your connection will be closed, which causes unopen connection.

You need to defer the connection closing in main, or somewhere you want it to be closed.

huangapple
  • 本文由 发表于 2016年4月13日 01:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/36579759.html
匿名

发表评论

匿名网友

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

确定