- Replies:
- 0
- Words:
- 8473

application.yml
files tailored for each environment.pom.xml
includes the necessary dependencies:
<dependencies>
<!-- Micronaut MQTT integration -->
<dependency>
<groupId>io.micronaut.mqtt</groupId>
<artifactId>micronaut-mqtt-v3</artifactId>
</>dependency>
<!-- Micronaut Kubernetes client -->
<dependency>
<groupId>io.micronaut.kubernetes</groupId>
<artifactId>micronaut-kubernetes-client</artifactId>
</dependency>
</dependencies>
These dependencies enable MQTT communication and Kubernetes integration within your Micronaut application.
docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
tcp://localhost:1883
ws://localhost:9001
application-dev.yml
application-dev.yml
in src/main/resources/
with the following content:
micronaut:
application:
name: mqtt-app
mqtt:
client:
server-uri: tcp://localhost:1883
client-id: dev-client
user-name: devuser
password: devpass
clean-session: true
automatic-reconnect: true
keep-alive-interval: 30
This configuration sets up the MQTT client to connect to the local broker with specified credentials and connection settings../mvnw mn:run -Dmicronaut.environments=dev
Micronaut will automatically use the application-dev.yml
file for configuration.
apiVersion: v1
kind: Secret
metadata:
name: mqtt-secret
type: Opaque
data:
MQTT_USERNAME: cHJvZHVzZXI= # 'produser' base64 encoded
MQTT_PASSWORD: cHJvZHBhc3M= # 'prodpass' base64 encoded
apiVersion: v1
kind: ConfigMap
metadata:
name: mqtt-config
data:
MQTT_SERVER_URI: tcp://mqtt-broker:1883
These Kubernetes resources store your MQTT configuration securely and make them accessible to your application.
application-prod.yml
application-prod.yml
in src/main/resources/
with the following content:
micronaut:
application:
name: mqtt-app
mqtt:
client:
server-uri: ${MQTT_SERVER_URI}
client-id: prod-client
user-name: ${MQTT_USERNAME}
password: ${MQTT_PASSWORD}
clean-session: false
automatic-reconnect: true
keep-alive-interval: 60
This configuration uses environment variables provided by Kubernetes to set up the MQTT client for production.
env:
- name: MICRONAUT_ENVIRONMENTS
value: prod
- name: MQTT_SERVER_URI
valueFrom:
configMapKeyRef:
name: mqtt-config
key: MQTT_SERVER_URI
- name: MQTT_USERNAME
valueFrom:
secretKeyRef:
name: mqtt-secret
key: MQTT_USERNAME
- name: MQTT_PASSWORD
valueFrom:
secretKeyRef:
name: mqtt-secret
key: MQTT_PASSWORD
This setup ensures that your application retrieves the necessary configuration from Kubernetes resources during deployment.application-dev.yml
and application-prod.yml
, you can maintain clean and secure environments for development and production. Utilizing Docker for local development and Kubernetes for production allows for scalable and manageable deployments.This approach provides a robust and flexible setup for integrating MQTT into your Micronaut applications across different environments.
image quote pre code