Integrating RocketMQ in Spring Boot, you need to perform the following steps to configure producers and consumers. Here is a simplified process:
1. Add dependencies
First, in yourAdd RocketMQ dependencies to the file. Make sure you are using a Spring Boot-compatible version.
<dependencies> <!-- Other dependencies --> <!-- RocketMQ Spring Boot Starter --> <dependency> <groupId></groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.2.3</version> <!-- Confirm this is the latest or suitable version for you --> </dependency> <!-- Other dependencies --> </dependencies>
2. Configuration
existsrc/main/resources/
Add the relevant configuration of RocketMQ in it.
rocketmq: name-server: 127.0.0.1:9876 # RocketMQ NameServer Address producer: group: your-producer-group # Producer's group name send-message-timeout: 3000 # Send message timeout, default 3 seconds consumer: group: your-consumer-group # Consumer group name
3. Create a producer
You can create a service class to send messages.
import ; import ; import ; @Service public class RocketMQProducer { @Autowired private RocketMQTemplate rocketMQTemplate; public void sendMessage(String topic, String message) { (topic, message); } }
4. Create a consumer
Next, create a listener to consume the message.
import ; import ; import ; @Service @RocketMQMessageListener(topic = "your-topic", consumerGroup = "your-consumer-group") public class RocketMQConsumer implements RocketMQListener<String> { @Override public void onMessage(String message) { ("Receive message: %s %n", message); // Process the received message } }
5. Use
- Inject where you need to send a message
RocketMQProducer
and call itsendMessage
method. - When a message is posted to the specified topic,
RocketMQConsumer
These messages will be automatically received and processed.
This is the end of this article about springboot rocketmq configuration producers and messagers. For more related contents of springboot rocketmq producers and messagers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!