【问题标题】:RabbitMQ Closes connection after publishRabbitMQ 发布后关闭连接
【发布时间】:2021-11-25 22:31:57
【问题描述】:

我在 Golang 上使用 RabbitMQ,我发现自己遇到了一些以前从未遇到过的麻烦。在一个Publish 之后,连接会一直关闭。这是sn-p:


func (k*K) DoWork(){
[...]
go func() {
        for {
            time.Sleep(time.Second * 5)
            k.eventLock.Lock()
            if !k.connected {
                fmt.Printf("Not connected, skip\n")
                k.eventLock.Unlock()
                continue
            }
            k.eventLock.Unlock()
            err = k.outChannel.Publish(k.outExchangeName, "", true, true, amqp.Publishing{
                ContentType: "application/json",
                Body:        content,
            })

            if err != nil {
                fmt.Printf("Error sending status - %v\n", err)
                for {
                    err = k.initOutConnection()
                    if err != nil {
                        fmt.Printf("An error occurred initOut - %v\n", err)
                    } else {
                        fmt.Printf("Restablished connection\n")
                        break
                    }
                }
                continue
            } else {
                fmt.Printf("Sent keep alive\n")
            }
        }
    }()
}

func (k *K) initOutConnection() error {
    var err error
    k.outConnection, err = k.getRabbitConnection()

    if err != nil {
        return err
    }

    k.outChannel, err = k.outConnection.Channel()
    if err != nil {
        return err
    }

    k.outExchangeName = os.Getenv("RABBIT_MQ_OUT_EXCHANGE_NAME")

    err = k.outChannel.ExchangeDeclare(
        k.outExchangeName,
        "fanout",
        true,
        true,
        false,
        true, nil)

    if err != nil {
        return err
    }

    return nil
}

这是输出:

Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection
Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection
Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection
Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection
Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection
Sent keep alive
Error sending status - Exception (504) Reason: "channel/connection is not open"
Restablished connection

这是一个完美的循环,一次发送成功,连接关闭,我建立连接,然后又一次成功,一次失败。但这在生产者应用程序看来,消费者没有收到任何消息

如果我在一次发布后故意关闭连接,它确实有效:

err = k.outChannel.Publish()
if err == nil {
    k.outChannel.Close()
    k.initOutConnection()
    fmt.Printf("Sent keep alive\n")
}

它产生一致的输出:

Sent keep alive
Sent keep alive
Sent keep alive

但是我想为我的所有publish 使用一个连接,有人知道我做错了什么吗?

【问题讨论】:

    标签: go networking rabbitmq amqp


    【解决方案1】:

    您使用哪个 amqp 库?是“github.com/streadway/amqp”吗?

    我认为在您发送消息后连接不会关闭,除非您在代码中关闭连接/有一些错误的实现。可能发生的情况是传出通道无法发送消息,因为它处于确认模式。

    这是文档的摘录:

    由于发布是异步的,任何无法传递的消息都会被服务器返回。添加具有 Channel.NotifyReturn 的侦听器,以在使用强制参数或立即参数为 true 调用发布时处理任何无法传递的消息。

    文档:

    https://pkg.go.dev/github.com/streadway/amqp#Channel.Publish

    所以通常发布看起来像:

    channel := client.NotifyPublish(...)
    client.Publish(...)
    
    // wait for the channel
    

    来自存储库的示例:https://github.com/streadway/amqp/blob/master/_examples/simple-producer/producer.go

    【讨论】:

    • 您好,感谢您的建议,这个问题是一个布尔值,而不是 true 应该是 false,我将其添加为答案,所以没有人再为此浪费时间
    【解决方案2】:

    经过 1.5 天后,我终于想到在 Docker RabbitMQ 中启用调试日志记录,而无需与它分离:

    docker run --rm --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management
    

    又跑了上面的sn-p,果然出现了错误:

    Error on AMQP connection <0.1254.0> (172.17.0.1:41690 -> 172.17.0.2:5672, vhost: '/', user: 'guest', state: running), channel 1:
     operation basic.publish caused a connection exception not_implemented: "immediate=true"
    
    

    问题是.Publish() 的第四个参数是immediate,而不是真的我应该将它设置为false ... 连接确实在那里并且工作正常,只要我用immediate=true 调用发布它就关闭了连接,虽然我没有收到来自Publish 的错误消息没有到达它的交换目的地。

    这么简单,一个布尔值浪费了我无数个小时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2021-03-26
      • 2019-05-08
      • 1970-01-01
      相关资源
      最近更新 更多