Rabbitmq 消费者 : Exception (504) Reason: “channel/connection is not open”
启动微服务 Rabbitmq 时,定位 ch.consume() 出现异常
一、问题分析
分析日志:no exchange ‘log_topic’文章来源:https://www.toymoban.com/news/detail-533868.html
> docker logs rabbitMQ_1
2023-02-10 04:17:17.221260+00:00 [error] <0.834.0> Channel error on connection <
0.819.0> (172.19.0.1:51534 -> 172.19.0.4:5672, vhost: '/', user: 'guest'), chann
el 2:
2023-02-10 04:17:17.221260+00:00 [error] <0.834.0> operation queue.bind caused a
channel exception not_found: no exchange 'log_topic' in vhost '/'
二、检查代码
- consumer.go 中,ch.QueueBind 绑定在 “log_topic”
func (consumer *Consumer) Listen(topics []string) error {
ch, err := consumer.conn.Channel()
if err != nil {
log.Println("channel: ", err)
return err
}
defer ch.Close()
q, err := declareRandomQueue(ch)
if err != nil {
log.Println("declareRandomQueue: ", err)
return err
}
for _, tpoic := range topics {
ch.QueueBind(
q.Name,
tpoic,
"log_topic",
false,
nil,
)
if err != nil {
return err
}
}
msg, err := ch.Consume(q.Name, "", true, false, false, false, nil)
if err != nil {
log.Println("message: ", err)
return err
}
forever := make(chan bool)
go func() {
for d := range msg {
var payload Payload
_ = json.Unmarshal(d.Body, &payload)
go handlePayload(payload)
}
}()
fmt.Printf("Waiting for message on [Exchange, Queue] [log_topic, %s\n]", q.Name)
<-forever
return nil
}
- event.go 中,ch.ExchangeDeclare 配置的是 “logs_topic”
func declareExchange(ch *amqp.Channel) error {
return ch.ExchangeDeclare(
"logs_topic", //name
"topic", //type
true, //durable
false, //auto delete
false, //internal
false, //no wait
nil, //arguements
)
}
三、优化代码
定义一个常量 channelName,代码中使用这个常量文章来源地址https://www.toymoban.com/news/detail-533868.html
const channelName string = "log_topic"
到了这里,关于Rabbitmq 消费者 : Exception (504) Reason: “channel/connection is not open“的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!