SpringBoot integrates RabbitMQ message producer

Configure the message expiration time (TTL): https://www.jianshu.com/p/341c63cf0459 The
expiration time can be set for the entire queue or a single piece of data, and the setting is subject to the shortest time.

Dead letter queue (DLX):
Send the expired message or the message that the queue is full of to the dead letter switch, and then send it to the specified queue through the dead letter switch. To achieve the purpose of collecting expired messages.

Delay queue: The
expiration time is matched with the private message queue to realize the delay queue. Set a certain expiration time. After the message reaches the expiration time, it will be sent to the designated queue by the dead letter exchange, and finally consumers can listen to the designated queue.

pom:

<!--RabbitMQ依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

fr:

spring:
  rabbitmq:
    #RabbitMQ部署服务器ip
    host: 127.0.0.1
    #RabbitMQ端口
    port: 5672
    #用户名
    username: june
    #密码
    password: 123456
    #虚拟主机
    virtual-host: /VirtualTest

Main start:

@SpringBootApplication
public class ProducerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ProducerApplication.class, args);
    }

}

RabbitMQ working mode:
1. Work queue mode-use the default switch: the producer sends the message directly to the queue, and the consumer gets it from the queue. Setting multiple consumers can realize the function of multiple consumers grabbing news.
Insert picture description here
2. Routing mode: send messages one-to-one, or distribute messages one-to-many. The producer distributes messages to different message queues through different routing keys of the exchange.
Insert picture description here
3. Wildcard mode: an upgraded version of the routing mode. Using the exchange switch is an upgraded version of the routing mode. Wildcards are used when binding the key to the queue. This can be achieved through wildcards, and messages are sent to multiple specified queues.
Insert picture description here
Insert picture description here
Subscription mode: similar to group messaging. Use the exchange switch to uniformly distribute information to all queues bound to the exchange.
Insert picture description here

Configuration class:

package com.student.config;


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

/**
* 作者:June
* @author June
*/
@Configuration
public class RabbitMQConfig {
    
    
    //交换机名称
    public static final String EXCHANGE = "exchangeBoot";
    //队列名称
    private static final String QUEUE1 = "queueBoot1";
    //队列名称
    private static final String QUEUE2 = "queueBoot2";

    //注册交换机
    @Bean
    public Exchange exchange() {
    
    
        /* 交换机模式:
         *           fanout:订阅模式
         *           topic:通配符模式
         *           direct:路由模式
         *           headers:
        * */
        return ExchangeBuilder.topicExchange(EXCHANGE).durable(true).build();
    }
    //注册队列
    @Bean
    public Queue queue1() {
    
    
        return QueueBuilder.durable(QUEUE1).build();
    }
    //注册队列
    @Bean
    public Queue queue2() {
    
    
        return QueueBuilder.durable(QUEUE2).build();
    }
    //绑定队列和交换机
    @Bean
    public Binding itemQueueExchange1(@Qualifier("queue1")Queue queue1, @Qualifier("exchange")Exchange exchange) {
    
    
        //参数依次是队列对象、交换机对象、路由key
        return BindingBuilder.bind(queue1).to(exchange).with("queue.*").noargs();
    }
    //绑定队列和交换机
    @Bean
    public Binding itemQueueExchange2(@Qualifier("queue2")Queue queue2, @Qualifier("exchange")Exchange exchange) {
    
    
        //参数依次是队列对象、交换机对象、路由key
        return BindingBuilder.bind(queue2).to(exchange).with("queue.*").noargs();
    }
}

controller:

package com.student.controller;

import com.student.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 作者:June
*/
@RestController
public class ProducerController {
    
    
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/producer")
    public String produce(String message, String key) {
    
    
        //发送消息
        /*
        * 参数1:交换机名称
        * 参数2:路由key
        * 参数3:发送的消息
        * */
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE,key,message);
        return "发送消息成功!!!";
    }
}

Guess you like

Origin blog.csdn.net/weixin_52877849/article/details/115022088