“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

Download the source code

When reading the RocketMQ source code, we first need to download the source code from the open source website to the local, and then start it. This will make it easier for us to read the source code, some places do not understand, we can use local Debug to track the results of execution, so that we can better understand the author’s design ideas. Source code download address:

www.apache.org/dyn/closer….

After downloading the source code locally, import it into Idea, configure maven address, and import the dependency

Start the MQ

Start the Namesrv module

NamesrvStartup = NamesrvStartup = NamesrvStartup

Please set the ROCKETMQ_HOME variable in your environment to match the location of the RocketMQ installation

The ROCKETMQ_HOME environment variable needs to be configured to be added when NamesrvStartup is started

ROCKETMQ_HOME = E: \ workspace \ source \ rocketmq \ rocketmqHome

At this point the startup will still report an errorAs you can see, this is when to start to configure the directory, find the conf directory logback_namesrv. XML log configuration file, in the configuration directory to create the conf directory, then the source code distribution directory under the conf directory: Copy the logback_namesrv. XML file and start it again

The Name Server boot success. serializeType=JSON

Start the Broker module

Find the Main method in the BrokerStartup class under the RocketMQ-Broker module in the engineering class and start it, which also appears when started:

Please set the ROCKETMQ_HOME variable in your environment to match the location of the RocketMQ installation

The ROCKETMQ_HOME configuration is also required in the startup environment. After the configuration is complete, the following error will be reported when starting again:

In this case, copy the logback_broker.xml file from the conf file in the distribution directory to the conf file in the configured directory and start it again

The broker[silly, 192.168.0.103:10911] Boot Success. SerializeType =JSON

There is also a broker.conf file in the distribution directory. This file is used to set broler startup parameters and specify the location of the file during startup

-c E:\workspace\ source \ Rocketmq \rocketmqHome\conf\broker.conf

At this time the following prompt will appear:

The broker[broker-a, 192.168.0.103:10911] Boot Success. SerializeType =JSON

As you can see, the broler name for this startup is Broker-a because brokerName is configured in broker.conf, indicating that this startup reads the configuration file we specified

brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
Copy the code

Verify startup results

Send a message

To verify that the startup was successful, use the producer and consumer codes provided in the source code to find the producer code in Rocketmq-Example:

org.apache.rocketmq.example.quickstart.Producer
Copy the code

Modify the source code, which is NameServer address after startup

producer.setNamesrvAddr("127.0.0.1:9876");
Copy the code

Starting at this time will report an error

org.apache.rocketmq.client.exception.MQClientException: No route info of this topic: TopicTest

We need to add a configuration line to the broker.conf file to allow the client to create the topic automatically and restart the broker when it is used

autoCreateTopicEnable = true

The same problem occurred when SENDING MQ again. I configured the NameServer address in the broker.conf file

namesrvAddr = 127.0. 01.:9876
Copy the code

When you restart the broker, the startup message is printed as follows

The broker[broker-a, 192.168. 0103.:10911] boot success. serializeType=JSON and name server is 127.0. 01.:9876
Copy the code

If you send the message again, it will succeed

News consumption

Find the org. Apache. Rocketmq. Example. Quickstart. Consumer code in this class, specify the NameServer address after startup

consumer.setNamesrvAddr("127.0.0.1:9876");
Copy the code

You can view the consumer log printed by the console

Through the above steps, a basic stand-alone version of RocetMQ is successfully launched from source code.

Small configuration

Log file directory Settings

${user.name}; ${user.name}; ${user.name}; ROCKETMQ_HOME, and then restarting, logs to custom logs