Dropwizard is great for building Java APIs, and integrating RabbitMQ can elevate your app’s messaging and event-handling capabilities. This guide explores how to configure RabbitMQ in a Dropwizard project—using Docker locally for development and Kubernetes for production deployments—while maintaining clear separation between environments through profile-based configuration.
1. Adding Dependencies to pom.xml
Include the following in your Maven project:
<dependencies>
<!-- Dropwizard framework -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- RabbitMQ Java client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>${rabbitmq.version}</version>
</dependency>
<!-- Kubernetes client library (optional for extended interactions) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This ensures your project includes Dropwizard, RabbitMQ for message brokering, and optional Kubernetes support.
2. Run RabbitMQ with Docker for Development
Create a
docker-compose.yml
for local testing:
version: '3'
services:
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
Then start it with:
docker-compose up -d
RabbitMQ will run locally on
localhost:5672
for AMQP and
localhost:15672
for its web management console.
3. Dev Configuration: config-dev.yml
Define the RabbitMQ settings for your development environment:
server:
applicationConnectors:
- type: http
port: 8080
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtualHost: /
queue: dev-queue
Create a matching configuration class:
public class RabbitConfig {
@JsonProperty public String host;
@JsonProperty public int port;
@JsonProperty public String username;
@JsonProperty public String password;
@JsonProperty public String virtualHost;
@JsonProperty public String queue;
}
public class AppConfig extends Configuration {
@JsonProperty("rabbitmq")
public RabbitConfig rabbitmq;
}
Initialize RabbitMQ connection in your application class:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(config.rabbitmq.host);
factory.setPort(config.rabbitmq.port);
factory.setUsername(config.rabbitmq.username);
factory.setPassword(config.rabbitmq.password);
factory.setVirtualHost(config.rabbitmq.virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(config.rabbitmq.queue, true, false, false, null);
Run your app with:
java -jar target/myapp.jar server config-dev.yml
Test by publishing or consuming messages manually or via code.
4. Prepare for Kubernetes Production
Use ConfigMaps and Secrets to inject configuration securely in production:
kubectl create configmap rabbitmq-config \
--from-literal=RABBIT_HOST=rabbitmq-service \
--from-literal=RABBIT_PORT=5672 \
--from-literal=RABBIT_VHOST=/ \
--from-literal=RABBIT_QUEUE=prod-queue
kubectl create secret generic rabbitmq-secret \
--from-literal=RABBIT_USERNAME=produser \
--from-literal=RABBIT_PASSWORD=prodpass
5. Production Configuration: config-prod.yml
Tell Dropwizard to use environment variables in production:
server:
applicationConnectors:
- type: http
port: 8080
rabbitmq:
host: ${RABBIT_HOST}
port: ${RABBIT_PORT}
username: ${RABBIT_USERNAME}
password: ${RABBIT_PASSWORD}
virtualHost: ${RABBIT_VHOST}
queue: ${RABBIT_QUEUE}
6. Kubernetes Deployment Snippet
Add the environment variables to your deployment YAML:
env:
- name: RABBIT_HOST
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: RABBIT_HOST
- name: RABBIT_PORT
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: RABBIT_PORT
- name: RABBIT_VHOST
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: RABBIT_VHOST
- name: RABBIT_QUEUE
valueFrom:
configMapKeyRef:
name: rabbitmq-config
key: RABBIT_QUEUE
- name: RABBIT_USERNAME
valueFrom:
secretKeyRef:
name: rabbitmq-secret
key: RABBIT_USERNAME
- name: RABBIT_PASSWORD
valueFrom:
secretKeyRef:
name: rabbitmq-secret
key: RABBIT_PASSWORD
Run your service with:
java -jar myapp.jar server config-prod.yml
7. Why This Approach Works
- Fast feedback locally using Docker + dev config
- Production safety, with secrets managed externally via Kubernetes
- Clear environment separation, keeping both configs easy to maintain
- Simple real-world messaging, ready to scale out with RabbitMQ
Using Docker for local development and Kubernetes for production, along with Dropwizard’s flexible configuration, enables you to build robust, secure, and maintainable messaging-powered applications. Your Dropwizard service can now send and receive messages with RabbitMQ confidently—no matter the environment, configuration stays clear, scalable, and under control.
image quote pre code