Home > Software engineering >  Connecting dockerized Spring boot app to MQTT Mosquitto broker
Connecting dockerized Spring boot app to MQTT Mosquitto broker

Time:07-21

hope someone can help me with this issue.

I have an app running with docker called "my-app". It connects to a MQTT Mosquitto broker which is also running in a docker container. Here is the docker-compose file:

  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto:2.0.14
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - "./mosquitto/Broker.conf:/mosquitto/config/mosquitto.conf"
      - "./mosquitto/Password:/mosquitto/config/mosquitto.passwd"

  my-app:
    container_name: my-app
    image: data-provider:latest
    ports:
      - 8080:8080
    depends_on:
      - mosquitto
    networks:
      - my-app-network

I have a configuration file for the broker with the following:

listener 1883
password_file /mosquitto/config/mosquitto.passwd
allow_anonymous false

Looking at the mosquitto container log files, I can see it starts without erros when running docker-compose.

1658337045: mosquitto version 2.0.14 starting
1658337045: Config loaded from /mosquitto/config/mosquitto.conf.
1658337045: Opening ipv4 listen socket on port 1883.
1658337045: mosquitto version 2.0.14 running

However, my Java app is unable to connect with the mosquitto broker. Whenever I try to send a message to be published, I receive the following error:

Caused by: org.springframework.messaging.MessagingException: Failed to connect; nested exception is MqttException (0) - java.net.UnknownHostException: mosquitto

The Java code is the following:

    @Value("${mqtt.url}")
    private String url;

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(new String[] { url });
        ...
        ...
        return factory;
    }

In the properties file:

mqtt.url = tcp://mosquitto:1883

If I try to run the app with an embedded tomcat server, and I change the url to localhost:1883, then everything will work fine:

1658338620: New connection from <ip>:59730 on port 1883.
1658338621: New client connected from <ip>:59730 as my-app.topic (p2, c1, k60, u'admin').

I think I'm not exposing the docker host container URL in the correct way, but I have tried a few configurations without success. Does anyone know what I'm doing wrong and what I need to change in my broker config?

PS: I'm using spring-integration-mqtt 5.5.10

Thanks in advance!

CodePudding user response:

Currently you mosquitto service is on the Bridge network and my-app is on the my-app-network. Hence they are unable to talk to each other. You need to bring both the services under one network.

mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto:2.0.14
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - "./mosquitto/Broker.conf:/mosquitto/config/mosquitto.conf"
      - "./mosquitto/Password:/mosquitto/config/mosquitto.passwd"
    networks:
      - my-app-network
  • Related