Go-micro Rabbit mq插件-优先发布消息

问题描述 投票:1回答:1

由于支持RabbitMQ版本3.5.0优先级队列-https://www.rabbitmq.com/priority.html

如果在队列创建过程中传递了x-max-priority参数,则可以声明该队列。

我可以成功声明具有优先级支持的队列

brkrSub := broker.NewSubscribeOptions(
        broker.DisableAutoAck(),
        rabbitmq.QueueArguments(map[string]interface{}{"x-max-priority": 10}),
    )

    service.Server().Subscribe(
        service.Server().NewSubscriber(
            "mytopic",
            h.Handle,
            server.SubscriberQueue("mytopic.hello"),
            server.SubscriberContext(brkrSub.Context),
        ),
    )

但是我如何发布指定优先级的消息?

    body := &message.MyTestMessage{
        Message: fmt.Sprintf("Message number %d", counter),
    }

    msg := client.NewMessage(
        topic,
        body,
        // TODO: Priority
    )
    if err := client.Publish(ctx, msg); err != nil {
        fmt.Printf("Cannot publish message: ", err.Error())
        return
    }

我找不到将Priority作为MessageOption或PublishOption传递的直接方法,但是,似乎有一种方法可以在client.Publish上下文中指定其他选项。我在寻找正确的方向吗?如果可以,您可以在这里帮我些忙吗?

Edit:我能够执行以下操作而不会引起任何编译时错误。优先级仍然被忽略,但是消息以通常的方式出现


func setPriority(ctx context.Context, priority int) client.PublishOption {
    return func(o *client.PublishOptions) {
        o.Context = context.WithValue(ctx, "priority", priority)
    }
}

func publish(ctx context.Context, priority int, counter int) {
    //body := fmt.Sprintf("hello, I am a message %d", counter)
    body := &message.MyTestMessage{
        Message: fmt.Sprintf("Message number %d", counter),
    }

    msg := client.NewMessage(
        topic,
        body,
    )
    if err := client.Publish(ctx, msg, setPriority(ctx, priority)); err != nil {
        fmt.Printf("Cannot publish message: ", err.Error())
        return
    }

    fmt.Printf("Published message %d to %s \n", counter, topic)
}
go rabbitmq go-micro
1个回答
0
投票

尝试类似的事情:

func publishMessageToChan(queue *amqp.Queue, channel *amqp.Channel, messageToQueue string) error {
    return channel.Publish(
        "<exchange>", // exchange
        "<queue>",    // routing key
        false,        // mandatory
        false,        // immediate
        amqp.Publishing{
            Timestamp:   time.Now(),
            ContentType: "text/plain",
            Body:        []byte(messageToQueue),
            Priority:    0, // <-- Priority here < 0 to 9>
        })
}

带库“ github.com/streadway/amqp”

© www.soinside.com 2019 - 2024. All rights reserved.