doudang4857 2016-07-04 13:46
浏览 65

Golang Rabbit MQ扇出交换多个使用者

I am publishing messages in fanout exchange from Java application. I am able to receive message in multiple consumer in Java. I have 2 consumers in golang app but only one of the consumer (alternatively ) is receiving the message (Not both of them for a published message).

func HandleMessageFanout1(){

    conn := system.EltropyAppContext.RabbitMQConn

    channel, err := conn.Channel()

    if(err!=nil){
        log.Println(err)
    }
    //forever := make(chan bool)



    deliveries,err := channel.Consume(
        "example.queue", //queue
        "qw",
        true,
        false,
        false,
        false,
        nil)

    if(err!=nil){
        log.Println(err)
    }

    go func() {

        for d := range deliveries {
            log.Printf("Message recived in fanout 1")
            log.Printf("Received a message: %s", d.Body)
        }
    }()

    //<-forever

}

//2nd Consumer

package consumer

import (
    "github.com/eltropy/shehnai/backend/golang/common-packages/system"
    log "github.com/Sirupsen/logrus"
)

    func HandleMessageFanout2() {

        conn := system.EltropyAppContext.RabbitMQConn

        channel, err := conn.Channel()

        if (err!=nil) {
            log.Println(err)
        }

        //forever := make(chan bool)

        deliveries, err := channel.Consume(
            "example.queue", //queue
            "q2",
            true,
            false,
            false,
            false,
            nil)

        if (err!=nil) {
            log.Println(err)
        }

        go func() {
            for d := range deliveries {
                log.Printf("Message recived in fanout 2")
                log.Printf("Received a message: %s", d.Body)
            }
        }()

        //<-forever

    }

I am using https://github.com/streadway/amqp library for rabbit mq.

  • 写回答

1条回答 默认 最新

  • douwo5710 2016-11-04 11:54
    关注

    On the channel type, before you publish, declare an exchange like this:

    err = channel.ExchangeDeclare(
      "example.queue",   // name
      "fanout", // type
      true,     // durable
      false,    // auto-deleted
      false,    // internal
      false,    // no-wait
      nil,      // arguments
    )
    

    See the official RabbitMQ tutorial.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题