我在 Golang 上使用 RabbitMQ,但我發現自己遇到了以前從未遇到過的麻煩。一次后連接一直關閉Publish。這是片段:
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,有人知道我做錯了什么嗎?
uj5u.com熱心網友回復:
您使用哪個 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
uj5u.com熱心網友回復:
在 1.5 天之后,我終于想到要在 Docker RabbitMQ 中啟用除錯日志記錄,而無需與它分離:
docker run --rm --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management
并運行上面的代碼片段,果然出現了一個錯誤:
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"
問題是第 4 個引數.Publish()是immediate,而不是 true 我應該將它設定為false......連接確實存在并且可以正常作業,只要我用immediate=true它呼叫 Publish就關閉了連接,盡管我沒有收到錯誤從Publish訊息沒有到達其交換目的地。
就這么簡單,一個布林值浪費了我無數個小時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369736.html
下一篇:將二進制檔案轉換為位元組陣列
