springBoot集成rabbitMQ topic模式

大家好 最近在学习rabbitMQ 的时候发现topic模式挺有意思的,贴出来学习过程和大家分享一下,希望一起学习交流和成长
首先pom.xml写入mq的maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

然后就是配置文件

server.port=8082
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672 #注意这里是5672 并不是15672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

接下来就是比较重要的 mq的配置类了

package com.rabbitmq.util;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



/**
 * @author 陈龙龙
 * @title: rabbitMqUtil
 * @projectName rabbit_demo
 * @description: TODO
 * @date 2019/5/2215:48
 */
@Configuration
public class rabbitMqUtil {
    @Bean(name = "okong")
    public Queue queue() {
        //创建一个消息队列
        return new Queue("topic.okong");
    }
    @Bean(name = "qune")
    public Queue qune() {
        //创建一个消息队列
        return new Queue("topic.qune");
    }
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topic");//配置路由器为Topic模式
    }

    @Bean
    Binding bindingExchangeA(@Qualifier("okong") Queue queue, TopicExchange topicExchange) {
        // 配置该消息队列的  routingKey 
        //topic.* 匹配 第一个.后面的单词    代表      一个    单词
        //比如 topic.asd 会被该消息队列接受 topic.asd.dsf不会被该消息队列接受
        return BindingBuilder.bind(queue).to(topicExchange).with("topic.*");
    }

    @Bean
    Binding bindingExchangeB(@Qualifier("qune") Queue qune, TopicExchange topicExchange) {
        // 配置该消息队列的  routingKey 
        //topic.# 匹配 所有.后面的单词     代表     任意    个      单词
        //比如 topic.asd 会被该消息队列接受 topic.asd.dsf也会被该消息队列接受
          return BindingBuilder.bind(qune).to(topicExchange).with("topic.#");
    }
}

配置完以后在写一个监听类 即消费者

package com.rabbitmq.util;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 陈龙龙
 * @title: ListenterQune
 * @projectName rabbit_demo
 * @description: TODO
 * @date 2019/5/2216:36
 */

@Component
public class ListenterQune {
    @RabbitListener(queues = "topic.qune")
    @RabbitHandler
    public void process(String mess){
        System.out.println("我是topic.qune的消费者接收到的消息为 :"+mess);
    }
    @RabbitListener(queues = "topic.okong")
    @RabbitHandler
    public void okong(String mess){
        System.out.println("我是topic.okong的消费者接收到的消息为:"+mess);
    }
}

接下来在写一个生产者 即消息发送者

@RequestMapping("sendTwo")
public void sendTwo(String mes){
    amqpTemplate.convertAndSend("topic","topic.name",mes);
    amqpTemplate.convertAndSend("topic","topic.a",mes);
    
}

这个样发送两个队列都可以收到
两个队列都收到了消息
然后我们在改一下生产者看一下效果

@RequestMapping("sendTwo")
public void sendTwo(String mes){
    amqpTemplate.convertAndSend("topic","topic.name.asda",mes);
    amqpTemplate.convertAndSend("topic","topic.a",mes);

}


这样子topic.qune的消息队列收到两个消息

而topic.okong只收到了一条消息
出现这样的原因就在于配置交换机的 routingKey 不一样
绑定topic.qune消息队列配置的routingKeytopic.# 意思是说开头是topic的全部都会匹配到该消息队列,
而绑定topic.okong消息队列配置的routingKey 是topic.,他只会匹配topic.后面有一个单词的,如果有两个或者多个就会匹配不上该消息队列
当然,我们也可以直接将
routingKey* 设为topic.asd等什么的,但是这样的话就和普通的Direct基本没什么区别了,
希望可以帮到大家。

猜你喜欢

转载自blog.csdn.net/ChenLong_0317/article/details/90720421