RabbitMQ is one of the most reliable and widely-used message brokers available today. It helps manage asynchronous communication in microservice architectures and supports features like message queuing, routing, and retries out of the box. If you're using Spring Boot, integrating RabbitMQ can be done easily—and Docker makes it even smoother.
In this article, we’ll walk through setting up RabbitMQ using Docker and configuring your Spring Boot application via
application.properties
. You’ll learn how to handle settings for both development and production environments, using Spring profiles and Kubernetes ConfigMaps.
Why Use Docker for RabbitMQ?
Manually installing RabbitMQ and its dependencies can be time-consuming and inconsistent across environments. Docker solves this by offering:
- Quick containerized setup
- No need for local installations
- Easy reset and removal of instances
- Consistent behavior across all systems
Step 1: Run RabbitMQ in Docker for Development
The fastest way to get RabbitMQ running locally is with the official Docker image. You can also use the management version for easy access to the web dashboard:
docker run -d --name rabbitmq-dev \
-p 5672:5672 -p 15672:15672 \
rabbitmq:management
This exposes RabbitMQ’s message port (5672) and web UI (15672). Visit
http://localhost:15672
in your browser and log in using the default credentials:
- Username:
guest
- Password:
guest
Step 2: Add RabbitMQ Dependency to Spring Boot
To connect your Spring Boot application to RabbitMQ, add this dependency to your
pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
This brings in Spring AMQP support, enabling you to send and receive messages using RabbitMQ.
Step 3: Configure application-dev.properties
for Local Docker
Now that your Docker container is running, configure your Spring Boot application to connect to it using a dedicated
application-dev.properties
file:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
These values correspond to the default settings of the RabbitMQ Docker container.
To activate this configuration profile in development, use the following environment variable when running your app:
SPRING_PROFILES_ACTIVE=dev
Spring Boot will automatically load the
application-dev.properties
file.
Step 4: Prepare RabbitMQ Configuration for Production (Kubernetes)
In a production Kubernetes environment, you typically won’t connect to RabbitMQ on
localhost
. Instead, you’ll point to a RabbitMQ service running in the cluster or managed externally. It’s also important not to hardcode credentials or connection settings. Instead, use environment variables passed in from a ConfigMap or Secret.
Step 4.1: Create a Kubernetes ConfigMap
Define a ConfigMap with RabbitMQ connection details:
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config
namespace: your-namespace
data:
rabbitmq_host: rabbitmq-service
rabbitmq_port: "5672"
rabbitmq_username: your-username
rabbitmq_password: your-password
For sensitive data like passwords, consider using a Secret instead of a ConfigMap.
Step 4.2: Inject Environment Variables in Deployment YAML
Your application deployment should reference the ConfigMap and inject the values as environment variables:
containers:
- name: your-app
image: your-app-image
env:
- name: RABBITMQ_HOST
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: rabbitmq_host
- name: RABBITMQ_PORT
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: rabbitmq_port
- name: RABBITMQ_USERNAME
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: rabbitmq_username
- name: RABBITMQ_PASSWORD
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: rabbitmq_password
- name: SPRING_PROFILES_ACTIVE
value: prod
This setup keeps your application secure and adaptable across environments.
Step 5: Configure application-prod.properties
Now create
application-prod.properties
to read values from the environment:
spring.rabbitmq.host=${RABBITMQ_HOST}
spring.rabbitmq.port=${RABBITMQ_PORT}
spring.rabbitmq.username=${RABBITMQ_USERNAME}
spring.rabbitmq.password=${RABBITMQ_PASSWORD}
Spring Boot will automatically resolve these values using the environment variables injected by Kubernetes.
Step 6: Using Spring Profiles
Spring Boot supports profiles to separate configuration for different environments. Here's how to organize it:
application-dev.properties
: for local development
application-prod.properties
: for production in Kubernetes
Activate the desired profile by setting the
SPRING_PROFILES_ACTIVE
environment variable.
Examples:
- Local:
SPRING_PROFILES_ACTIVE=dev
- Kubernetes: Defined in the deployment YAML as shown earlier
image quote pre code