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.
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:9876Send message:
C:\Users\MSI-NB>tools org.apache.rocketmq.example.quickstart.ProducerReceive 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-mqProducer 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
