springBoot integrates rabbitMQ topic mode

Hello everyone. Recently, when I was learning rabbitMQ, I found that the topic mode was quite interesting. Post it and share the learning process with you. I hope to learn, communicate and grow together.
First, write pom.xml to maven of mq.

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

Then there is the configuration file

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

Next is the more important configuration class of 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.#");
    }
}

After the configuration is complete, write a listener class that is a consumer

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);
    }
}

Next, I am writing a producer that is a message sender

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

Send in this way, both queues can be received,
Both queues received the message
and then we are changing the producer to see the effect

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

}


In this way, the message queue of topic.qune receives two messages

The topic.okong just received a message
such a reason is that routingKey switch configuration is not the same
binding topic.qune message queue configuration routingKey is the topic. # Mean the beginning of a whole topic will be matched to the message queue,
The routingKey configured for the bound topic.okong message queue is topic. It will only match the word after topic. If there are two or more, it will not match the message queue.
Of course, we can also directly set the
routingKey * Set it as topic.asd and so on, but in this case, it is basically no different from ordinary Direct.
I hope I can help you.

Guess you like

Origin blog.csdn.net/ChenLong_0317/article/details/90720421