【RabbitMQ】fanout type exchange example in golang
【RabbitMQ】核心概念講到,Exchange有三種型別:
- fanout
- direct
- topic
緊接著的三篇文章將介紹著三種型別,並給出golang的實現,本篇為第一篇,介紹fanout型別(fanout型別的exchange,會將訊息傳送到所有繫結了該exchange的queue上,可以理解成廣播模式)
在我的開發環境中,GOPATH=C:\Users${user_name}\go,在GOPATH\src 目錄下新建fanout專案。在fanout專案中新建兩個檔案:
- 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.fanout", // name
"fanout", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
exit_on_error(err)
message := os.Args[1]
err = ch.Publish(
"example.fanout", // exchange
"", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(message),
},
)
log.Printf(" [x] Sent %s", message)
}
producer流程:
- 建立exchange
- 傳送訊息到exchange(指定routingkey,但fanout型別的exchange不關心routingkey)
在consumer.go中寫入以下程式碼:
package main
import (
"log"
"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.fanout", // name
"fanout", // 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)
err = ch.QueueBind(
q.Name, // queue name
"", // routing key
"example.fanout", // 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 logs. 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 <message>
執行consumer(為了測試效果,開啟兩個以上終端,執行以下命令):
go run consumer.go
執行結果示例:
相關文章
- 【RabbitMQ】direct type exchange example in golangMQGolang
- 【RabbitMQ】topic type exchange example in golangMQGolang
- RabbitMQ 入門案例 - fanout 模式MQ模式
- RabbitMQ - SpringBoot 案例 - fanout 模式MQSpring Boot模式
- 【RabbitMQ】—— Exchange型別MQ型別
- Behind RabbitMQ Exchange TypesMQ
- Understanding RabbitMQ Exchange & QueueMQ
- 【RabbitMQ】三種型別交換器 Fanout,Direct,TopicMQ型別
- Golang RabbitMQ DemoGolangMQ
- RabbitMQ系列(三)RabbitMQ交換器Exchange介紹與實踐MQ
- RabbitMQ 訊息佇列之 Exchange TypesMQ佇列
- RabbitMQ學習心得體會之ExchangeMQ
- RabbitMQ Exchange & Queue Design Trade-offMQ
- Golang Cannot use ss(type AAA) as type AAA in map indexGolangIndex
- C#使用RabbitMq佇列(Sample,Work,Fanout,Direct等模式的簡單使用)C#MQ佇列模式
- SAP中Exchange rate type EURX到底幹嘛用的
- 上手了RabbitMQ?再來看看它的交換機(Exchange)吧MQ
- 快速掌握RabbitMQ(二)——四種Exchange介紹及程式碼演示MQ
- springboot使用RabbitMQ的fanout廣播模式消費者死活接收不到訊息Spring BootMQ模式
- Golang 實現 RabbitMQ 的死信佇列GolangMQ佇列
- SpringBoot+RabbitMQ通過fanout模式實現訊息接收(支援消費者多例項部署)Spring BootMQ模式
- Spring Boot+RabbitMQ 通過fanout模式實現訊息接收(支援消費者多例項部署)Spring BootMQ模式
- Golang 實現 RabbitMQ 的延遲佇列GolangMQ佇列
- golang使用sqlx報錯:unsupported type []interface {}, a slice of interfaceGolangSQL
- Spark exampleSpark
- oracle exampleOracle
- An example of polybase for OracleOracle
- angular 2 by exampleAngular
- Oracle By Example (OBE)Oracle
- simd example code
- golang 封裝 rabbitmq,正常訊息,延時訊息,非炫技,僅記錄(golang新人)Golang封裝MQ
- exchange partition
- An example about git hookGitHook
- react router animation exampleReact
- An Example of How Oracle WorksOracle
- [Typescript] Query builder exampleTypeScriptUI
- exchange partition(轉)
- A example that using JQuery clonejQuery