RabbitMQ入门学习系列(六) Exchange的Topic类型

快速阅读

介绍exchange的topic类型,和Direct类型相似,但是增加了"."和"#"的匹配。比Direct类型灵活

Topic消息类型

特点是:topic消息类型不能是任意的routing key, 必须是有点"."组成的单词列表。

和dirct类似,最后也是也相应的key进行匹配

例如:speed.color.sepcies

注意可以用*号和#号出现,和我们日常用的正则表达式含义相近

"*"号代表任意一个单词

"#"号代表0个或多个单词

生产者代码

  1. 创建连接和信道
  2. 声明exchange类型
  3. 设置两个routingkey 各自发送两次。一共发送四条消息
static void Main(string[] args)
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
    {
        channel.ExchangeDeclare(exchange: "topicType", type: "topic");

        var list = new List<string>();
        list.Add("www.baidu.com");
        list.Add("www.google.com");
        for (var i = 0; i < 2; i++)
        {
            var routingKey = list[0];
            string message = "Hello World!this  message routingkey is  " + routingKey;
            var body = Encoding.UTF8.GetBytes(message);
            var properties = channel.CreateBasicProperties();
            properties.Persistent = true;

            channel.BasicPublish(exchange: "topicType",
                                 routingKey: routingKey,
                                 basicProperties: null,
                                 body: body);

            Console.WriteLine(" [x] Sent {0},id={1}", message,i);
            Thread.Sleep(1000);
        }

        for (var i = 0; i < 2; i++)
        {
            var routingKey = list[1];
            string message = "Hello World!this  message routingkey is  " + routingKey;
            var body = Encoding.UTF8.GetBytes(message);
            var properties = channel.CreateBasicProperties();
            properties.Persistent = true;

            channel.BasicPublish(exchange: "topicType",
                                 routingKey: routingKey,
                                 basicProperties: null,
                                 body: body);

            Console.WriteLine(" [x] Sent {0},id={1}", message, i);
            Thread.Sleep(1000);
        }

    }

    Console.WriteLine(" Press [enter] to exit.");
    Console.ReadLine();
} 

消费者代码

  1. 创建连接和信道
  2. 为了测试 topic 类型,限制输入中含 有baidu,google两个关键词和发送的routing key相匹配。
  3. 声明交换器类型
  4. 队列声明
  5. 把exchange和队列绑定到信道上
  6. 接受消息
static void Main(string[] args)
{
    bool flag = true;
    string level = "";
    while (flag)
    {
        Console.WriteLine("请选择要查看的消息类型");
        level = Console.ReadLine();
        if (level.IndexOf(".baidu.")>0 || level.IndexOf(".google.") > 0 )
            flag = false;
        else
            Console.Write("不支持你输入的消息");
    }

    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange: "topicType", type: "topic");
            var queueName = channel.QueueDeclare().QueueName;
            channel.QueueBind(queue: queueName, exchange: "topicType", routingKey: level);
            //以下是区别生产者的
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (sender, e) =>
            {
                var body = e.Body;
                var message = Encoding.UTF8.GetString(body);
                var rk = e.RoutingKey;
                Console.WriteLine("Received {0},routingKey:{1}", message, rk);
                Thread.Sleep(3000);//模拟耗时任务 ,
                Console.WriteLine("Received over");
                channel.BasicAck(deliveryTag: e.DeliveryTag, multiple: false);
            };
            channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
            Console.WriteLine("");
            Console.ReadLine();
        }

    }

测试结果

我们启动消费者以后,输入四个routingkey去测试

分别是

.google.#, #.google. , *.google.com, *.baidu.com

分别监听中间是google 或者baidu的消息

结果符合要求

posted @ 2019-07-07 23:40  水木  阅读(576)  评论(0编辑  收藏  举报