RabbitMq話題模式(topic)

話題模式

話題模式主要使用routingKey來進行路由匹配,匹配規則如下,

要注意key規則
其中"*"用於匹配一個單詞, "#"用於匹配多個單詞(可以是零)
匹配imooc.* 表示可以匹配 imooc.hello ,但是imooc.hello.one需要使用imooc.#纔可以匹配到

話題模式創建RabbitMq實例

func NewRabbitMqTopic(exchangeName, routingKey string)*RabbitMq{
	return NewRabbitMq("", exchangeName, routingKey)
}

生產者

1、嘗試創建交換機,topic模式,

2、發送消息傳入交換機和key

func (r *RabbitMq)PublishTopic(message string){
	//1. 嘗試創建交換機
	err := r.channel.ExchangeDeclare(
		r.Exchange,
		"topic",
		//是否持久化
		false,
		//
		false,
		false,
		false,
		nil)
	r.failOnErr(err, "failed to declare an exchange")

	//2. 發送消息
	r.channel.Publish(
		//交換機
		r.Exchange, //簡單模式爲空
		//key
		r.Key,
		false,
		false,
		//發送的信息
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(message),
		})
}

消費者

1、嘗試創建交換機, 模式爲topic,傳入exchange

2、申請隊列,隊列名稱爲空

3、綁定隊列傳入隊列名稱,key,交換機

4、channel.Consume消費消息

func(r *RabbitMq)RecivedTopic(){
	//嘗試創建交換機
	err := r.channel.ExchangeDeclare(
		r.Exchange,
		"topic",
		//是否持久化
		false,
		//
		false,
		false,
		false,
		nil)
	r.failOnErr(err, "failed to declare an exchange")


	//2 嘗試創建隊列
	q, err := r.channel.QueueDeclare(
		"",
		false,
		false,
		true,
		false,
		nil)

	r.failOnErr(err, "failed to declare an queue")

	//3 綁定隊列到exchange中
	err = r.channel.QueueBind(
		q.Name,
		r.Key,
		r.Exchange,
		false,
		nil)

	//消費消息
	msgs, err := r.channel.Consume(
		q.Name,
		"",
		true,
		false,
		false,
		false,
		nil)

	if err != nil {
		fmt.Printf("channel.Consume failed, error:%+v\n", err)
		return
	}

	forever := make(chan bool)
	go func() {
		for d := range msgs {
			//實現我們要處理的邏輯函數
			log.Printf("Received a message:%s", d.Body)
		}
	}()
	log.Printf("[*] Waiting for message, exit to  press CTRL + C")
	<-forever
}

使用案例

生產者

package main

import (
	"rabbitmq/RabbitMq"
	"strconv"
	"time"
)

func main() {

	imoocOne := RabbitMq.NewRabbitMqTopic("exImoocTopic", "imooc.topic.one")
	imoocTwo := RabbitMq.NewRabbitMqRouting("exImoocTopic", "imooc.topic.two")
	for i := 0; i < 10; i++ {
		imoocOne.PublishTopic("imooc topic one " + strconv.Itoa(i))
		imoocTwo.PublishTopic("imooc topic two " + strconv.Itoa(i))
		time.Sleep(10 * time.Millisecond)
	}
}

消費者

package main

import "rabbitmq/RabbitMq"

func main(){

	rabbitmq := RabbitMq.NewRabbitMqRouting("exImoocTopic", "#")
	rabbitmq.RecivedTopic()
}
=========================================================
package main

import (
	"rabbitmq/RabbitMq"
)

func main(){
	rabbitmq := RabbitMq.NewRabbitMqRouting("exImoocTopic", "imooc.*.two")
	rabbitmq.RecivedTopic()
}

分析

1、生產者者創建兩個rabbitmq實例交換機名稱爲exImoocTopic,routingKey爲imooc.topic.one 和 imooc.topic.two

2、消費者1的routingKey爲#, 消費者2的routingKey爲imooc.*.two,

3、消費者1可以獲取到所有的路由的消息,消費者2可以獲取到格式爲imooc.topic.two的消息,實際消費可以消費imooc.[任意].two的消息,只是在本案例中只能消費imooc.topic.two的消息

輸出結果

消費者1輸出結果

2019/07/04 12:05:57 [*] Waiting for message, exit to  press CTRL + C
2019/07/04 12:06:04 Received a message:imooc topic one 0
2019/07/04 12:06:04 Received a message:imooc topic two 0
2019/07/04 12:06:04 Received a message:imooc topic one 1
2019/07/04 12:06:04 Received a message:imooc topic two 1
2019/07/04 12:06:04 Received a message:imooc topic one 2
2019/07/04 12:06:04 Received a message:imooc topic two 2
2019/07/04 12:06:04 Received a message:imooc topic one 3
2019/07/04 12:06:04 Received a message:imooc topic two 3
2019/07/04 12:06:04 Received a message:imooc topic one 4
2019/07/04 12:06:04 Received a message:imooc topic two 4
2019/07/04 12:06:04 Received a message:imooc topic one 5
2019/07/04 12:06:04 Received a message:imooc topic two 5
2019/07/04 12:06:04 Received a message:imooc topic one 6
2019/07/04 12:06:04 Received a message:imooc topic two 6
2019/07/04 12:06:04 Received a message:imooc topic one 7
2019/07/04 12:06:04 Received a message:imooc topic two 7
2019/07/04 12:06:04 Received a message:imooc topic one 8
2019/07/04 12:06:04 Received a message:imooc topic two 8
2019/07/04 12:06:04 Received a message:imooc topic one 9
2019/07/04 12:06:04 Received a message:imooc topic two 9

消費者2輸出結果

2019/07/04 12:06:01 [*] Waiting for message, exit to  press CTRL + C
2019/07/04 12:06:04 Received a message:imooc topic two 0
2019/07/04 12:06:04 Received a message:imooc topic two 1
2019/07/04 12:06:04 Received a message:imooc topic two 2
2019/07/04 12:06:04 Received a message:imooc topic two 3
2019/07/04 12:06:04 Received a message:imooc topic two 4
2019/07/04 12:06:04 Received a message:imooc topic two 5
2019/07/04 12:06:04 Received a message:imooc topic two 6
2019/07/04 12:06:04 Received a message:imooc topic two 7
2019/07/04 12:06:04 Received a message:imooc topic two 8
2019/07/04 12:06:04 Received a message:imooc topic two 9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章