Deploying RocketMQ with Docker and Integrating It into a Spring Boot Application
This guide walks through setting up a RocketMQ server using Docker Compose, configuring the necessary broker and name server files, and then demonstrates how to quickly start the services and integrate RocketMQ with Spring Boot via the rocketmq‑spring‑boot‑starter, including full code examples.
In this tutorial we explain how to install and run RocketMQ using Docker. First, clone the repository https://github.com/modouxiansheng/about-docker , then edit broker.conf to set the brokerIP1 parameter to your machine's IP address.
Next, create a docker-compose.yml file that defines three services: rmqnamesrv (the name server), rmqbroker (the broker), and rmqconsole (a web UI). The file also sets up the required volumes ( conf , logs , store ) and network configuration. The full content of the compose file is:
version: '3.5'
services:
rmqnamesrv:
image: foxiswho/rocketmq:server
container_name: rmqnamesrv
ports:
- 9876:9876
volumes:
- ./logs:/opt/logs
- ./store:/opt/store
networks:
rmq:
aliases:
- rmqnamesrv
rmqbroker:
image: foxiswho/rocketmq:broker
container_name: rmqbroker
ports:
- 10909:10909
- 10911:10911
volumes:
- ./logs:/opt/logs
- ./store:/opt/store
- ./conf/broker.conf:/etc/rocketmq/broker.conf
environment:
NAMESRV_ADDR: "rmqnamesrv:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: mqbroker -c /etc/rocketmq/broker.conf
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqbroker
rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
ports:
- 8080:8080
environment:
JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false"
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqconsole
networks:
rmq:
name: rmq
driver: bridgeAfter creating the docker-compose.yml , create the directories conf , logs , and store beside it, and place the provided broker.conf file inside conf . The broker.conf file contains standard RocketMQ settings; the only required change is updating brokerIP1 to the host IP.
Run docker-compose up in the directory containing the compose file. Once the containers start, open http://localhost:8080/ in a browser to see the RocketMQ console, confirming a successful deployment.
To use RocketMQ from a Spring Boot application, add the dependency org.apache.rocketmq:rocketmq-spring-boot-starter:2.0.3 (or the equivalent Gradle line implementation 'org.apache.rocketmq:rocketmq-spring-boot-starter:2.0.3' ) to your project.
Configure the producer in application.properties :
rocketmq.name-server=ip:9876
rocketmq.producer.group=my-groupExample producer code:
@SpringBootApplication
public class ProducerApplication implements CommandLineRunner {
@Resource
private RocketMQTemplate rocketMQTemplate;
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
}
}Configure the consumer similarly:
rocketmq.name-server=ip:9876Example consumer code:
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public static class MyConsumer1 implements RocketMQListener
{
@Override
public void onMessage(String message) {
log.info("received message: {}", message);
}
}
}With these steps you can run RocketMQ locally, monitor it via the web console, and send/receive messages from a Spring Boot application. The repository https://github.com/modouxiansheng/about-docker contains all the Docker files, and an alternative ready‑made project is available at https://github.com/modouxiansheng/Doraemon for those who prefer not to set up a Spring Boot project from scratch.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.