VWED_server/docs/async_mqtt_setup.md

76 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 异步MQTT服务安装指南
## 依赖安装
为了使用高性能的异步MQTT服务需要安装额外的依赖
```bash
pip install aiomqtt
```
## 配置说明
系统会自动检测是否安装了 `aiomqtt`
- **如果已安装 aiomqtt**系统将使用异步MQTT客户端提供更好的并发性能
- **如果未安装 aiomqtt**系统会回退到同步MQTT客户端但包装为异步接口
## 性能对比
### 异步MQTT服务优势
1. **高并发处理**:可以同时处理大量设备的消息
2. **非阻塞IO**:消息处理不会阻塞其他操作
3. **更好的资源利用**:减少线程开销
4. **自动重连**:内置连接管理和自动重连机制
### 适用场景:
- 大量设备同时连接(>50个设备
- 高频率消息传输(>100消息/秒)
- 对延迟敏感的应用
- 需要可靠连接管理的场景
## 使用方法
系统会自动选择最佳的MQTT服务
```python
# 设备处理服务会自动使用异步MQTT如果可用
from services.online_script.device_handler_service import get_device_service
device_service = get_device_service()
await device_service.start_service()
```
## 监控和调试
可以通过以下方式检查MQTT服务状态
```python
# 获取连接信息
connection_info = device_service.registry.mqtt_service.get_connection_info()
print(f"异步模式: {connection_info['async_mode']}")
print(f"连接状态: {connection_info['connected']}")
print(f"订阅主题: {connection_info['subscribed_topics']}")
```
## 故障排除
### 1. 如果aiomqtt安装失败
```bash
# 尝试升级pip
pip install --upgrade pip
pip install aiomqtt
# 或者使用conda安装
conda install -c conda-forge aiomqtt
```
### 2. 如果连接问题
- 检查MQTT服务器配置
- 确认网络连接
- 查看日志中的错误信息
### 3. 性能问题
- 监控CPU和内存使用
- 检查消息处理频率
- 考虑调整MQTT配置参数