我有:
Nginx loadbalance和proxy_pass分别为http和mqtt,分别为NodeJS应用程序和Mqtt Mosca Brokers,
所以我有:
mqtt传感器 - > nginx - > Mqtt Mosca Brokers" flow"工作正常
浏览器 - > 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 });
没有任何作用,任何人都可以帮忙吗?
答案 0 :(得分:0)
我找到了一个使用docker-compose.yml的解决方案
而非链接我使用 depends_on 和网络
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 });