This is the 11th day of my participation in the First Challenge 2022.

1. What is RabbitMQ

1.1 it

Before you get to Know RabbitMQ, know what AMQP is. AMQP (Advanced Message Queuing Protocol) is a widely used messaging Protocol developed in an independent language. It defines Message flows in a second-tier format.

Clients based on the AMQP protocol can pass messages to intermediate services, and any language can implement the protocol and communicate with intermediate services.

1.2 the RabbitMQ

RabbitMQ is a message middleware based on the AMQP protocol and developed using Erlang. As an excellent message system, RabbitMQ has the advantages of high concurrency and scalability, and is suitable for communication between modules in large systems.

RabbitMQ features are:

  • Features such as persistence, transport confirmation, and publication confirmation ensure that messages are reliable
  • Support multiple message distribution modes, more flexible processing
  • Provides a visual management interface for easy use
  • Cluster deployment is supported to ensure high service availability

2. The RabbitMQ installation

Rabbitmq is a third-party message queue framework. You need to install the rabbitMQ service independently. For details about the installation process, see related tutorials.

  • Install rabbitMQ on Windows
  • Installing RabbitMQ in Linux and faQs

The process can be summarized as:

  1. Install the Erlang service
  2. Install RabbitMQ. Erlang dependencies are automatically installed for versions 3.9.10 and later, skip step 1
  3. Start the RabbitMQ service and log in to the background service using the account

3. Integrate RabbitMQ into the project

SpringBoot import RabbitMQ official document

3.1 Introducing dependency information

Spring Boot has encapsulated AMQP. You only need to import the startup dependency information of Spring-boot-starter-AMQP in the project dependency.

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

3.2 Editing Configuration Information

3.2.1 Configuration File

After the RabbitMQ dependency information is imported, set the rabbitMQ service address, port number, and account for connecting to the service in the project configuration file.

# 1 RabbitMQ configuration
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
# 2 use the address attribute directly
spring.rabbitmq.addresses=amqp://guest:guest@localhost:5672
Copy the code
  • The two methods have the same effect. You can configure either method
  • In the second method, the host address and port number are the default valueslocalhost:5672Can be omitted
  • Both producers and consumers use the configuration, and note that the default connection port number is 5672, not 15672 on the admin page

3.2.2 Exchange and Queue Configuration

To use RabbitMQ, you need to create a Queue as a message carrier to store the data generated by producers and provide it to consumers for processing. You can create a Queue either on the management interface or in your code.

  1. Management Interface Configuration

After the RabbitMQ service is started, you can log in to the management page using your account to manage rabbitMQ container components.

There are seven Exchanges in RabbitMQ by default. You can manually add custom Exchanges in the Exchanges TAB.

Switch to the Queues TAB to add a Queue.

For Exchanges and queues, click the Exchange name on the Exchanges TAB and bind the Queue with Routing Key.

Once the binding is complete, the Roting Key and Queue name can be used in the project to specify the contents of the sent and received messages.

  1. Declare the configuration in the project

The management interface is a visual operation, which is prone to human operation. Exchange and Queue messages can be declared in the code configuration, and containers will be created automatically if they are not present when used.

@Configuration
public class RabbitMQConfig {
    /** * Custom message queue *@return* /
    @Bean
    public Queue rabbitmqQueue(a){
        return new Queue("queue".true.false.false);
    }

    /** * Custom switch *@return* /
    @Bean
    public DirectExchange rabbitmqDirectExchange(a){
        return new DirectExchange("directExchange".true.false);
    }

    /** * associate bind *@return* /
    @Bean
    public Binding bindDirect(a){
        return BindingBuilder.bind(rabbitmqQueue()).to(rabbitmqDirectExchange()).with("routingKey"); }}Copy the code
  • The queue is declared withorg.springframework.amqp.core.QueueIn the Queue
  • Declare the container object and add it to the Spring container. It will be created automatically in the RabbitMQ service at startup
  • When creating queues and switches, the parameters passed in represent the following:
    • Durable: Default false, whether messages are persistent, that is, whether data is stored in memory or on disks when no consumers consume data
    • Exclusive, which defaults to false. Specifies whether I can be used only by the currently created connection and will be automatically deleted when closed
    • AutoDelete: indicates whether the container will be automatically deleted. If this parameter is true, the container will be automatically deleted when it is no longer used

3.3 Producers generate data

As the producer of the message, the producer sends data to the message queue and specifies the queue name when sending the data.

  1. Add the configuration in 3.2.1 to the producer project to connect to the RabbitMQ service
  2. The message is sent using the RabbitTemplate object built into SpringBoot
@Autowired
private RabbitTemplate rabbitTemplate;

public void sendMessage2Queue(String message) {
    rabbitTemplate.convertAndSend("exchangeName"."routingKey",message);
}
Copy the code
  • The convertAndSend method sends the message message to the exchangeName switch and forwards it to the bound Queue via the routingKey
  • An empty string routingKey is forwarded to all bound queues

3.4 Consumers read data

The consumer, as the consuming party, listens to the queue container specified in RabbitMQ and consumes data when it is in the queue.

  1. The rabbitMQ connection configuration described in 3.2.1 also needs to be added to the project
  2. Use the @RabbitListener annotation class to listen on the specified queue and use the @Rabbithandler annotation method to read the message data
@Component
@RabbitListener(queues = {"queueName"})
public class RabbitMQConsumer {

    @RabbitHandler
    public void consumer(String message){
        System.out.println("Get message to queue:"+ message); }}Copy the code