0

我有:

  • 1 MongoDB
  • 2 Mqtt Mosca 经纪人
  • 2 NodeJS 应用程序都与 mqtt.js cient
  • 1个

Nginx loadbalance 和 proxy_pass 都将 http 和 mqtt 分别传递给 NodeJS 应用程序和 Mqtt Mosca Brokers,

所以我有:

mqtt Sensors -> nginx -> Mqtt Mosca Brokers“流”正常工作

浏览器 -> nginx -> NodeJS App“流程正常”

我无法做的是让 NodeJS 应用程序(mqtt.js 客户端部分)通过 nginx 连接到 Mqtt Mosca Brokers,类似于:

NodeJS (mqtt.js 客户端) -> nginx -> Mqtt Mosca Broker。

奇怪的是,如果我尝试:

NodeJS(mqtt.js 客户端)-> Mqtt Mosca Broker 流程​​正常工作

我有以下配置:

码头工人-compose.yml

     nginx:
      build: ./nginx
      links:
        - app1:app1
        - app2:app2
        - mqttbroker1:mqttbroker1
        - mqttbroker2:mqttbroker2
      ports:
        - "80:80"
        - "443:443"
        - "1883:18833"
    app1:
      build: ./node_app
      links:
        - mongo:mongo
        - mqttbroker1:mqttbroker1
        - mqttbroker2:mqttbroker2
      ports:
        - "3000"
      environment:
        - NODE_ENV=production
        - PORT=3000
    app2:
      build: ./node_app
      links:
        - mongo:mongo
        - mqttbroker1:mqttbroker1
        - mqttbroker2:mqttbroker2
      ports:
        - "3000"
      environment:
        - NODE_ENV=production
        - PORT=3000    
    mqttbroker1:
      build: ./node_broker
      links:
        - mongo:mongo
      ports:
        - "18831"
      environment:
        - NODE_ENV=production
    mqttbroker2:
      build: ./node_broker
      links:
        - mongo:mongo
      ports:
        - "18831"
      environment:
       - NODE_ENV=production
    mongo:
     container_name: mongo
     restart: always
     image: mongo
     volumes:
       - ./mongo/data:/data/db
     ports:
       - "27017:27017"

nginx.conf(摘录)

    stream {
      upstream mqtt_cluster{

        server mqttbroker1:18831 max_fails=3 fail_timeout=30s;
        server mqttbroker2:18831 max_fails=3 fail_timeout=30s;
      }

      server {
        listen 18833; #mqtt continer prot mapped on std port (1883:18833)
        proxy_pass mqtt_cluster;
      }
}

基本上,如果在我使用的 NodeJS 应用程序中:

const mqttClient = mqtt.connect('mqtt://mqttbroker1:18831', { clientId: clientId });

它有效,但如果我尝试:

const mqttClient = mqtt.connect('mqtt://nginx:18833', { clientId: clientId });

没有任何效果,有人可以帮忙吗?

4

1 回答 1

0

我找到了使用 docker-compose.yml 的解决方案

我使用了depends_onnetworks而不是链接

YML 文件是:

version: '3'
services:
  nginx:
    build: ./nginx
    depends_on:
      - app1
      - app2
      - mqttbroker1
      - mqttbroker2
    ports:
      - "80:80"
      - "443:443"
      - "1883:1883"
    networks:
      main:
        aliases:
          - proxy
  app1:
    build: ./node_app
    depends_on:
      - mongo
      - mqttbroker1
      - mqttbroker2
    ports:
      - "3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
    networks:
      main:
        aliases:
          - app1
  app2:
    build: ./node_app
    depends_on:
      - mongo
      - mqttbroker1
      - mqttbroker2
    ports:
      - "3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
    networks:
      main:
        aliases:
          - app2
  mqttbroker1:
    build: ./node_broker
    depends_on:
      - mongo
    ports:
      - "18831"
    environment:
      - NODE_ENV=production
    networks:
      main:
        aliases:
          - mqttbroker1
  mqttbroker2:
    build: ./node_broker
    depends_on:
      - mongo
    ports:
      - "18831"
    environment:
      - NODE_ENV=production
    networks:
      main:
        aliases:
          - mqttbroker2
  mongo:
    container_name: mongo
    restart: always
    image: mongo
    volumes:
      - ./mongo/data:/data/db
    ports:
      - "27017:27017"
    networks:
      main:
        aliases:
          - mongo
networks:
  main:

我使用它通过 mqtt 进行连接:

const mqttClient = mqtt.connect('mqtt://proxy', { clientId: clientId });
于 2018-05-30T22:28:08.847 回答