Getting started with rocketmq

Time:2020-2-13

1: What is MQ

MQ is message middleware.

 

2: Why MQ

Scene: e-commerce double 11:00 seconds. At that moment, requests from users will surge, and if nothing is done, the service is likely to be overwhelmed. But we can’t directly discard these requests. In order to expand the capacity of the machine in such a small period of time, it’s a big deal. So we naturally think about whether we can put these requests into a message queue first, and then the system takes the requests out of the message queue for logical processing and response. The stability of the service is guaranteed by lengthening the time dimension. This is MQ.

Using MQ only solves the problem that in the producer consumer mode, the data produced by the producer may suddenly surge, and the consumer can’t afford to consume.

 

3: Rocketmq

Rocketmq is an implementation of MQ. We have always stressed in the development that we should not build wheels repeatedly. Since we need an MQ, we need to find someone else’s MQ. Rocketmq is one of them. Of course, there are other MQ components, such as ActiveMQ, rabbitmq, Kafka.

 

4: Rocketmq Download

http://rocketmq.apache.org/release_notes

Download the bin package, such as rocketmq-all-4.3.2-bin-release.zip

 

Five: installation

Extract the downloaded file to the corresponding directory. For example, I unzip it to C: \ rocketmq-all-4.3.2

 

6: Start nameserver

Go to the directory C: \ rocketmq-all-4.3.2 \ bin and find mqnamesrv.cmd. Double click the operation.

 

Seven: start upBROKER

start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true

(if a prompt box appears saying “error: cannot find or load the main class xxxxx”. Open runbroker.cmd, and then double quote ‘% classpath%’. Save and re execute the start statement.)

 

So far, rocketmq is installed and started. Now we write the demo to use rocket to make a helloword

 

8: Write a producer, send a message

public class Producer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("rmq-group");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.setInstanceName("producer");
        producer.start();
        try {
            for (int i = 0; i < 10; i++) {
                Thread. Sleep (2000); // send a message every 2 seconds
                Message msg = new Message("TopicA-test",// topic
                        "TagA",// tag
                        (new Date() + "Hello RocketMQ ,QuickStart" + i)
                                .getBytes()// body
                );
                SendResult sendResult = producer.send(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        producer.shutdown();
    }
}

  

 

9: Write a consumer to listen to messages

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(
                "rmq-group");

        Consumer. Setnamesrvaddr ("127.0.0.1:9876"); // set the deployment address of the rocketmq service
        consumer.setInstanceName("consumer");
        /**
         *Topic and subexpression of the subscribed message.
         *Note: be sure to be consistent with the topic and subexpression of the message publisher
         */
        consumer.subscribe("TopicA-test", "TagA");

        Consumer. Registermessagelistener (New messagelistenercurrency() {// listener implementation
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(
                    List msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System. Out. Println (new string (MSG. Getbody()); // I print out every time I get a message
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("Consumer Started.");
    }
}

 

A simple demo is OK