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

发布了166 篇原创文章 · 获赞 26 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/94604025