在 Docker 秒内通过 Nginx 将 mqtt.js 客户端连接到 Mosca Broker(相同 Docker-compose)
connect mqtt.js client to Mosca Broker through Nginx within Dockers (same Docker-compose)
我有:
- 1 MongoDB
- 2 Mqtt Mosca 经纪人
- 2 个 NodeJS 应用程序都具有 mqtt.js cient
- 1 ngnix
Nginx 负载平衡和 proxy_pass http 和 mqtt,分别用于 NodeJS 应用程序和 Mqtt Mosca 代理,
所以我有:
mqtt 传感器 -> nginx -> Mqtt Mosca 代理 "flow" 正常工作
浏览器 -> nginx -> NodeJS 应用 "flow working properly"
我无法做的是让 NodeJS 应用程序(mqtt.js 客户端部分)通过 nginx 连接到 Mqtt Mosca Brokers,例如:
NodeJS(mqtt.js 客户端)-> nginx -> Mqtt Mosca Broker。
奇怪的是,如果我尝试:
NodeJS(mqtt.js 客户端)-> Mqtt Mosca Broker 流程正常
我有以下配置:
docker-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 (excerpt)
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 });
没有任何效果,有人可以帮忙吗?
我找到了使用 docker-compose.yml
的解决方案
而不是 link 我使用了 depends_on 和 networks
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 });
我有:
- 1 MongoDB
- 2 Mqtt Mosca 经纪人
- 2 个 NodeJS 应用程序都具有 mqtt.js cient
- 1 ngnix
Nginx 负载平衡和 proxy_pass http 和 mqtt,分别用于 NodeJS 应用程序和 Mqtt Mosca 代理,
所以我有:
mqtt 传感器 -> nginx -> Mqtt Mosca 代理 "flow" 正常工作
浏览器 -> nginx -> NodeJS 应用 "flow working properly"
我无法做的是让 NodeJS 应用程序(mqtt.js 客户端部分)通过 nginx 连接到 Mqtt Mosca Brokers,例如:
NodeJS(mqtt.js 客户端)-> nginx -> Mqtt Mosca Broker。
奇怪的是,如果我尝试:
NodeJS(mqtt.js 客户端)-> Mqtt Mosca Broker 流程正常
我有以下配置:
docker-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 (excerpt)
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 });
没有任何效果,有人可以帮忙吗?
我找到了使用 docker-compose.yml
的解决方案而不是 link 我使用了 depends_on 和 networks
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 });