#1
MQTT is a lightweight messaging protocol perfect for IoT, sensor networks, and real-time applications. If your Spring Boot app needs to send or receive messages using MQTT, setting up an MQTT broker like Eclipse Mosquitto via Docker is a smart move. It's fast, isolated, and works seamlessly in both development and production environments.
In this guide, we'll walk you through how to configure your Spring Boot app to use an MQTT broker running in Docker. We'll use application.properties files for configuration, showing separate setups for local development and Kubernetes-based production environments.

What You’ll Need to Configure

To connect to an MQTT broker from Spring Boot, you typically need three things:
  • url: The broker's address (including the port)
  • username: Your MQTT username (if authentication is enabled)
  • password: Your MQTT password
In development, these values can be hardcoded in your properties file. In production, it’s best practice to inject them as environment variables via Kubernetes ConfigMaps and Secrets.

Step 1: Run MQTT with Docker Locally (Dev Setup)

We’ll use the Eclipse Mosquitto image, which is lightweight and easy to run.
docker run -d --name mosquitto-dev -p 1883:1883 eclipse-mosquitto
This command launches Mosquitto and exposes port 1883, the default MQTT port. You now have a working broker locally!

Step 2: Add MQTT Dependency to Spring Boot

Add the MQTT client library (like Eclipse Paho) to your pom.xml:
<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.5</version>
</dependency>
This allows your Spring Boot app to connect and interact with the MQTT broker.

Step 3: Configure application.properties for Dev

In your src/main/resources/application-dev.properties, define:
mqtt.url=tcp://localhost:1883
mqtt.username=
mqtt.password=
No authentication is enabled by default in Mosquitto’s Docker image, but you can update the settings later if needed. This simple config lets Spring Boot connect during development.
Make sure you activate the profile with:
SPRING_PROFILES_ACTIVE=dev

Step 4: Configure for Production Using Kubernetes

In production, it's risky to hardcode secrets or environment-specific values. Instead, inject MQTT configuration via environment variables using a Kubernetes ConfigMap (or Secret for passwords).

Step 4.1: Create the ConfigMap

Create a mqtt-config ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: mqtt-config
  namespace: your-namespace
data:
  mqtt_url: tcp://mqtt-service:1883
  mqtt_username: your-username
  mqtt_password: your-password
This stores the MQTT broker details safely within Kubernetes.

Step 4.2: Inject Values into the App Deployment

In your deployment YAML, pass these values into your Spring Boot container:
containers:
  - name: your-app
    image: your-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 ensures your app reads the correct MQTT details from Kubernetes when running in production.

Step 5: Configure application-prod.properties for Prod

In your application-prod.properties:
mqtt.url=${MQTT_URL}
mqtt.username=${MQTT_USERNAME}
mqtt.password=${MQTT_PASSWORD}
Spring Boot will automatically replace these placeholders with the environment variable values injected by Kubernetes.

Step 6: Using Profiles to Separate Environments

To keep things clean and modular:
  • Use application-dev.properties during local development
  • Use application-prod.properties in production
  • Activate the correct profile using the SPRING_PROFILES_ACTIVE environment variable
This lets you switch contexts without changing your code or main configuration files.

image quote pre code