Repository navigation
Expand file tree
/
Copy pathedgeos-quickstart.md
More file actions
386 lines (319 loc) · 7.18 KB
/
Copy pathedgeos-quickstart.md
File metadata and controls
386 lines (319 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
---
layout: default
---
# edgeOS 北向通道快速开始指南
## 1. 环境准备
### 1.1 启动 MQTT Broker
使用 Mosquitto:
```bash
docker run -d \
--name mosquitto \
-p 1883:1883 \
-p 9001:9001 \
eclipse-mosquitto
```
### 1.2 启动 NATS Server
使用普通模式:
```bash
docker run -d \
--name nats \
-p 4222:4222 \
-p 8222:8222 \
nats
```
使用 JetStream 模式(推荐):
```bash
docker run -d \
--name nats-js \
-p 4222:4222 \
-p 8222:8222 \
nats -js
```
## 2. 配置 edgeOS 北向通道
### 2.1 通过 Web UI 配置
1. 打开 EdgeX 网关 Web UI: `http://localhost:8082`
2. 导航到"北向通道"页面
3. 点击"新增 edgeOS(MQTT)"或"新增 edgeOS(NATS)"
4. 填写配置信息:
- **edgeOS(MQTT)**:
- 名称: edgeOS MQTT Channel
- Broker 地址: tcp://127.0.0.1:1883
- Client ID: edgex-node-001
- 节点 ID: edgex-node-001
- QoS: 1
- 心跳周期: 30s
- **edgeOS(NATS)**:
- 名称: edgeOS NATS Channel
- URL: nats://127.0.0.1:4222
- Client ID: edgex-node-001
- 节点 ID: edgex-node-001
- JetStream: 启用
5. 点击"保存"
### 2.2 通过 API 配置
**创建 edgeOS(MQTT):**
```bash
curl -X POST http://localhost:8082/api/northbound/edgeos-mqtt \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"id": "edgeos-mqtt-1",
"name": "edgeOS MQTT Channel",
"enable": true,
"broker": "tcp://127.0.0.1:1883",
"client_id": "edgex-node-001",
"node_id": "edgex-node-001",
"qos": 1,
"keep_alive": 60,
"heartbeat_interval": "30s"
}
EOF
```
**创建 edgeOS(NATS):**
```bash
curl -X POST http://localhost:8082/api/northbound/edgeos-nats \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"id": "edgeos-nats-1",
"name": "edgeOS NATS Channel",
"enable": true,
"url": "nats://127.0.0.1:4222",
"client_id": "edgex-node-001",
"node_id": "edgex-node-001",
"jetstream_enabled": true,
"heartbeat_interval": "30s"
}
EOF
```
## 3. 验证连接
### 3.1 检查通道状态
```bash
curl http://localhost:8082/api/northbound/config
```
响应示例:
```json
{
"success": true,
"data": {
"edgeos_mqtt": [
{
"id": "edgeos-mqtt-1",
"name": "edgeOS MQTT Channel",
"enable": true,
"status": 1
}
],
"edgeos_nats": [
{
"id": "edgeos-nats-1",
"name": "edgeOS NATS Channel",
"enable": true,
"status": 1
}
]
}
}
```
状态码说明:
- `0`: 已断开
- `1`: 已连接
- `2`: 重连中
- `3`: 错误
### 3.2 查看统计信息
**edgeOS(MQTT):**
```bash
curl http://localhost:8082/api/northbound/edgeos-mqtt/edgeos-mqtt-1/stats
```
**edgeOS(NATS):**
```bash
curl http://localhost:8082/api/northbound/edgeos-nats/edgeos-nats-1/stats
```
## 4. 消息测试
### 4.1 监听消息
**MQTT 监听:**
```bash
mosquitto_sub -h 127.0.0.1 -p 1883 -t "edgex/#" -v
```
**NATS 监听:**
```bash
nats sub "edgex.>"
```
### 4.2 发送命令
**发送设备发现命令 (MQTT):**
```bash
mosquitto_pub -h 127.0.0.1 -p 1883 \
-t "edgex/cmd/edgex-node-001/discover" \
-m '{
"header": {
"message_id": "test-disc-001",
"timestamp": 1744680000000,
"source": "edgeos-queen",
"message_type": "discover_command",
"version": "1.0"
},
"body": {
"protocol": "modbus-tcp",
"network": "192.168.1.0/24"
}
}'
```
**发送设备发现命令 (NATS):**
```bash
nats pub "edgex.cmd.edgex-node-001.discover" '{
"header": {
"message_id": "test-disc-001",
"timestamp": 1744680000000,
"source": "edgeos-queen",
"message_type": "discover_command",
"version": "1.0"
},
"body": {
"protocol": "modbus-tcp",
"network": "192.168.1.0/24"
}
}'
```
### 4.3 发送写入命令
**写入命令 (MQTT):**
```bash
mosquitto_pub -h 127.0.0.1 -p 1883 \
-t "edgex/cmd/edgex-node-001/device-001/write" \
-m '{
"header": {
"message_id": "test-write-001",
"timestamp": 1744680000000,
"source": "edgeos-queen",
"destination": "edgex-node-001",
"message_type": "write_command",
"version": "1.0",
"correlation_id": "req-write-001"
},
"body": {
"request_id": "req-write-001",
"device_id": "device-001",
"timestamp": 1744680000000,
"points": {
"Switch": true,
"Setpoint": 80.5
}
}
}'
```
**写入命令 (NATS):**
```bash
nats pub "edgex.cmd.edgex-node-001.device-001.write" '{
"header": {
"message_id": "test-write-001",
"timestamp": 1744680000000,
"source": "edgeos-queen",
"destination": "edgex-node-001",
"message_type": "write_command",
"version": "1.0",
"correlation_id": "req-write-001"
},
"body": {
"request_id": "req-write-001",
"device_id": "device-001",
"timestamp": 1744680000000,
"points": {
"Switch": true,
"Setpoint": 80.5
}
}
}'
```
## 5. 监控与调试
### 5.1 查看日志
```bash
# 查看所有日志
tail -f logs/edgex-gateway.edgex.log
# 只查看 edgeOS 相关日志
tail -f logs/edgex-gateway.edgex.log | grep edgeOS
```
### 5.2 查看连接状态
在 Web UI 的"北向通道"页面,可以看到每个通道的连接状态和统计信息。
### 5.3 常见问题
**Q: MQTT 连接失败?**
```bash
# 检查 Broker 是否运行
docker ps | grep mosquitto
# 检查端口是否可访问
telnet 127.0.0.1 1883
# 查看 MQTT Broker 日志
docker logs mosquitto
```
**Q: NATS 连接失败?**
```bash
# 检查 Server 是否运行
docker ps | grep nats
# 检查端口是否可访问
telnet 127.0.0.1 4222
# 查看 NATS 日志
docker logs nats
```
**Q: 消息未接收?**
- 检查 Topic/Subject 是否正确
- 确认通道已启用
- 查看设备映射配置
- 检查 QoS 级别设置
## 6. 性能调优
### 6.1 MQTT 调优
```yaml
# 降低心跳频率以减少网络负载
keep_alive: 120 # 秒
# 根据网络质量选择合适的 QoS
qos: 1 # 0: 最快, 1: 可靠, 2: 最可靠但最慢
# 调整重连间隔
reconnect_interval: 5 # 秒
```
### 6.2 NATS 调优
```yaml
# 启用 JetStream 以获得持久化
jetstream_enabled: true
# 调整 Ping 间隔
ping_interval: 30 # 秒
# 调整最大重连次数
max_reconnects: 10
```
## 7. 生产环境部署
### 7.1 安全配置
**MQTT:**
```yaml
username: "secure-user"
password: "strong-password"
# 或使用 TLS
# broker: "tls://broker.example.com:8883"
```
**NATS:**
```yaml
username: "secure-user"
password: "strong-password"
# 或使用 TLS
# url: "tls://nats.example.com:4222"
```
### 7.2 高可用配置
部署多个 edgeOS 节点,配置不同的节点 ID:
```yaml
# 节点 1
node_id: "edgex-node-001"
client_id: "edgex-node-001-mqtt"
# 节点 2
node_id: "edgex-node-002"
client_id: "edgex-node-002-mqtt"
```
### 7.3 监控告警
- 监控连接状态
- 监控消息发送/接收速率
- 监控重连次数
- 监控消息丢失率
- 设置阈值告警
## 8. 下一步
- 阅读完整文档: [edgeos-northbound.md](edgeos-northbound.html)
- 查看协议规范: [EdgeX-EdgeOS通信协议规范(MQTT-NATS).md](../doc/TODO/EdgeX-EdgeOS通信协议规范(MQTT-NATS).md)
- 集成到现有 edgeOS 蜂群网络
- 实现自定义消息处理逻辑
## 技术支持
如遇问题,请查看:
1. 日志文件: `logs/edgex-gateway.edgex.log`
2. 统计信息: `/api/northbound/config`
3. 帮助文档: Web UI 北向通道页面 → 帮助按钮