MQTT Qos2设计方案
mqtt相关协议:
qos2整体实现方案:
主要实现步骤如下:
mqtt Qos2增加redis的主要存储结构以及作用为:
名称 | key | value | 作用 |
---|---|---|---|
receiveQos2Message | clientId^packetId | msgId | 记录producer向mqtt server发送消息是否成功,避免重复发送 |
deliverQos2Message | clinetId-del | publishMessage(hset) | PUBLISH重发消息存储 |
deliverPubrelMessage | clinetId-rel | pubrelMessage(hset) | PUBREL重发消息存储 |
producer到MQTT Bridge的过程:
具体的流程图如下:
1.PUBLISH:
MqttPublishMessageProcessor:
当MQTT Bridge收到producer的PUBLISH MESSAGE,判断redis的clientId^packetId的值是否存在,如果存在不做处理,如果key不存在,把消息发送到broker保存起来,并将msgId 存入redis,保存deliverQos2Message到redis中后期投递给consumer使用;
2.PUBREC:
MqttPublishMessageProcessor:
MQTT Bridge向producer发送PUBREC MESSAGE,如果producer没有收到PUBREC MESSAGE会重新发送PUBLISH MESSAGE;
3.PUBREL:
MqttPubrelMessageProcess:
当MQTT Bridge收到producer的PUBREL MESSAE,判断redis中的clientId^packetId能否查询到msgId,如果存在,MQTT Bridge发布消息给consumer(MQTT Bridge到consumer的过程,task任务实现),把该msgId从redis中remove掉,向producer发送PUBCOMP MESSAGE,如果不存在,直接向producer发送PUBCOMP MESSAGE;
4.PUBCOM:
MqttPubrelMessageProcess:
MQTT Bridge向producer发送PUBCOMP MESSAGE,让producer知道producer到MQTT Bridge Qos2消息发送已结束,如果producer没有收到PUBCOMP MESSAGE,producer会重新发送PUBREL MESSAGE;
MQTT Bridge到consumer的过程:
具体流程图如下:
1.PUBLISH:
MqttPubrelMessageProcess:
当MQTT Bridge收到producer发送的PUBREL MESSAGE时从redis中取出deliverQos2Message投递给consumer;
2.PUBREC:
consumer向MQTT Bridge发送PUBREC MESSAGE,MQTT Bridge如果没有收到consumer 反馈的PUBREC MESSAGE,会继续重发消息;
3.PUBREL:
MqttPubrecMessageProcess:
当MQTT Bridgemqtt收到consumer的PUBREC MESSAGE时,MQTT Bridge向consumer发送PUBREL MESSAGE,并在redis中存入deliverPubrelMessage,从redis中移除对应的deliverQos2Message;
4.PUBCOM:
MqttPubcomMessageProcess:
当MQTT Bridge收到consumer的PUBCOMP MESSAGE,从redis中移除该deliverPubrelMessage,如果MQTT Bridge没有收到consumer反馈的PUBCOM MESSAGE,MQTT Bridge会重发PUBREL MESSAGE;
对于离线消息以及clinet掉线的处理过程:
MqttConnectMessageProcessor:
因为对于在线离线的clinet都会把deliverQos2Message,deliverPubrelMessage存入redis中,所以对于离线消息以及clinet掉线的处理只要在收到clinet的CONNET MESSAGE时,把redis中存储的deliverQos2Message,deliverPubrelMessage投递给clinet即可