gpt4 book ai didi

go - Go-Micro Rabbit MQ插件-优先发布消息

转载 作者:行者123 更新时间:2023-12-01 21:14:05 27 4
gpt4 key购买 nike

由于支持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上下文中指定其他选项。我在寻找正确的方向吗?如果可以,您能在这里帮我些忙吗?

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

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)
}

最佳答案

尝试这样的事情:

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”

关于go - Go-Micro Rabbit MQ插件-优先发布消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60652897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com