#1
MQTT is one of the most reliable messaging protocols for IoT and event-driven applications. It’s lightweight, efficient, and works perfectly with low-bandwidth or high-latency networks. Whether you’re building smart home apps or real-time dashboards, MQTT is a solid choice.
In this guide, we’ll show you how to integrate MQTT into a Spring Boot application using Docker. We’ll use application.yml for configuration and demonstrate setups for both development and production environments. The production setup includes best practices for Kubernetes, using environment variables from ConfigMaps.

Why MQTT with Docker?

Running an MQTT broker using Docker is fast, isolated, and repeatable. It’s great for both local development and staging environments. Docker ensures your setup works the same way every time—regardless of your operating system.
We’ll focus on the Eclipse Mosquitto image, one of the most commonly used and actively maintained MQTT brokers.

Step 1: Running Mosquitto with Docker for Development

Let’s start by spinning up a local MQTT broker using Docker:
docker run -d --name mqtt-dev -p 1883:1883 eclipse-mosquitto
This command pulls and runs the Mosquitto broker, exposing port 1883, which is the default for MQTT.

Step 2: Add MQTT Dependency to Your Spring Boot Project

To enable MQTT communication in Spring Boot, add the Eclipse Paho client library:
<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.5</version>
</dependency>
This allows your application to publish and subscribe to MQTT topics.

Step 3: Define MQTT Config in application-dev.yml

For your development environment, define your MQTT broker settings like this:
mqtt:
  url: tcp://localhost:1883
  username:
  password:
Since the Mosquitto Docker image doesn’t require authentication by default, username and password can be left blank for now.
To activate the dev profile locally, run your app with:
SPRING_PROFILES_ACTIVE=dev
This tells Spring Boot to load settings from application-dev.yml.

Step 4: Setting Up MQTT for Production (Kubernetes)

For production environments—especially when deploying to Kubernetes—you’ll want to avoid hardcoding sensitive data. Instead, use environment variables, which are often sourced from Kubernetes ConfigMaps and Secrets.

Step 4.1: Create a ConfigMap in Kubernetes

Define the necessary MQTT values in a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: mqtt-config
  namespace: your-namespace
data:
  mqtt_url: tcp://mqtt-service:1883
  mqtt_username: your-mqtt-username
  mqtt_password: your-mqtt-password
This securely holds the configuration values for your broker.

Step 4.2: Pass ConfigMap Values to the App Container

Update your deployment YAML to inject environment variables:
containers:
  - name: your-app
    image: your-app-image
    env:
      - name: MQTT_URL
        valueFrom:
          configMapKeyRef:
            name: mqtt-config
            key: mqtt_url
      - name: MQTT_USERNAME
        valueFrom:
          configMapKeyRef:
            name: mqtt-config
            key: mqtt_username
      - name: MQTT_PASSWORD
        valueFrom:
          configMapKeyRef:
            name: mqtt-config
            key: mqtt_password
      - name: SPRING_PROFILES_ACTIVE
        value: prod
This setup ensures your app will access the right environment-specific values when running inside Kubernetes.

Step 5: Configure application-prod.yml

Your production application.yml should be ready to read these injected values:
mqtt:
  url: ${MQTT_URL}
  username: ${MQTT_USERNAME}
  password: ${MQTT_PASSWORD}
Spring Boot will automatically substitute the placeholders with the environment variable values provided by Kubernetes.

Step 6: Profile-Based Environment Separation

To manage different settings for development and production, use Spring Boot profiles. You can maintain multiple application-{profile}.yml files:
  • application-dev.yml for local development
  • application-prod.yml for Kubernetes or cloud deployments
Switching between them is as simple as setting the SPRING_PROFILES_ACTIVE variable.

image quote pre code