我有:
- 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 });
没有任何效果,有人可以帮忙吗?