Writing in the front

Short for Message Queue, MQ (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages in and out of queues (data for the application) without the need for dedicated connections to link them. Messaging refers to programs communicating with each other by sending data in messages rather than by direct calls, which are typically used for techniques such as remote procedure calls. Queuing refers to applications communicating through queues. The use of queues removes the need for receiving and sending applications to execute simultaneously. RabbitMQ is an open source message broker that implements the advanced message queuing protocol. RabbitMQ servers are written in the Erlang language, while clustering and failover are built on top of the open telecom platform framework.

The deployment of the RabbitMQ

You can install RabbitMQ on your own computer, but I recommend using Docker to install RabbitMQ. Docker is very convenient for RabbitMQ deployment. RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ: Docker RabbitMQ The Management version has an admin interface, for example

docker pull rabbitmq:3.82.-management
Copy the code

Wait for the pull to come down, and then we can run it (this is a simple installation of a single point of Rabbitmq, if you want to deploy a cluster, see my other blog post) with the following command:

docker run -d  -p 15672:15672  -p  5672:5672  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin --name rabbitmq rabbitmq:3.82.-management
Copy the code

The image has a Web-based console and Http API. The Http API can see how to use the address: http://localhost:15672/api/, here to explain instructions options

  • 15672: Specifies the RabbitMQ console port number. You can perform RabbitMQ operations on the console in a browser.
  • 5672: indicates the TCP port number listened by RabbitMQ. An application can use this port to establish a TCP connection with RabbitMQ and complete asynchronous communication
  • RABBITMQDEFAULTUSER: Used to set the user name for logging in to the console. Here I set admin
  • RABBITMQDEFAULTPASS: Used to set the password for login to the console. Here I set admin

After the container is successfully started, you can enter http://ip address :15672/ to access the console





Here’s a quick description of what the console list is for:

  • Overview: Used to view basic information about RabbitMQ (message queues, message sending rates, nodes, ports, and context information)
  • Connections: Used to query the RabbitMQ client connection information
  • Channels: Users can view the channel information of RabbitMQ
  • Exchange: Used to view RabbitMQ switches
  • Queues: Used to view RabbitMQ
  • Admin: Manage users and add users

The RabbitMQ SpringBoot integration

First create a project containing only web modules using Idea and then introduce rabbitMQ dependencies as follows:

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

We then do the following configuration in the application.properties main configuration file

Spring. The rabbitmq. Host = xxxxxxip address spring. The rabbitmq. Port =5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
Copy the code

Then we write a configuration file for RabbitMQ (to manage message queues), a Sender and a Receiver as follows:

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(a){
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context); }}Copy the code
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {

    @RabbitHandler
    public void process(String hello){
        System.out.println("Receiver : "+ hello); }}Copy the code
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Bean
    public Queue helloQueue(a){
        return new Queue("hello"); }}Copy the code

Tests and Results

You can also test the Bean class, which needs to be serialized.

@Autowired
private HelloSender helloSender;
@Test
public void hello(a) throws Exception {
	helloSender.send();
}
Copy the code