Master RocketMQ: Core Concepts and Spring Boot Integration Guide

This tutorial explains RocketMQ's architecture—including producers, brokers, consumers, topics, tags, and consumption models—then walks through installing the service, configuring the environment, and integrating it with Spring Boot using Maven dependencies, YAML settings, and sample producer and consumer code.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master RocketMQ: Core Concepts and Spring Boot Integration Guide

Environment: springboot2.3.9 + RocketMQ4.8.0

RocketMQ Architecture and Concepts

1. Message Model

2. Producer

3. Consumer

4. Topic

5. Broker Server

6. Name Server

7. Pull Consumer

8. Push Consumer

9. Producer Group

10. Consumer Group

11. Clustering Consumption

12. Broadcasting Consumption

13. Normal Ordered Message

14. Strictly Ordered Message

15. Message

16. Tag

RocketMQ consists of three core components: Producer (creates messages), Broker (stores and forwards messages), and Consumer (receives messages). Each Broker runs on a server and can host multiple Topics, which are divided into Message Queues. Consumers belong to ConsumerGroups, enabling load balancing and fault tolerance.

RocketMQ Service Setup

1. Download RocketMQ

2. Configure environment variables

3. Start Name Server

4. Start Broker

5. Send & receive messages via command line

Set environment variable:

C:\Users\MSI-NB>set NAMESRV_ADDR=localhost:9876

Send message:

C:\Users\MSI-NB>tools org.apache.rocketmq.example.quickstart.Producer

Receive message:

Spring Boot Integration with RocketMQ

Dependency:

<dependency>
  <groupId>org.apache.rocketmq</groupId>
  <artifactId>rocketmq-spring-boot-starter</artifactId>
  <version>2.2.0</version>
</dependency>

Configuration (application.yml):

rocketmq:
  nameServer: localhost:9876
  producer:
    group: demo-mq

Producer service:

@Service
public class ProducerService {

    @Resource
    private RocketMQTemplate rocketMQTemplate ;

    public void send(String message) {
        rocketMQTemplate.convertAndSend("test-topic", message);
    }

}

Consumer listener:

@RocketMQMessageListener(topic = "test-topic", consumerGroup = "consumer01-group")
@Component
public class ConsumerListener implements RocketMQListener<String> {

    @Override
    public void onMessage(String message) {
        System.out.println("Received message: " + message);
    }

}

REST controller to trigger sending:

@RestController
@RequestMapping("/messages")
public class MessageController {

    @Resource
    private ProducerService ps ;

    @GetMapping("")
    public Object send(String message) {
        ps.send(message) ;
        return "send success" ;
    }

}

Sending messages with specific tags:

rocketMQTemplate.convertAndSend("test-topic:tag1", message);

Receiving messages with tag filter:

@RocketMQMessageListener(topic = "test-topic", consumerGroup = "consumer01-group", selectorExpression = "tag1")

The selectorExpression defaults to "*"; specifying "tag1" ensures only messages with that tag are consumed. The topic and tag are separated by a colon, and the tag is the second element of the split array.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Spring BootMessage Queue
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.