I wish you a happy New Year and a happy Year of the Ox.

1 introduction

Message-oriented middleware is a commonly used component in our system and has many application scenarios in our business. We can use it to realize asynchronous processing of business, decoupling between systems and peak traffic cutting. There are many different messaging-oriented middleware (RocketMq, RabbitMq, Kafka, etc.), and in this article I will focus on the basics of RocketMq, which I use a lot in my work.

RocketMq is a low latency, high performance, high reliability, distributed messaging middleware from Alibaba open Source. The project was written in the Java language and has been contributed to the Apache Foundation.

2 features

RocketMQ offers a wealth of features, briefly listed below, described in its official documentation.

  • Subscribe and publish
  • The order message
  • The message filter
  • Message reliability
  • At least once
  • Transaction message
  • Timing of the message
  • Message retry
  • Flow control
  • Dead-letter queue

3 architecture

RocketMq’s architecture is shown in the figure above. It consists of NameServer, Broker, Producer, and Consumer, all of which support cluster deployment.

3.1 NameServer

NameServer is a simple Topic routing registry that acts like Zookeeper in Dubbo and supports dynamic registration and discovery of brokers.

NameServer is almost stateless. There are no independent nodes in the cluster that communicate with each other, and each node records complete routing information.

NameServer mainly provides Broker management and routing information management. When the Broker starts, it registers its own information in the NameServer cluster and reports Topic routing information periodically. After that, NameServer removes non-viable Broker nodes through heartbeat monitoring. Producer and Consumer obtain routing information from NameServer for sending and consuming messages.

3.2 the Broker

Broker Acts as a message Broker, storing and forwarding messages.

When the Broker starts, it connects to all services in the NameServer and registers its own information. And regularly report their own Topic routing information.

The Broker supports one master with many slaves, but only one slave participates in the read load of the message.

3.3 Producer

A Producer is a Producer of messages. Responsible for delivering messages generated by the business system to the corresponding Broker server via the MQ load-balancing module, with low latency and fast failure support.

RocketMQ provides synchronous, asynchronous, sequential, one-way delivery.

3.4 Consumer

A Consumer is a message Consumer. Messages can be consumed in push and pull modes. At the same time, it also supports the consumption of cluster mode and broadcast mode. It provides real-time message subscription mechanism, which can meet the needs of most users.

3.5 Communication Mechanism

The communication process among NameServer, Broker, Producer and Consumer is as follows:

  • BrokerAfter startup, you need to complete a registration to yourselfNameServerThe operation is then timed to every 30 secondsNameServerreportTopicRouting information.
  • When a message producer sends a message as a client, it needs to be based on the messageTopicCached locallyTopicPublishInfoTableObtain routing information. If not, followNameServerPull up and update whileProducerThe default value is 30 secondsNameServerPull the routing information once.
  • The message producer selects a queue from the obtained routing information to send the message.BrokerAs the receiver of the message, the received message is stored.
  • The message consumer gets the routing information as well as the producer and selects one or more message queues to pull and consume messages through the client’s load balancing.

RocketMQ customizes communication protocols and extends communication modules on top of Netty. We will elaborate on this in a later article.

Installation and use of RocketMq

4.1 installation

Installation of RocketMq is relatively simple, as follows:

/ / on the git download and unpack the installation package, the address is: https://github.com/apache/rocketmq/tags
/ / start NameServer
nohup sh bin/mqnamesrv &

/ / start the Broker
nohup sh bin/mqbroker -n localhost:9876 &

Copy the code

So far ourRocketMqWe installed one to make it easier to use and view messagesRocketMqConsole, the project address is as follows:Github.com/apache/rock…

You can download it, compile it and start it. The content of the console is as follows

4.2 the use of

Now that we have RocketMq installed, let’s take a look at using RocketMQ-Client to send and consume messages.

Create a Maven project and add dependencies:

<! -- https://mvnrepository.com/artifact/org.apache.rocketmq/rocketmq-client -->
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
    <version>4.8.0</version>
</dependency>
Copy the code

The message producer code is as follows

public static void main(String[] args) throws Exception {
    DefaultMQProducer producer = new DefaultMQProducer("TEST_TOPIC_PRODUCER_ONE");
    producer.setNamesrvAddr("127.0.0.1:9876");
    producer.start();

    Message message = new Message("TEST_TOPIC".null."Hello RocketMQ".getBytes());
    SendResult sendResult = producer.send(message);
    System.out.printf("%s%n", sendResult);
    producer.shutdown();
}
Copy the code

Message consumers are as follows:

public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("TEST_TOPIC_CONSUMER_ONE");
    consumer.setNamesrvAddr("localhost:9876");

    consumer.subscribe("TEST_TOPIC"."*");
    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println("Receive new message: " + msgs);
            returnConsumeConcurrentlyStatus.CONSUME_SUCCESS; }}); consumer.start(); }Copy the code

The basic use of RocketMq is now complete. We have only covered the use of ordinary messages here, and we will cover the use of various RocketMq messages and their usage scenarios in more detail in a later article.

5 concludes

This is the end of today’s content. In this article, we introduce the basic knowledge of RocketMq and the basic use of the client. We hope that you can understand the RocketMq architecture and the role of its components and communication process.

Keep an eye out for more in a follow-up article.