RabbitMQ combat (2) Spring Boot integrates RabbitMQ

This article focuses on basic integration. Run the code first, then talk about advanced features.

Some terms in RabbitMQ

If you open the RabbitMQ web console, you will find that there is one Exhanges that is difficult to understand. The following is a brief description.

Exchange

An exchange is like a router. We first send the message to the exchange, and then the exchange will deliver the message to the corresponding queue according to the routing key (routingKey). (It is very important to understand this concept, which is fully reflected in the following code)

Queue

Queues are easy to understand and don't need to be explained.

Binding

How does the exchange know which queue to deliver the message to? This requires binding. Probably: use a routing key (routingKey) to bind a queue (Queue) to an exchange (Exchange), so that the exchange knows which queue to deliver the message to according to the routing key. (This is fully reflected in the code behind)

Add RabbitMQ maven dependency

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

Add another dependency (this dependency can be omitted, mainly to simplify the code)

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.0.2</version>
</dependency>

RabbitMQConfig.java configuration

@Configuration
public class RabbitMQConfig {

    public final static String QUEUE_NAME = "spring-boot-queue";
    public final static String EXCHANGE_NAME = "spring-boot-exchange";
    public final static String ROUTING_KEY = "spring-boot-key";

    // 创建队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME);
    }

    // 创建一个 topic 类型的交换器
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    // 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

}

producer

Just call the convertAndSend method of rabbitTemplate directly. It can also be seen from the following code that we do not send the message directly to the queue, but first send it to the exchange, and the exchange then delivers our message to the corresponding queue according to the routing key.

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMessage")
    public Object sendMessage() {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                String value = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
                Console.log("send message {}", value);
                rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, value);
            }
        }).start();
        return "ok";
    }

}

consumer

The consumer is also very simple, just add the @RabbitListener annotation to the corresponding method and specify the name of the queue to be monitored.

@Component
public class Consumer {

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void consumeMessage(String message) {
        Console.log("consume message {}", message);
    }
}

run the project

Run the project, then open a browser and enter http://localhost:9999/sendMessage. On the console, you can see that the producer is constantly sending messages, and the consumer is constantly consuming messages.

Open the RabbitMQ web console, and you can also see the switches and queues we just configured in the code, as well as the binding information.

Click to enter the details of the switch

Source address

Epilogue

Due to my limited knowledge and ability, if there is something unclear in the text, I hope you can point it out in the comment area to help me write the blog post better.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324541872&siteId=291194637