When building real-time applications or IoT services, integrating MQTT into your Dropwizard app can simplify messaging workflows. Let’s walk through how to configure MQTT in Dropwizard—using Docker for development and Kubernetes for production. We’ll touch on project setup, Docker setup, Dropwizard config files (
config-dev.yml
and
config-prod.yml
), and the environment-variable approach you’ll need in Kubernetes. Let’s dive in!
1. Add Required Dependencies
Start by updating your
pom.xml
to include essential libraries:
<dependencies>
<!-- Dropwizard core -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- Eclipse Paho MQTT client -->
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>
<!-- Kubernetes client for env var integration -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This gives you the MQTT client, Dropwizard framework, and optional Kubernetes support if you need more advanced interaction later.
2. Get MQTT Broker Running with Docker
For development, a simple Mosquitto container works great. Create a
docker-compose.yml
like this:
version: '3.7'
services:
mosquitto:
image: eclipse-mosquitto
ports:
- "1883:1883"
- "9001:9001"
Start it with:
docker-compose up
It exposes MQTT on port
1883
and WebSockets on
9001
. No credentials here—just free-flow messaging while you prototype.
3. Configure Dropwizard for Development
Create a
config-dev.yml
file:
server:
applicationConnectors:
- type: http
port: 8080
mqtt:
brokerUrl: tcp://localhost:1883
clientId: dev-client
username: ""
password: ""
topic: test-topic
You'll also need a config class to load these values:
public class MqttConfig {
@JsonProperty
public String brokerUrl;
@JsonProperty
public String clientId;
@JsonProperty
public String username;
@JsonProperty
public String password;
@JsonProperty
public String topic;
}
And then include it in your Dropwizard config:
public class MyAppConfig extends Configuration {
@JsonProperty
public MqttConfig mqtt = new MqttConfig();
}
Within your application, use something like this to connect and publish:
MqttClient client = new MqttClient(
config.mqtt.brokerUrl,
config.mqtt.clientId);
client.connect();
client.publish(config.mqtt.topic,
new MqttMessage("Hello, MQTT!".getBytes()));
Launch locally with:
java -jar target/myapp.jar server config-dev.yml
And’ve fun testing your MQTT setup on
tcp://localhost:1883
.
4. Prepare for Kubernetes Production
You’ll want to move MQTT credentials and broker endpoint into Kubernetes environment variables. This keeps sensitive information out of your code.
Generate Kubernetes resources:>
kubectl create secret generic mqtt-secret \
--from-literal=MQTT_USERNAME=produser \
--from-literal=MQTT_PASSWORD=prodpass
kubectl create configmap mqtt-config \
--from-literal=MQTT_BROKER_URL=tcp://mqtt-service:1883 \
--from-literal=MQTT_TOPIC=prod-topic
5. Create Production Config File: config-prod.yml
This file will reference environment variables:
server:
applicationConnectors:
- type: http
port: 8080
mqtt:
brokerUrl: ${MQTT_BROKER_URL}
clientId: prod-client
username: ${MQTT_USERNAME}
password: ${MQTT_PASSWORD}
topic: ${MQTT_TOPIC}
Running the app in Kubernetes, Dropwizard substitutes these environment variable values for real config.
6. Kubernetes Deployment Snippet
Make sure your Deployment includes these env vars:
env:
- name: MQTT_BROKER_URL
valueFrom:
configMapKeyRef:
name: mqtt-config
key: MQTT_BROKER_URL
- name: MQTT_TOPIC
valueFrom:
configMapKeyRef:
name: mqtt-config
key: MQTT_TOPIC
- name: MQTT_USERNAME
valueFrom:
secretKeyRef:
name: mqtt-secret
key: MQTT_USERNAME
- name: MQTT_PASSWORD
valueFrom:
secretKeyRef:
name: mqtt-secret
key: MQTT_PASSWORD
Your container command would reference the prod config:
java -jar myapp.jar server config-prod.yml
7. Why This Setup Works
- Dev simplicity: Local Docker + YAML config = fast feedback loop.
- Secure prod: No secrets in repos. Env vars from Kubernetes = safe and flexible.
- Easy separation: Environment-specific YAML keeps dev/prod cleanly separated.
- Scalable: Works great for a single broker today, and can be extended to MQTT clusters tomorrow.
By using Docker and Kubernetes for environment setup, and Dropwizard's YAML-based configuration, you create a robust, maintainable, and secure MQTT integration. Whether you're building IoT services or pushing live events, this setup gives you a solid foundation that scales from local testing to production deployment.
Your Dropwizard service can now send or receive MQTT messages confidently—whatever the environment, your configuration stays clear, clean, and under control.
image quote pre code