MQTT mosquitto docker 与 python docker [已关闭]

问题描述 投票:0回答:0

我正在尝试简单地将经纪人与客户端连接起来。我收到 ConnectionRefusedError: [Errno 111] 连接被拒绝。

我的文件是什么样的:

docker-compose.yml

version: "3.9"
services:
  brkr:
    build: ./brkr
    ports:
     - "1883:1883"
  clnt:
    build: ./clnt
    depends_on:
     - brkr

mosquitto.conf

#stream1\nconnection stream1\naddress stream1.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream1/GPB_LOCR in 2\nbridge_identity stream1\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream6\nconnection stream6\naddress stream6.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream6/GPB_LOCR in 2\nbridge_identity stream6\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream9\nconnection stream9\naddress stream9.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream9/GPB_LOCR in 2\nbridge_identity stream9\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream13\nconnection stream13\naddress stream13.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream13/GPB_LOCR in 2\nbridge_identity stream13\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream15\nconnection stream15\naddress stream15.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream15/GPB_LOCR in 2\nbridge_identity stream15\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream22\nconnection stream22\naddress stream22.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream22/GPB_LOCR in 2\nbridge_identity stream22\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream29\nconnection stream29\naddress stream29.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream29/GPB_LOCR in 2\nbridge_identity stream29\nbridge_psk rrrreally_long_maskded_psk\nbridge_tls_version tlsv1\n\n#stream5\nconnection stream5\naddress stream5.venue.link.com:8883\ntopic 1.0.0/LOC/SPOT/stream5/GPB_LOCR in 2\nbridge_identity stream5\nbridge_psk 

经纪商

FROM eclipse-mosquitto:2.0.15

WORKDIR /app

COPY .  .
# now reading mosquitto.conf as a variable, making change and saving it into mosquitto conf (it is on the platform)
RUN echo "${mosquitto//'\n'/$'\n'}" > mosquitto.conf

#mosquitto.conf contains url where to connect
ENTRYPOINT \[ "mosquitto" , "-c","./mosquitto.conf" \]\`

客户

FROM python:3.8-slim-buster

ENV DEBIAN_FRONTEND="noninteractive"
ENV PYTHONUNBUFFERED=1
ENV PYTHONIOENCODING=UTF-8

WORKDIR /app
COPY . .

RUN pip install  --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir --upgrade pip \
&& pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir -r requirements.txt

ENTRYPOINT \["python3", "test.py"\]

测试.py

import logging
import paho.mqtt.client as mqtt
import os

logging_level = 20
logging.basicConfig(level=logging_level, format='%(asctime)s %(name)s %(levelname)s:%(message)s')
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

list_of_mqtt_topics = "from the mosquitto file"

# Create an MQTT client
client = mqtt.Client("", True, None, mqtt.MQTTv31)

# MQTT broker connection callback

def on_connect(client, userdata, flags, rc):
  if rc == 0:
    logging.info("Connected to MQTT broker!")
    client.subscribe(list_of_mqtt_topics, qos=1)
    logger.info("connection established and subscribed to topics")
  else:
    logging.error(f"Failed to connect, return code: {rc}")

# print message, useful for checking if it was successful

def on_message(client, userdata, msg):
  logger.debug(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
  logger.info(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
# handle the message in the relevant function

# print which topic was subscribed to

def on_subscribe(client, userdata, mid, granted_qos, properties=None):
  logger.debug("Subscribed: " + str(mid) + " " + str(granted_qos))

# Set up logging

# Set the connection callback

client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message

# Connect to the MQTT broker

mqtt_port = 1883
mqtt_url = "brkr"

client.connect(mqtt_url, mqtt_port, 60)

# Start the MQTT client loop

client.loop_start()

# Wait for the connection to be established

client.loop_stop()

出了什么问题?

ConnectionRefusedError: \[Errno 111\] Connection refused. 

经纪商启动,但客户端没有启动。

我尝试了 2 个独立的 docker,但我会遇到防火墙问题。我尝试了多层 docker 文件,因为 mosquitto docker 是高山类型,不支持我需要的库。

无法从 Python Docker 连接到 mqtt Docker 类似,但它确实解决了 docker 名称的问题。

docker mqtt mosquitto paho
© www.soinside.com 2019 - 2024. All rights reserved.