大佬教程收集整理的这篇文章主要介绍了如何在 Golang 中使用 RabbitMQ 实现 Exchange Header?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以目前我正在使用 RabbitMQ 和 Golang。我在 RabbitMQ 主页上开始了教程并创建了队列和交换。但是我找不到任何解释如何在 Golang 中实现 Exchange header 的教程。@H_874_3@
重要的是,我也想知道,如何将数据放在 header 中,例如 ID 和名称?@H_874_3@
我假设您的意思是队列或交换的名称。您可以遍历一个列表,或者如果 name 永远不会更改,则只需静态键入它。例如,我创建了 x 个基于 CPU 的队列,以将数据并行发送到每个核心,同时串行(每个队列在自己的线程上运行)。可能不是最好的方法,这是我的方法,并添加了一个明显没有内置的第三层,将循环串联到队列中!@H_874_3@
package main
import (
"log"
"github.com/streadway/amqp"
)
var i int = 0
var coreCount []String = make([]String,runtime.NumCPU())
func main() {
//Make a connection
conn,_ := amqp.Dial("amqp://guest:guest@localhost:5672/")
defer conn.Close()
//Ccreate a chAnnel
ch,_ := conn.ChAnnel()
defer ch.Close()
//Declare a queue
q,err := ch.QueueDeclare(
"Hello",// Your Queue name is "Hello" bound to the Default Exchange <------ q.Name is Defined here
false,// should the message be persistent? also queue will survive if the cluster gets reset
false,// autodelete if there's no consumers (like queues that have anonymous names,often used with fanout exchangE)
false,// exclusive means I should get an error if any other consumer subsribes to this queue
false,// no-wait means I don't want RabbitMQ to wait if there's a queue successfully setup
nil,// arguments for more advanced configuration
)
//Publish a message
body := "Hello world"
err = ch.Publish(
"",// exchange <--- change the header here
q.Name,// routIng key
false,// mandatory
false,// immediate
amqp.publishing{
ContentType: "text/plain",Body: []byte(body),})
log.Printf("message: %s",body)
}
q.Name 是您要绑定到的队列名称和通道。 "" : // Exchange 是您的交换名称.. 保留双引号时,它的名称是默认值,并将具有相同路由键的所有队列绑定到该交换。@H_874_3@
查看 delcarequeue(arg),这是您更改队列名称的地方。 在publish(arg) 中,您可以在此处更改Exchange 标头。您可以创建一个包含多个交换的列表并遍历该列表。@H_874_3@
编辑您需要在发布商方面阅读您的消息。@H_874_3@
我设置了 5 个颜色队列。 如果一条消息是红色的,它就进入红色队列,蓝色进入蓝色队列..@H_874_3@
因此您的发布者需要读取每条消息,将字节类型转换为字符串,然后根据您的要求解析消息。假设您正在寻找红色消息。@H_874_3@
您的发布者发现了一条红色消息,现在需要将其定向到红色队列。这是您根据所有队列名称的数组列表以及消息中的颜色与队列名称匹配的地方更新 declarequeue() 中的 QUEUE 名称,现在您在消息进入时更新标头消息。> @H_874_3@
https://www.rabbitmq.com/tutorials/amqp-concepts.html
其中图片是发布者,读取每条消息并定向到每个队列。@H_874_3@
以上是大佬教程为你收集整理的如何在 Golang 中使用 RabbitMQ 实现 Exchange Header?全部内容,希望文章能够帮你解决如何在 Golang 中使用 RabbitMQ 实现 Exchange Header?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。