MQTT概述

MQTT是一种轻量级的发布-订阅消息传递协议,它可能最适合各种物联网设备。你可

ThingsBoard服务器节点充当支持QoS级别0(最多一次)和QoS级别1(至少一次)以及一组预定义主题的MQTT主题。

ThingsBoard基于MQTT协议提供给设备的API是非常”灵活”的。

关联模块

查找思路:application 启动项目,pom 文件中找到 mqtt-transport-common,再查找 mqtt-transport-common的引用模块


1. mqtt-transport-service

2. mqtt-transport-common

3. 微服务版本(忽略)
image.png image.png image.png

MQTT Transport Service

  1. /**
  2. * Copyright © 2016-2022 The Thingsboard Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.thingsboard.server.mqtt;
  17. import org.springframework.boot.SpringApplication;
  18. import org.springframework.boot.SpringBootConfiguration;
  19. import org.springframework.context.annotation.ComponentScan;
  20. import org.springframework.scheduling.annotation.EnableAsync;
  21. import org.springframework.scheduling.annotation.EnableScheduling;
  22. import java.util.Arrays;
  23. @SpringBootConfiguration
  24. @EnableAsync
  25. @EnableScheduling
  26. @ComponentScan({"org.thingsboard.server.mqtt", "org.thingsboard.server.common", "org.thingsboard.server.transport.mqtt", "org.thingsboard.server.queue", "org.thingsboard.server.cache"})
  27. public class ThingsboardMqttTransportApplication {
  28. private static final String SPRING_CONFIG_NAME_KEY = "--spring.config.name";
  29. private static final String DEFAULT_SPRING_CONFIG_PARAM = SPRING_CONFIG_NAME_KEY + "=" + "tb-mqtt-transport";
  30. public static void main(String[] args) {
  31. SpringApplication.run(ThingsboardMqttTransportApplication.class, updateArguments(args));
  32. }
  33. private static String[] updateArguments(String[] args) {
  34. if (Arrays.stream(args).noneMatch(arg -> arg.startsWith(SPRING_CONFIG_NAME_KEY))) {
  35. String[] modifiedArgs = new String[args.length + 1];
  36. System.arraycopy(args, 0, modifiedArgs, 0, args.length);
  37. modifiedArgs[args.length] = DEFAULT_SPRING_CONFIG_PARAM;
  38. return modifiedArgs;
  39. }
  40. return args;
  41. }
  42. }

MQTT Transport Common

  1. └── main
  2. └── java
  3. └── org.thingsboard.server.transport.mqtt
  4. └── adaptors
  5. ├── BackwardCompatibilityAdaptor.java 兼容jsonProtobuf协议适配器
  6. ├── JsonMqttAdaptor.java Mqtt传输内容Json适配器
  7. ├── MqttTransportAdaptor.java Mqtt协议传输适配器
  8. └── ProtoMqttAdaptor.java Mqtt传输Protobuf适配器
  9. └── limits
  10. ├── IpFilter.java IP过滤器
  11. └── ProxyIpFilter.java 代理IP过滤器
  12. └── session
  13. ├── DeviceSessionCtx.java 设备会话上下文
  14. ├── GatewayDeviceSessionCtx.java 网关设备会话上下文
  15. ├── GatewaySessionHandler.java 网关会话处理类
  16. ├── MqttDeviceAwareSessionContext.java Mqtt设备会话上下文
  17. └── MqttTopicMatcher.java Mqtt主题匹配器
  18. └── util
  19. ├── AlwaysTrueTopicFilter.java always true 过滤器
  20. ├── EqualsTopicFilter.java Mqtt euqals过滤器
  21. ├── MqttTopicFilter.java Mqtt Topic过滤器
  22. ├── MqttTopicFilterFactory.java Mqtt Topic过滤器工厂
  23. └── RegexTopicFilter.java 正则topic过滤器
  24. ├── MqttSslHandlerProvider.java Mqtt Ssl逻辑处理提供类
  25. ├── MqttTransportContext.java Mqtt传输协议上下文
  26. ├── MqttTransportHandler.java Mqttt传输协议逻辑处理类
  27. ├── MqttTransportServerInitializer.java Mqtt传输协议初始化类
  28. ├── MqttTransportService.java Mqtt传输协议启动类
  29. └── TopicType.java Mqtt topic类型枚举