MQTT Broker 配置
MQTT Broker 配置
本节介绍如何在 Node-RED 中配置 MQTT Broker 连接。学习完成后,您将能够:
- 在 Node-RED 中添加和配置 MQTT Broker
- 配置连接安全选项(用户名/密码)
- 管理多个 Broker 连接
- 排查常见的 MQTT 连接问题
MQTT Broker Configuration
Section titled “MQTT Broker Configuration”添加新 Broker
Section titled “添加新 Broker”在 MQTT 节点配置中:1. 双击 MQTT 输入/输出节点2. Server → Add new mqtt-broker3. 填写连接信息4. 验证连接状态| 参数 | 默认值 | 说明 |
|---|---|---|
| Server | (必填) | Broker 主机地址 |
| Port | 1883 | MQTT 端口 (8883 for TLS) |
| Protocol | mqtt:// | mqtt:// 或 mqtts:// |
| Client ID | 自动生成 | 唯一标识 (建议自定义) |
| Username | (可选) | 认证用户名 |
| Password | (可选) | 认证密码 |
| Keep Alive | 60s | 心跳间隔 |
| Clean Session | true | 是否清除会话 |
| TLS | (可选) | SSL/TLS 配置 |
| Topic Prefix | (可选) | 主题前缀过滤 |
Configuration Examples
Section titled “Configuration Examples”1. 本地 Broker (无认证)
Section titled “1. 本地 Broker (无认证)”{ "broker": "localhost", "port": "1883", "clientid": "nodered_local", "protocol": "mqtt://"}2. 远程 Broker (密码认证)
Section titled “2. 远程 Broker (密码认证)”{ "broker": "192.168.1.100", "port": "1883", "clientid": "nodered_production", "protocol": "mqtt://", "credentials": { "user": "iot_user", "password": "StrongPassword2024!" }}3. Broker with TLS
Section titled “3. Broker with TLS”{ "broker": "mqtt.example.com", "port": "8883", "clientid": "nodered_tls", "protocol": "mqtts://", "credentials": { "user": "iot_user", "password": "SecurePassword123" }, "tls": { "ca": "-----BEGIN CERTIFICATE-----\n...", "cert": "-----BEGIN CERTIFICATE-----\n...", "key": "-----BEGIN PRIVATE KEY-----\n...", "servername": "mqtt.example.com", "verify": true }}Docker Compose Configuration
Section titled “Docker Compose Configuration”services: mosquitto: image: eclipse-mosquitto:2 container_name: mosquitto restart: unless-stopped ports: - "1883:1883" volumes: - ./mosquitto/config:/mosquitto/config - ./mosquitto/data:/mosquitto/data - ./mosquitto/log:/mosquitto/log
nodered: image: nodered/node-red:latest container_name: nodered restart: unless-stopped ports: - "1880:1880" volumes: - ./nodered/data:/data depends_on: - mosquittoMultiple Broker Management
Section titled “Multiple Broker Management”Node-RED 支持同时连接多个 MQTT Broker:
┌──────────────────────────────────────┐│ Node-RED │├──────────────────────────────────────┤│ Broker 1: [Local Mosquitto] ││ ├─ MQTT In: sensor/data ││ ├─ MQTT Out: device/control ││ ││ Broker 2: [Cloud EMQX] ││ ├─ MQTT In: cloud/commands ││ ├─ MQTT Out: cloud/telemetry ││ ││ Broker 3: [Customer Broker] ││ ├─ MQTT In: customer/alerts ││ └─ MQTT Out: customer/status │└──────────────────────────────────────┘配置多 Broker
Section titled “配置多 Broker”// Broker 1: 本地{ "id": "local-broker", "broker": "mosquitto", "port": "1883", "clientid": "nodered-local"}
// Broker 2: 云端{ "id": "cloud-broker", "broker": "cloud.emqx.io", "port": "1883", "clientid": "nodered-cloud", "credentials": { "user": "cloud_user", "password": "cloud_password" }}Troubleshooting Connection
Section titled “Troubleshooting Connection”连接状态指示
Section titled “连接状态指示”图标颜色说明:🟢 绿色: 已连接🔴 红色: 连接失败🟡 黄色: 正在连接⚪ 灰色: 未配置常见连接问题
Section titled “常见连接问题”问题 1: Connection refused
# 原因: Broker 未运行或端口错误
# 检查 Broker 状态docker ps | grep mosquitto
# 检查端口netstat -tlnp | grep 1883
# 检查防火墙sudo ufw statussudo ufw allow 1883问题 2: Not authorized
# 原因: 认证信息错误
# 检查用户名密码docker exec mosquitto cat /mosquitto/config/password.txt
# 检查配置文件docker exec mosquitto cat /mosquitto/config/mosquitto.conf | grep allow_anonymous问题 3: Connection timeout
# 原因: 网络不通
# 测试网络连通性ping broker-addresstelnet broker-address 1883
# 检查 DNS 解析nslookup broker-addressSecurity Configuration
Section titled “Security Configuration”# Mosquitto 密码文件配置docker exec -it mosquitto shmosquitto_passwd -c /mosquitto/config/password.txt iot_userexit
# Node-RED 配置# Server: broker-address# Port: 1883# Username: iot_user# Password: *****TLS 配置
Section titled “TLS 配置”# 生成自签名证书openssl req -new -x509 -days 365 \ -keyout mosquitto/certs/server.key \ -out mosquitto/certs/server.crt \ -subj "/CN=mqtt.example.com"
# Mosquitto 配置listener 8883cafile /mosquitto/certs/ca.crtcertfile /mosquitto/certs/server.crtkeyfile /mosquitto/certs/server.keytls_version tlsv1.2✅ 推荐做法:
- 为每个 Node-RED 实例设置唯一的 Client ID
- 生产环境始终启用密码认证
- 使用环境变量管理敏感信息
- 定期检查 Broker 连接状态
- 配置心跳 Keep Alive 参数
❌ 避免做法:
- 多个 Node-RED 实例使用相同 Client ID
- 生产环境使用匿名连接
- 在 Flow 中硬编码凭据
- 忽略 TLS 证书过期
- 不监控连接状态
Summary
Section titled “Summary”- Node-RED 支持配置多个 MQTT Broker 连接
- 支持匿名、密码、TLS 三种认证方式
- 连接状态指示器便于监控连接健康度
- Docker Compose 环境使用容器名连接
- 多 Broker 配置适合边云协同场景