RocketMQ is a distributed messaging middlewareoriginallyIt is developed by Alibaba messaging middleware team and applied to production system on a large scaleTo meet the demand of massive online information accumulation, it was donated to Apache Open Source Foundation at the end of 2016 to become an incubation project, and became an Apache top-level project in less than a year.

In the early stage, Ali used to develop message system based on ActiveMQ. As the scale of business message increased, the bottleneck gradually appeared. Later, It also considered Kafka. RocketMQ and Kafka are so similar in concept and principle that they are often compared; RocketMQ uses the long polling pull mode by default and supports tens of millions of message stacks on a single machine, making it an excellent application in massive message systems.

This paper is divided into three parts, as shown in the figure below:

1 Install the RocketMQ version

(1) Download the Windows installation package

Download for Windows version:Rocketmq.apache.org/release_not…



Download and unpack the RocketMQ installation package.

(2) Configure system environment variables

Configure the system variable ROCKETMQ_HOME= “D:\soft\ Rocketmq-all-4.5.1-bin-release”, as shown below:

Note: RocketMQ is stored in a different directory for everyone. Mine is in D:\soft, and users can configure system variables according to their environment.

Since the environment variable %ROCKETMQ_HOME% is used in the next launch of MQNAMesrv.cmd, this system variable needs to be configured here.

Start namesRV

Enter the bin directory of RocketMQ and run start mqnamesrv. CMD, as shown in the following figure:



Note: You cannot close this window after startup.

Start the broker

Or under the bin directory to perform start mqbroker. CMD – n127.0.0.1:9876 autocreatetopicenable = true, execution success as shown in the figure below:



Also do not close the above Windows.

Complete the following steps to show that your RocketMQ is successful.

2 Install the visual plug-in

(1) Download the plug-in

Open the connectionGithub.com/apache/rock…Download the visualization plug-in Rocketmq-externals, as shown below:



Click Download ZIP to Download it.

(2) Configure plug-ins

After downloading, go to Rocketmq-externals rocketmq-console SRC main resources application.properties to configure it, as shown below:



The main fields are described as follows:

Server.port =8066: The running port of this visual plug-in.

Rocketmq. Config. NamesrvAddr = 127.0.0.1:9876: rocketmq link information.

(3) Compile the plug-in

Go to rocketmq-externals\ RocketMq-console and execute MVN cleanpackage-dmaven.test.skip =true

Compile the project.

The following figure shows the successful compilation:



The following two problems may occur during the compilation phase: MVN command is not found, or the compilation is super slow. The following solutions are provided.

Problem 1: MVN command is not runnable solution: MAVEN_HOME= MAVEN_HOME= Maven installation directory. Add %MAVEN_HOME%\bin to path. Restart the command line tool (CMD) to re-execute the command.

Problem 2: Super slow compilation solution: this is caused by using Maven source as foreign source, only need to configure Ali Maven source. Open conf/setting. XML in the Maven directory and add the following information to the mirrors node:

<mirror>
 
<id>
alimaven
</id>
 
<name>
aliyun maven
</name>
 
<url>
http://maven.aliyun.com/nexus/content/groups/public/
</url>
 
<mirrorOf>
central
</mirrorOf>
        
</mirror>
Copy the code

(4) Run the plug-in

After compiling successfully, go to the target folder and run java-jar rocketmq-console-ng-1.0.1.jar to start the program.

After successful startup, type the address in your browserhttp://127.0.0.1:8066Access, the effect is as follows:

3 Basic Use

Add the following code to the jar package pom.xml

<! -- https://mvnrepository.com/artifact/com.alibaba.rocketmq/rocketmq-client --> <dependency> <groupId> Rocketmq </groupId> <artifactId> RocketMQ-client </artifactId> <version> 3.6.2.final </version> </dependency>Copy the code

(2) Add producer and consumer codes

public class RocketMQDemo {
    static final String MQ_NAMESRVADDR = "localhost:9876"; Public static void main(String[] args) {// groupName String groupName ="myGroup-1"; // topicName String topicName ="myTopic-1"; // Label name String tagName ="myTag-1"; new Thread(() -> { try { producer(groupName, topicName, tagName); } catch (InterruptedException e) { e.printStackTrace(); } catch (RemotingException e) { e.printStackTrace(); } catch (MQClientException e) { e.printStackTrace(); } catch (MQBrokerException e) { e.printStackTrace(); } }).start(); new Thread(() -> { try { consumer(groupName, topicName, tagName); } catch (MQClientException e) { e.printStackTrace(); } }).start(); } /** * @description producer * @author wanglei * @param [groupName topicName topicName, **/ public static void producer(String groupName, String topicName, String tagName) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { DefaultMQProducer producer = new DefaultMQProducer(groupName); producer.setNamesrvAddr(MQ_NAMESRVADDR); producer.start(); String body ="Hello, Lao wang"; Message message = new Message(topicName, tagName, body.getBytes()); producer.send(message); producer.shutdown(); } /** * @description consumer * @author wanglei * @param [groupName, topicName, **/ public static void consumer(String groupName, String topicName, String tagName) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName); consumer.setNamesrvAddr(MQ_NAMESRVADDR); consumer.subscribe(topicName, tagName); consumer.registerMessageListener(newMessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(
                    List
<MessageExt>
 msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println(new String(msg.getBody()));
                }
                returnConsumeConcurrentlyStatus.CONSUME_SUCCESS; }}); consumer.start(); }}Copy the code

The execution results of the above procedures are as follows:

Hello, Lao wangCopy the code