【RabbitMQ】topic type exchange example in golang
【RabbitMQ】核心概念講到,Exchange有三種型別:
- fanout
- direct
- topic
本篇為第三篇,介紹topic型別(topic型別的exchange,會將訊息傳送到指定routingkey的queue,這裡的routingkey跟direct型別的routingkey有所不同,可以指定類似:*.black.rabbit
, quick.*.rabbit
等)
在我的開發環境中,GOPATH=C:\Users${user_name}\go,在GOPATH\src 目錄下新建topic專案。在topic專案中新建兩個檔案:
- producer.go。負責傳送訊息
- consumer.go。負責接收訊息
在producer.go中寫入以下程式碼:
package main
import (
"log"
"os"
"github.com/streadway/amqp"
)
func exit_on_error(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
conn, err := amqp.Dial("amqp://<username>:<password>@<host>:<port>/")
exit_on_error(err)
defer conn.Close()
ch, err := conn.Channel()
exit_on_error(err)
defer ch.Close()
err = ch.ExchangeDeclare(
"example.topic", // name
"topic", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
exit_on_error(err)
routingkey := os.Args[1]
message := os.Args[2]
err = ch.Publish(
"example.topic", // exchange
routingkey, // routing key
false, // mandatory
false, // immediate
amqp.Publishing {
ContentType: "text/plain",
Body: []byte(message),
},
)
exit_on_error(err)
log.Printf(" [x] Sent %s", message)
}
producer流程:
- 建立exchange
- 傳送訊息到exchange(指定routingkey)
在consumer.go中寫入以下程式碼:
package main
import (
"log"
"os"
"github.com/streadway/amqp"
)
func exit_on_error(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
conn, err := amqp.Dial("amqp://<username>:<password>@<host>:<port>/")
exit_on_error(err)
defer conn.Close()
ch, err := conn.Channel()
exit_on_error(err)
defer ch.Close()
err = ch.ExchangeDeclare(
"example.topic", // name
"topic", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
exit_on_error(err)
q, err := ch.QueueDeclare(
"", // name
false, // durable
false, // delete when usused
true, // exclusive
false, // no-wait
nil, // arguments
)
exit_on_error(err)
routingkey := os.Args[1]
err = ch.QueueBind(
q.Name, // queue name
routingkey, // routing key
"example.topic", // exchange
false,
nil,
)
exit_on_error(err)
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
exit_on_error(err)
forever := make(chan bool)
go func() {
for d := range msgs {
log.Printf(" [x] %s", d.Body)
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}
說明:conn, err := amqp.Dial("amqp://<username>:<password>@<host>:<port>/")
使用<username>
等變數,需要替換成實際環境引數
consumer流程:
- 申明exchange
- 宣告queue
- 將queue繫結到exchange
- 訂閱queue
特別說明:如果將producer.go和consumer.go放到同一個目錄下,IDE會報錯,同一個main package中有兩個main function,可以忽略
執行producer:
go run producer.go <routingkey> <message>
執行consumer(為了測試效果,開啟兩個以上終端,執行以下命令):
go run consumer.go <routingkey>
執行結果示例:
相關文章
- 【RabbitMQ】direct type exchange example in golangMQGolang
- 【RabbitMQ】fanout type exchange example in golangMQGolang
- RabbitMQ - SpringBoot 案例 - topic 模式MQSpring Boot模式
- RabbitMQ的使用--以topic路由為例MQ路由
- RabbitMQ Go客戶端教程5——topicMQGo客戶端
- RabbitMQ 訊息佇列之 Exchange TypesMQ佇列
- RabbitMQ學習心得體會之ExchangeMQ
- RabbitMQ系列(三)RabbitMQ交換器Exchange介紹與實踐MQ
- Golang RabbitMQ DemoGolangMQ
- 上手了RabbitMQ?再來看看它的交換機(Exchange)吧MQ
- 快速掌握RabbitMQ(二)——四種Exchange介紹及程式碼演示MQ
- Golang 實現 RabbitMQ 的死信佇列GolangMQ佇列
- nsq topic
- golang使用sqlx報錯:unsupported type []interface {}, a slice of interfaceGolangSQL
- 4.5.1.3.2 Example
- Golang 實現 RabbitMQ 的延遲佇列GolangMQ佇列
- An example of polybase for OracleOracle
- simd example code
- RabbitMQ 入門(六)SpringAMQP五種訊息型別(釋出訂閱模式和Fanout Exchange )MQSpringGAM型別模式
- kafka檢視topicKafka
- [Typescript] Query builder exampleTypeScriptUI
- An example about git hookGitHook
- kafka2.x常用命令筆記(一)建立topic,檢視topic列表、分割槽、副本詳情,刪除topic,測試topic傳送與消費Kafka筆記
- Exchange Online Mailbox RestorationAIREST
- golang 封裝 rabbitmq,正常訊息,延時訊息,非炫技,僅記錄(golang新人)Golang封裝MQ
- Topic太多!RocketMQ炸了!MQ
- Exchange - Add Owner of Distribution Group
- Kafka原理剖析之「Topic建立」Kafka
- cannot convert (type interface {}) to type int: need type assertion
- package-tracking-app: Golang+RabbitMQ實時包裹跟蹤應用PackageAPPGolangMQ
- Erlang vs Elixir by example – kv store
- Tell Me About Yourself Example 1
- Data exchange of settype COMM_PRFREEATTR
- Exchange 2013 管理 視訊教程
- [轉]Angular 4|5 Material Dialog with ExampleAngular
- Java Scheduler ScheduledExecutorService ScheduledThreadPoolExecutor Example(ScheduledThreadPoolExecu...Javathread
- sim3d multiple component example3D
- For example, when you want to get the ball to the ground