springboot operation of RabbitMQ

The previous article explained the working mode of rabbitmq and
the detailed explanation of the working mode of rabbit single machine deployment rabbit connection rabbitMQ under Linux

Create a new springboot project in advance, if you don’t know it, go to my blog

Environment build

// maven 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
//application
spring.application.name=rabbitmq-demo
server.port= 9099

spring.rabbitmq.host=主机
spring.rabbitmq.port=5672
spring.rabbitmq.username=test-user
spring.rabbitmq.password=123
spring.rabbitmq.virtual-host=/test-v //虚拟主机

code manipulation

Direct working mode

// 生产者
@RestController
public class RabbitController {
    
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("createMQ")
    public void createMQ(){
    
    
        //  队列名称 没有则创建               发送消息信息
        rabbitTemplate.convertAndSend("test-q","hello");
    }
}

//消费者
@Component  
//                         参数               队列名称   是否持久化  是否自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "test-q",durable = "true",autoDelete = "true"))
//             此处有默认值 可以只写队列名称,其它参数默认值为持久化 非独占 非自动删除
public class CustomerMQ {
    
    

    @RabbitHandler
    public void receive(String msg){
    
    
        System.out.println("msg is "+msg);
    }
}



work model

default polling

In this model, the message queue is only responsible for sending messages to all listening consumers in polls, and has nothing to do with the internal logic of consumers

    @GetMapping("workMQ")
    public void workMQ(){
    
    
    // 循环10
        for (int i = 0; i < 10; i++) {
    
    
            //  队列名称  没有则创建(随便起)           发送消息内容
            rabbitTemplate.convertAndSend("work","hello");
        }
    }

//work模型消费者监听类
@Component
public class WorkCustomer {
    
    
// 注意 此处 rabbitListener是作用在方法上了 这样编程更加容易
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive(String msg) throws InterruptedException {
    
    
    //此处模拟消费者1执行的慢 消费者2 执行的快 
    //很显然执行结果与消费者执行快慢没有联系 是每消费者执行5条
        Thread.sleep(2000);
        System.out.println("receive1 msg is "+msg);
    }


    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String msg){
    
    
        System.out.println("receive2 msg is "+msg);
    }
}

Distribution according to work

As the name implies, the message queue allocation message under this strategy is directly related to the logic of the consumer. The consumer needs to consume the message and feed back the message queue before the message queue continues to send messages to the consumer.

// 代码与上面一致 
application.properties 添加一行 限制接收消息

spring.rabbitmq.listener.simple.prefetch=1
//即可
 



Broadcast model (fanout)

This scenario is a classic broadcast mode, and the consumer's message will be sent to all listening consumers


//生产者 接口
    @GetMapping("fanoutMQ")
    public void fanoutMQ(){
    
    
        for (int i = 0; i < 10; i++) {
    
    
    // 第一个参数交换机名称 与消费者对应即可 
   //第二个参数 队列名称 此处使用临时队列 不需要值 第三参数 发送消息内容
            rabbitTemplate.convertAndSend("test-fanout","","hello");
        }
    }

//消费者类

@Component
public class FanoutCustomer {
    
    

    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                                        //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-fanout",type = "fanout")
            )
    })
    public void receive(String msg) throws InterruptedException {
    
    
        Thread.sleep(2000);
        System.out.println("receive1 msg is "+msg);
    }


    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-fanout",type = "fanout")
            )
    })
    public void receive2(String msg){
    
    
        System.out.println("receive2 msg is "+msg);
    }

}


routing model

This model uses a fixed routing key to selectively send messages to all listening consumers

// 生产者

    @GetMapping("directMQ")
    public void directMQ(){
    
    
        // 第一个参数交换机名称 与消费者对应即可 第二个参数 路由key 该参数决定发送至哪个消费者
         //                                     第三参数 发送消息内容
        rabbitTemplate.convertAndSend("test-direct","key1","hello");

    }

//消费者
// 说明: 此处定义四个监听消费者  一监听的key:key1 key2 key 3 二: key1 三: key2
//四 :key3 
/**
此处消费者发送是的key1 很明显消息只可能被 消费者一和消费者二接收到

*/
@Component
public class DirectCustomer {
    
    

    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-direct",type = "direct") ,
                                                        // 该参数默认类型就是direct
                    key = {
    
    "key1","key2","key3"}
            )
    })
    public void receive(String msg) throws InterruptedException {
    
    
        System.out.println("receive1 msg is "+msg);
    }


    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-direct",type = "direct") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key1"}
            )
    })
    public void receive2(String msg){
    
    
        System.out.println("receive2 msg is "+msg);
    }
    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-direct",type = "direct") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key2"}                  )
    })
    public void receive3(String msg) throws InterruptedException {
    
    
        Thread.sleep(2000);
        System.out.println("receive1 msg is "+msg);
    }


    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    //   value 交换机名称 与生产者对应  type 交换机类型 fanout
                    exchange = @Exchange(value= "test-direct",type = "direct") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key3"}
            )
    })
    public void receive4(String msg){
    
    
        System.out.println("receive2 msg is "+msg);
    }
}


topic model

The topic model is consistent with the routing, that is, changing the fixed routing key into a routing wildcard. In fact, the principle is the same. The routing wildcard * and # * provided by mq
: match an arbitrary word
#: match multiple arbitrary words
Example: sent by the producer Routing key: "test.topic"
Routing key set by consumer 1: "test.* ", routing key set by consumer 2: "test.# "
Then both consumers can receive the message, but when The producer changes the routing key to test.topic.one.
At this time, only consumer 2 can receive the message because the #wildcard can receive any number of words


//生产者
    @GetMapping("topicMQ")
    public void topicMQ(){
    
    
        rabbitTemplate.convertAndSend("test-topic","key.topic","hello");
    }
    
//消费者
@Component
public class TopicCustomer {
    
    

    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    exchange = @Exchange(value= "test-topic",type = "topic") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key.#","key.*"}
            )
    })
    public void receive(String msg) throws InterruptedException {
    
    
        System.out.println("receive msg is "+msg);
    }
    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    exchange = @Exchange(value= "test-topic",type = "topic") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key.#"}
            )
    })
    public void receive1(String msg) throws InterruptedException {
    
    
        System.out.println("receive1 msg is "+msg);
    }
    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,// 代表使用的临时队列
                    exchange = @Exchange(value= "test-topic",type = "topic") ,
                    // 该参数默认类型就是direct
                    key = {
    
    "key.*"}
            )
    })
    public void receive2(String msg) throws InterruptedException {
    
    
        System.out.println("receive2 msg is "+msg);
    }


Guess you like

Origin blog.csdn.net/pgcdnameming/article/details/126666982