Run RocketMQ locally

This section will describe steps to quickly deploy a RocketMQ cluster with a single node; Commands to send and receive messages to/from it are also included as proof of work.

Run RocketMQ locally - 图1SYSTEM REQUIREMENT

  1. 64-bit OS,Linux/Unix/macOS is recommended
  2. 64-bit JDK 1.8+

1.Get Apache RocketMQ

Run RocketMQ locally - 图2Download RocketMQ

Apache RocketMQ is distributed both in binary and source packages. Click here to download Apache RocketMQ 5.2.0 source package. You may prefer prebuilt binary package, which can be run directly since it has been compiled.

The following instruction takes the application of RocketMQ 5.2.0 source package in Linux environment as an example in order to introduce the installation process of RocketMQ.

Extract the source package of RocketMQ 5.2.0, then compile and build the binary executables:

  1. $ unzip rocketmq-all-5.2.0-source-release.zip
  2. $ cd rocketmq-all-5.2.0-source-release/
  3. $ mvn -Prelease-all -DskipTests -Dspotbugs.skip=true clean install -U
  4. $ cd distribution/target/rocketmq-5.2.0/rocketmq-5.2.0

2. Start NameServer

After the installation of RocketMQ, start the NameServer:

  1. ### start namesrv
  2. $ nohup sh bin/mqnamesrv &
  3. ### verify namesrv
  4. $ tail -f ~/logs/rocketmqlogs/namesrv.log
  5. The Name Server boot success...

Run RocketMQ locally - 图3info

Once we see ‘The Name Server boot success..’ from namesrv.log, it means the NameServer has been started successfully.

3. Start Broker and Proxy

After nameserver startup, we need start the broker and proxy. We recommend Local deployment mode, where Broker and Proxy are deployed in the same process. We also support cluster deployment mode. Learn more Deployment introduction.

  1. ### start broker
  2. $ nohup sh bin/mqbroker -n localhost:9876 --enable-proxy &
  3. ### verify broker
  4. $ tail -f ~/logs/rocketmqlogs/proxy.log
  5. The broker[broker-a,192.169.1.2:10911] boot success...

Run RocketMQ locally - 图4info

Once we see “The broker[brokerName,ip:port] boot success..” from proxy.log, it means the Broker has been started successfully.

Run RocketMQ locally - 图5note

Thus far, a single-Master RocketMQ cluster has been deployed, and we are able to send and receive simple messages by scripts.

4. Send and Receive Messages with tools

Before test with tools, we need set the nameserver address to system. like system environment variables NAMESRV_ADDR.

  1. $ export NAMESRV_ADDR=localhost:9876
  2. $ sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
  3. SendResult [sendStatus=SEND_OK, msgId= ...
  4. $ sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
  5. ConsumeMessageThread_%d Receive New Messages: [MessageExt...

5. Send and Receive Messages with SDK

We can also try to use the client sdk to send and receive messages, you can see more details from rocketmq-clients.

  1. Create a java project.

  2. Add sdk dependency to pom.xml, remember to replace the rocketmq-client-java-version with the latest release.

    1. <dependency>
    2. <groupId>org.apache.rocketmq</groupId>
    3. <artifactId>rocketmq-client-java</artifactId>
    4. <version>${rocketmq-client-java-version}</version>
    5. </dependency>
  3. Create topic by mqadmin cli tools.

    1. $ sh bin/mqadmin updatetopic -n localhost:9876 -t TestTopic -c DefaultCluster
  4. In the Java project you have created, create a program that sends messages and run it with the following code:

    1. import java.io.IOException;
    2. import org.apache.rocketmq.client.apis.ClientConfiguration;
    3. import org.apache.rocketmq.client.apis.ClientConfigurationBuilder;
    4. import org.apache.rocketmq.client.apis.ClientException;
    5. import org.apache.rocketmq.client.apis.ClientServiceProvider;
    6. import org.apache.rocketmq.client.apis.message.Message;
    7. import org.apache.rocketmq.client.apis.producer.Producer;
    8. import org.apache.rocketmq.client.apis.producer.SendReceipt;
    9. import org.slf4j.Logger;
    10. import org.slf4j.LoggerFactory;
    11. public class ProducerExample {
    12. private static final Logger logger = LoggerFactory.getLogger(ProducerExample.class);
    13. public static void main(String[] args) throws ClientException, IOException {
    14. String endpoint = "localhost:8081";
    15. String topic = "TestTopic";
    16. ClientServiceProvider provider = ClientServiceProvider.loadService();
    17. ClientConfigurationBuilder builder = ClientConfiguration.newBuilder().setEndpoints(endpoint);
    18. ClientConfiguration configuration = builder.build();
    19. Producer producer = provider.newProducerBuilder()
    20. .setTopics(topic)
    21. .setClientConfiguration(configuration)
    22. .build();
    23. Message message = provider.newMessageBuilder()
    24. .setTopic(topic)
    25. .setKeys("messageKey")
    26. .setTag("messageTag")
    27. .setBody("messageBody".getBytes())
    28. .build();
    29. try {
    30. SendReceipt sendReceipt = producer.send(message);
    31. logger.info("Send message successfully, messageId={}", sendReceipt.getMessageId());
    32. } catch (ClientException e) {
    33. logger.error("Failed to send message", e);
    34. }
    35. // producer.close();
    36. }
    37. }
  5. In the Java project you have created, create a consumer demo program and run it. Apache RocketMQ support SimpleConsumer and PushConsumer.

    1. import java.io.IOException;
    2. import java.util.Collections;
    3. import org.apache.rocketmq.client.apis.ClientConfiguration;
    4. import org.apache.rocketmq.client.apis.ClientException;
    5. import org.apache.rocketmq.client.apis.ClientServiceProvider;
    6. import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
    7. import org.apache.rocketmq.client.apis.consumer.FilterExpression;
    8. import org.apache.rocketmq.client.apis.consumer.FilterExpressionType;
    9. import org.apache.rocketmq.client.apis.consumer.PushConsumer;
    10. import org.slf4j.Logger;
    11. import org.slf4j.LoggerFactory;
    12. public class PushConsumerExample {
    13. private static final Logger logger = LoggerFactory.getLogger(PushConsumerExample.class);
    14. private PushConsumerExample() {
    15. }
    16. public static void main(String[] args) throws ClientException, IOException, InterruptedException {
    17. final ClientServiceProvider provider = ClientServiceProvider.loadService();
    18. String endpoints = "localhost:8081";
    19. ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
    20. .setEndpoints(endpoints)
    21. .build();
    22. String tag = "*";
    23. FilterExpression filterExpression = new FilterExpression(tag, FilterExpressionType.TAG);
    24. String consumerGroup = "YourConsumerGroup";
    25. String topic = "TestTopic";
    26. PushConsumer pushConsumer = provider.newPushConsumerBuilder()
    27. .setClientConfiguration(clientConfiguration)
    28. .setConsumerGroup(consumerGroup)
    29. .setSubscriptionExpressions(Collections.singletonMap(topic, filterExpression))
    30. .setMessageListener(messageView -> {
    31. logger.info("Consume message successfully, messageId={}", messageView.getMessageId());
    32. return ConsumeResult.SUCCESS;
    33. })
    34. .build();
    35. Thread.sleep(Long.MAX_VALUE);
    36. // pushConsumer.close();
    37. }
    38. }

6. Shutdown Servers

After finishing the practice, we could shut down the service by the following commands.

  1. $ sh bin/mqshutdown broker
  2. The mqbroker(36695) is running...
  3. Send shutdown request to mqbroker(36695) OK
  4. $ sh bin/mqshutdown namesrv
  5. The mqnamesrv(36664) is running...
  6. Send shutdown request to mqnamesrv(36664) OK