#1
RabbitMQ is a widely used message broker that supports reliable, asynchronous communication between microservices and applications. Whether you're building a task queue or a full-blown event-driven architecture, RabbitMQ integrates well with Spring Boot—and even better when paired with Docker.
This guide will walk you through setting up RabbitMQ using Docker and configuring it in a Spring Boot project using application.yml. We'll cover both development and production setups, with production running in a Kubernetes cluster using environment variables.

Step 1: Why Use Docker for RabbitMQ?

Using Docker for RabbitMQ gives you a portable, isolated, and repeatable setup. You don’t need to install RabbitMQ on your local machine, and you can mirror your dev environment to staging or production easily.

Step 2: Run RabbitMQ in Docker for Development

To run RabbitMQ locally, you can use the official image with the management plugin for easy web UI access:
docker run -d --name rabbitmq-dev 
\  -p 5672:5672 -p 15672:15672 
\  rabbitmq:management
  • Port 5672 is used for message communication.
  • Port 15672 gives access to the management dashboard via browser.
Once the container is up, access the UI at:
http://localhost:15672
Default credentials:
  • Username: guest
  • Password: guest

Step 3: Add RabbitMQ Dependency to Spring Boot

In your pom.xml, include the Spring Boot AMQP starter:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
This brings in everything needed to connect to RabbitMQ using Spring’s messaging abstraction.

Step 4: Configure application-dev.yml

For development, set up the application-dev.yml file to connect to your local RabbitMQ Docker container:
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
This configuration uses default settings that match the RabbitMQ Docker container we launched.
To run the app using this config, set:
SPRING_PROFILES_ACTIVE=dev
Spring Boot will pick up the application-dev.yml file.

Step 5: Set Up RabbitMQ for Production in Kubernetes

When deploying to Kubernetes, hardcoding sensitive values is a no-go. Instead, use ConfigMaps and environment variables to manage configuration externally.

Step 5.1: Create a ConfigMap for RabbitMQ Connection

Here’s an example ConfigMap for RabbitMQ details:
apiVersion: v1
kind: ConfigMap
metadata:
  name: rabbitmq-config
  namespace: your-namespace
data:
  rabbitmq_host: rabbitmq-service
  rabbitmq_port: "5672"
  rabbitmq_user: your-username
  rabbitmq_pass: your-password
Use a Secret for passwords in secure setups.

Step 5.2: Pass ConfigMap Values into the Deployment

Modify your Kubernetes deployment YAML to use these values:
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_user
      - name: RABBITMQ_PASSWORD
        valueFrom:
          configMapKeyRef:
            name: rabbitmq-config
            key: rabbitmq_pass
      - name: SPRING_PROFILES_ACTIVE
        value: prod
This setup injects all necessary values into your Spring Boot app as environment variables.

Step 6: Configure application-prod.yml

Create a production config file that uses placeholders to map environment variables:
spring:
  rabbitmq:
    host: ${RABBITMQ_HOST}
    port: ${RABBITMQ_PORT}
    username: ${RABBITMQ_USERNAME}
    password: ${RABBITMQ_PASSWORD}
This lets Spring Boot load the RabbitMQ settings dynamically from the environment—ideal for Kubernetes deployments.

Step 7: Switch Between Profiles

To use different configs for development and production, Spring Boot makes it simple with profiles:
  • Local:
    SPRING_PROFILES_ACTIVE=dev
  • Kubernetes:
    env:
      - name: SPRING_PROFILES_ACTIVE
        value: prod
Spring automatically selects the correct application-{profile}.yml file based on this value.

image quote pre code