RabbitMQ learning of Message Oriented Middleware -- summary 1

1. Overview of Message Oriented Middleware

1.1. What is message oriented middleware

The full name of MQ is Message Queue. Message Queue is the communication method between applications.

  • Why use MQ

    In the project, some time-consuming operations that do not need immediate return can be extracted for asynchronous processing, which greatly saves the server's request response time and improves the system throughput.

  • Message queues in development usually have the following application scenarios:

    1. Task asynchronous processing

    The message queue notifies the message receiver of long-time operations that do not need synchronous processing for asynchronous processing. Improved application response time.

    2. Application decoupling

    MQ is equivalent to an intermediary. The producer interacts with the consumer through MQ, which decouples the application.

    3. Peak cutting and valley filling

    For example, the order system will write data to the database when placing an order. However, the database can only support about 1000 concurrent writes per second. No matter how high the concurrency is, it is easy to go down. In the low peak period, there are more than 100 concurrencies, but in the peak period, the concurrency will suddenly surge to more than 5000. At this time, the database must be stuck.

  • Messages are saved by MQ, and then the system can consume according to its own consumption capacity, such as 1000 data per second, which can be slowly written to the database, so that the database will not be stuck.

    However, after using MQ, the speed of consuming messages is limited to 1000, but in this way, the data generated during the peak period is bound to be overstocked in MQ, and the peak period is "cut". However, due to the backlog of messages, the speed of consuming messages will remain at 1000QPS for a period of time after the peak period until the backlog of messages is consumed, which is called "filling the valley"

    1.2. AMQP and JMS

    MQ is the model of message communication; There are two main ways to implement MQ: AMQP and JMS.

    1.2.1. AMQP

    AMQP is a protocol, more precisely a binary wire level protocol. This is the essential difference between AMQP and JMS. AMQP does not limit from the API layer, but directly defines the data format of network exchange.

    1.2.2. JMS

    JMS, the Java message service application program interface, is an API for message oriented middleware (MOM) in the Java platform. It is used to send messages between two applications or in distributed systems for asynchronous communication.

    1.2.3. Difference between AMQP and JMS

  • JMS defines a unified interface to unify message operations; AMQP unifies the format of data interaction by specifying protocols
  • JMS defines that the Java language must be used; AMQP is only a protocol and does not specify the implementation method, so it is cross language.
  • JMS specifies two message modes; AMQP has richer message patterns  

1.3. Message queue products

Common message queues in the market are as follows:

  • ActiveMQ: JMS based
  • ZeroMQ: development based on C language
  • RabbitMQ: Based on AMQP protocol, developed in erlang language, with good stability
  • RocketMQ: Alibaba products based on JMS
  • Kafka: products similar to MQ; Distributed message system, high throughput

1.4. RabbitMQ

RabbitMQ is a message queue developed by erlang language and implemented based on AMQP (Advanced Message Queue protocol). It is a communication method between applications. Message queue is widely used in distributed system development.

RabbitMQ official address: Messaging that just works — RabbitMQ

RabbitMQ provides six modes: simple mode, work mode, Publish/Subscribe publishing and subscription mode, Routing routing mode, Topics topic mode, RPC remote call mode (remote call, not MQ; not introduced temporarily);

Introduction to the corresponding mode on the official website: RabbitMQ Tutorials — RabbitMQ

2. Install and configure RabbitMQ

3. RabbitMQ getting started

3.1. Construction example project

3.1.1. Add dependency

Add the following dependencies to the pom.xml file of the project:

<dependency>
   <groupId>com.rabbitmq</groupId>
   <artifactId>amqp-client</artifactId>
   <version>5.6.0</version>
</dependency>

3.2. Compilation

Write message producer com.conley.rabbitmq.simple.Producer

package com.conley.rabbitmq.simple;
​
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
​
public class Producer {
​
    static final String QUEUE_NAME = "simple_queue";
​
    public static void main(String[] args) throws Exception {
        //Create connection factory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //Host address; The default is localhost
        connectionFactory.setHost("localhost");
        //Connection port; The default is 5672
        connectionFactory.setPort(5672);
        //Virtual host name; Default to/
        connectionFactory.setVirtualHost("/itcast");
        //Connection user name; The default is guest
        connectionFactory.setUsername("conley");
        //Connection password; The default is guest
        connectionFactory.setPassword("conley");
​
        //Create connection
        Connection connection = connectionFactory.newConnection();
​
        // Create channel
        Channel channel = connection.createChannel();
​
        // Declare (create) queue
        /**
         * Parameter 1: queue name
         * Parameter 2: define persistence queue
         * Parameter 3: exclusive connection
         * Parameter 4: automatically delete queue when not in use
         * Parameter 5: other queue parameters
         */
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
​
        // Information to send
        String message = "Hello; Little rabbit!";
        /**
         * Parameter 1: switch name. If it is not specified, the Default Exchage will be used
         * Parameter 2: routing key. Simple mode can transfer queue name
         * Parameter 3: other properties of message
         * Parameter 4: message content
         */
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("Sent message:" + message);
​
        // close resource
        channel.close();
        connection.close();
    }
}
​

  After performing the above message transmission; You can log in to rabbitMQ's administrative console to discover the queue and its messages:

 

  3.3. Writing consumer

Extract the tool class com.conley.rabbitmq.util.ConnectionUtil for creating connection;

package com.conley.rabbitmq.util;
​
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
​
public class ConnectionUtil {
​
    public static Connection getConnection() throws Exception {
        //Create connection factory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //Host address; The default is localhost
        connectionFactory.setHost("localhost");
        //Connection port; The default is 5672
        connectionFactory.setPort(5672);
        //Virtual host name; Default to/
        connectionFactory.setVirtualHost("/itcast");
        //Connection user name; The default is guest
        connectionFactory.setUsername("conley");
        //Connection password; The default is guest
        connectionFactory.setPassword("conley");
​
        //Create connection
        return connectionFactory.newConnection();
    }
​
}

Consumer who wrote the message: com.itheima.rabbitmq.simple.Consumer

package com.conley.rabbitmq.simple;
​
import com.conley.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
​
import java.io.IOException;
​
public class Consumer {
​
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
​
        // Create channel
        Channel channel = connection.createChannel();
​
        // Declare (create) queue
        /**
         * Parameter 1: queue name
         * Parameter 2: define persistence queue
         * Parameter 3: exclusive connection
         * Parameter 4: automatically delete queue when not in use
         * Parameter 5: other queue parameters
         */
        channel.queueDeclare(Producer.QUEUE_NAME, true, false, false, null);
​
        //Creating consumers; And set message processing
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            /**
             * consumerTag The message sender tag can be specified in channel.basicConsume
             * envelope The content of the message package, from which you can obtain the message id, message routingkey, switch, message and retransmission flag (whether you need to resend after receiving the message failure)
             * properties Attribute information
             * body news
             */
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //Routing key
                System.out.println("route key Is:" + envelope.getRoutingKey());
                //Switch
                System.out.println("The switch is:" + envelope.getExchange());
                //Message id
                System.out.println("news id Is:" + envelope.getDeliveryTag());
                //Messages received
                System.out.println("The received message is:" + new String(body, "utf-8"));
            }
        };
        //Listen for messages
        /**
         * Parameter 1: queue name
         * Parameter 2: whether to confirm automatically. If it is set to true, it means that the message is received and replied to mq automatically. If mq receives the reply, it will delete the message. If it is set to false, it needs to be confirmed manually
         * Parameter 3: callback after receiving message
         */
        channel.basicConsume(Producer.QUEUE_NAME, true, consumer);
​
        //If you do not close the resource, you should always listen for messages
        //channel.close();
        //connection.close();
    }
}

3.4. Summary of this summary

In the above introductory case, the following simple mode is actually used:

In the model above, there are the following concepts:

  • P: The producer is the program that sends the message
  • C: Consumer: the receiver of the message will always wait for the message to arrive.
  • Queue: message queue, shown in red. Similar to a mailbox, messages can be cached; The producer delivers the message to it, and the consumer takes the message out of it.

In rabbitMQ, consumers must go to a message queue to get messages  

Tags: Java RabbitMQ

Posted on Tue, 12 Oct 2021 01:47:46 -0400 by jahtex