2. Source code analysis

2.1 Environment Construction

Depend on the tool

  • The JDK: 1.8 +
  • Maven
  • IntelliJ IDEA

2.1.1 Source code pull

From the official warehouse github.com/apache/rock… Clone or Download the source code

Source directory structure:

  • Broker: Broker module (Broke starts the process)
  • Client: indicates the message client, including classes related to message producers and message consumers
  • Common: public package
  • Dev: Developer info (not source code)
  • Distribution: Deployment instance folder (non-source code)
  • Example: RocketMQ example code
  • Filter: basic classes related to message filtering
  • Filtersrv: Message filtering server implementation classes (Filter to start the process)
  • Logappender: Log implementation-related class
  • Namesrv: NameServer implementation related classes (NameServer start process)
  • Openmessage Ageing: An open standard for messages
  • Remoting: Remote communication module, given to Netty
  • Srcutil: service utility class
  • Store: Message store implementation related classes
  • Style: checkstyle related implementation
  • Test: Tests related classes
  • Tools: tools class that monitors commands

2.1.2 import IDEA

Perform the installation

clean install -Dmaven.test.skip=true
Copy the code

2.1.3 debugging

Create a CONF configuration folder and copy broker.conf and logback_broker.xml and logback_namesrv.xml from distribution

1) Start NameServer

  • Expand the namesrv module and right-click namesrvstartup. Java

  • Configuration ROCKETMQ_HOME

  • Restart the

Console print results

The Name Server boot success. serializeType=JSON
Copy the code

2) Start the Broker

Broker. Conf Configuration file contents

brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
# namesrvAddr address
namesrvAddr=127.0.0.1:9876
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
autoCreateTopicEnable=true
# storage path
storePathRootDir=E:\\RocketMQ\\data\\rocketmq\\dataDir
# commitLog path
storePathCommitLog=E:\\RocketMQ\\data\\rocketmq\\dataDir\\commitlog
Message queue storage path
storePathConsumeQueue=E:\\RocketMQ\\data\\rocketmq\\dataDir\\consumequeue
Message index storage path
storePathIndex=E:\\RocketMQ\\data\\rocketmq\\dataDir\\index
# checkpoint File path
storeCheckpoint=E:\\RocketMQ\\data\\rocketmq\\dataDir\\checkpoint
# abort file storage path
abortFile=E:\\RocketMQ\\data\\rocketmq\\dataDir\\abort
Copy the code
  • Create a data folderdataDir
  • Start theBrokerStartup, the configurationbroker.confandROCKETMQ_HOME

3) Send a message

  • Enter the example moduleorg.apache.rocketmq.example.quickstart
  • Specify the Namesrv address
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
producer.setNamesrvAddr("127.0.0.1:9876");
Copy the code
  • runmainMethod to send a message

4) Consumption news

  • Enter the example moduleorg.apache.rocketmq.example.quickstart
  • Specify the Namesrv address
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
consumer.setNamesrvAddr("127.0.0.1:9876");
Copy the code
  • runmainMethod, consuming messages