简体   繁体   English

本地 MQTT mosquitto 实例连接 ECONNREFUSED 127.0.0.1:1883

[英]Local MQTT mosquitto instance getting connect ECONNREFUSED 127.0.0.1:1883

I am trying to run a local mosquitto broker, publisher and subscriber setup via docker and docker-compose, but the publisher cannot connect to the broker.我正在尝试通过 docker 和 docker-compose 运行本地 mosquitto 代理、发布者和订阅者设置,但发布者无法连接到代理。 However, connecting to local broker via cli works fine.但是,通过 cli 连接到本地代理可以正常工作。 Getting following error when running below setup.运行以下设置时出现以下错误。

{ Error: connect ECONNREFUSED 127.0.0.1:1883
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 1883 }

Local dockerized setup:本地 dockerized 设置:

docker-compose.yml : docker-compose.yml

version: "3.5"

services:
  publisher:
    hostname: publisher
    container_name: publisher
    build:
      context: ./
      dockerfile: dev.Dockerfile
    command: npm start
    networks:
      - default
    depends_on:
      - broker

  broker:
    image: eclipse-mosquitto
    hostname: mosquitto-broker
    container_name: mosquitto-broker
    networks:
      - default
    ports:
      - "1883:1883"

networks:
  default:

dev.Dockerfile : dev.Dockerfile

FROM node:11-alpine

RUN mkdir app
WORKDIR app

COPY package*.json ./

RUN npm ci

COPY ./src ./src

CMD npm start

src/index.js :源代码/索引.js

const mqtt = require("mqtt");

const client = mqtt.connect("mqtt://localhost:1883");

client.on("connect", () => {
  console.log("Start publishing...");
  client.publish("testTopic", "test");
});

client.on("error", (error) => {
  console.error(error);
});

However, if I connect to the mosquitto broker via mqtt-js cli, it works as expected.但是,如果我通过 mqtt-js cli 连接到 mosquitto 代理,它会按预期工作。 Eg mqtt sub -t 'testTopic' -h 'localhost' and mqtt pub -t 'testTopic' -h 'localhost' -m 'from MQTT.js' .例如mqtt sub -t 'testTopic' -h 'localhost'mqtt pub -t 'testTopic' -h 'localhost' -m 'from MQTT.js'

What am I missing?我错过了什么?

your publisher container and broker are running in two different containers that's mean that they are two different machines each machine has it's own ip.您的发布者容器和代理在两个不同的容器中运行,这意味着它们是两台不同的机器,每台机器都有自己的 IP。

you can't call broker service from your publisher container by using localhost:1883 and vice verse , from broker to publisher container您不能使用 localhost:1883 从您的发布者容器调用代理服务,反之亦然,从代理到发布者容器

To reach broker container you have to call container ip or name or service name要访问代理容器,您必须调用容器 ip 或名称或服务名称

in your case change mqtt.connect("mqtt://localhost:1883");在你的情况下改变mqtt.connect("mqtt://localhost:1883"); value to be mqtt.connect("mqtt://broker:1883");值为mqtt.connect("mqtt://broker:1883"); and give it a try试一试

in your index.js you should change "localhost" to "broker".在您的 index.js 中,您应该将“localhost”更改为“broker”。 When inside a container "localhost" will resolve to that specific container so you should always use the service name instead and docker will take care of the routing to that specific service.当在容器内时,“localhost”将解析为该特定容器,因此您应该始终使用服务名称,而 docker 将负责路由到该特定服务。 Also by default all service in the same compose file are added to the same network so there is no need to specify it.此外,默认情况下,同一撰写文件中的所有服务都添加到同一网络中,因此无需指定。

So basically change this: const client = mqtt.connect("mqtt://localhost:1883");所以基本上改变这个: const client = mqtt.connect("mqtt://localhost:1883");

To this: const client = mqtt.connect("mqtt://broker:1883");对此: const client = mqtt.connect("mqtt://broker:1883");

The publisher and broker run in different containers, meaning they have different IPs. publisherbroker运行在不同的容器中,这意味着它们具有不同的 IP。

When the publisher is trying to reach the broker at localhost:1883 , it is normal to receive a ECONNREFUSED , hence the broker is not in the same container.当发布者尝试在localhost:1883访问代理时,收到ECONNREFUSED是正常的,因此代理不在同一个容器中。

You should replace the 127.0.0.1 or localhost with the service name of the broker( broker in this case).此时应更换127.0.0.1localhost与经纪人(的服务名broker在这种情况下)。 The service name will be resolved to the correct IP of the broker container.服务名称将被解析为代理容器的正确 IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:在 Github Actions 中连接 ECONNREFUSED 127.0.0.1:5432 - Getting ERROR: connect ECONNREFUSED 127.0.0.1:5432 in Github Actions 在 Docker Compose 中获取“MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017” - Getting "MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017" in Docker Compose SequelizeConnectionRefusedError:连接ECONNREFUSED 127.0.0.1:4321 - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:4321 错误:连接ECONNREFUSED 127.0.0.1:8000 - Error: connect ECONNREFUSED 127.0.0.1:8000 使用 Localstack 和 Docker-Compose 运行本地集成测试给出: NetworkingError: connect ECONNREFUSED 127.0.0.1:3000 - Running local integration test with Localstack and Docker-Compose gives: NetworkingError: connect ECONNREFUSED 127.0.0.1:3000 Docker NodeJS 与 MySQL 连接 ECONNREFUSED 127.0.0.1:8000 - Docker NodeJS with MySQL connect ECONNREFUSED 127.0.0.1:8000 错误:连接ECONNREFUSED 127.0.0.1:3003 Docker容器 - Error: connect ECONNREFUSED 127.0.0.1:3003 Docker containers 在 docker 中使用 jest 连接 ECONNREFUSED 127.0.0.1:3306 - connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker Docker Redis 错误:连接 ECONNREFUSED 127.0.0.1:6379 - Docker Redis Error: connect ECONNREFUSED 127.0.0.1:6379 Docker - Redis 连接 ECONNREFUSED 127.0.0.1:6379 - Docker - Redis connect ECONNREFUSED 127.0.0.1:6379
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM