1. Introduction
mica-mqtt simple , low latency , high-performance open source mqtt IoT components based on t-io use See : mica-mqtt-example
module.
2. Function
- [x] Support MQTT v3.1, v3.1.1 and v5.0 protocols.
- [x] Support MQTT client client.
- [x] Support MQTT server server.
- [x] Support MQTT will message.
- [x] Support MQTT reserved messages.
- [x] Support custom message (mq) processing and forwarding to realize clustering.
- [x] MQTT client Alibaba Cloud mqtt connects to the demo.
- [x] Support Spring boot project quick access (mica-mqtt-spring-boot-starter).
Three, to do
- [] Add websocket support (pre-researched successfully).
- [] Optimize the handling of mqtt session, and support v5.0
Four, update records
- ✨ Subscription management is integrated into session management.
- ✨ Add comments to MqttProperties.MqttPropertyType, consider the new feature processing of mqtt V5.0.
- ✨ Add Spring boot starter for easy access and compatible with lower version of Spring boot.
- ✨ Research the t-io websocket sub-protocol.
- 🐛 Fix some problems during java8 runtime, NoSuchMethodError: java.nio.ByteBuffer.xxx
Five, Spring boot quick access
5.1 Add dependency
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-spring-boot-starter</artifactId>
<version>${最新版本}</version>
</dependency>
5.2 Configuration items
Configuration item | Defaults | instruction |
---|
mqtt.server.name | Mica-Mqtt-Server | name |
mqtt.server.port | 1883 | port |
mqtt.server.ip | 127.0.0.1 | Server ip |
mqtt.server.buffer-allocator | Heap memory | Heap memory and off-heap memory |
mqtt.server.heartbeat-timeout | 120s | Heartbeat timeout (unit: milliseconds, default: 1000 * 120), if the user does not want to do heartbeat related work at the framework level, please set this value to 0 or a negative number |
mqtt.server.read-buffer-size | 8092 | The buffer size of the received data, default: 8092 |
mqtt.server.max-bytes-in-message | 8092 | Maximum bytes length of message parsing, default: 8092 |
mqtt.server.debug | false | debug |
5.3 Realizable interface (just register as Spring Bean)
interface | Do you have to | instruction |
---|
IMqttServerAuthHandler | Yes | For client authentication |
IMqttMessageListener | Yes | Message monitoring |
IMqttConnectStatusListener | Yes | Connection status monitoring |
IMqttSessionManager | no | session management |
IMqttMessageStore | Cluster yes, stand-alone no | Wills and retention message storage |
IMqttMessageDispatcher | Cluster yes, stand-alone no | Message forwarding |
IpStatListener | no | t-io ip transition monitoring |
5.4 Custom configuration (optional)
@Configuration(proxyBeanMethods = false)
public class MqttServerCustomizerConfiguration {
@Bean
public MqttServerCustomizer activeRecordPluginCustomizer() {
return new MqttServerCustomizer() {
@Override
public void customize(MqttServerCreator creator) {
// 此处可自定义配置 creator,会覆盖 yml 中的配置
System.out.println("----------------MqttServerCustomizer-----------------");
}
};
}
}
Six, common java project access
6.1 maven dependency
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-core</artifactId>
<version>1.0.1</version>
</dependency>
6.2 mica-mqtt client
// 初始化 mqtt 客户端
MqttClient client = MqttClient.create()
.ip("127.0.0.1")
.port(1883) // 默认:1883
.username("admin")
.password("123456")
.version(MqttVersion.MQTT_5) // 默认:3_1_1
.clientId("xxxxxx") // 默认:MICA-MQTT- 前缀和 36进制的纳秒数
.connect(); // 连接
// 消息订阅,同类方法 subxxx
client.subQos0("/test/#", (topic, payload) -> {
logger.info(topic + '\t' + ByteBufferUtil.toString(payload));
});
// 取消订阅
client.unSubscribe("/test/#");
// 发送消息
client.publish("/test/client", ByteBuffer.wrap("mica最牛皮".getBytes(StandardCharsets.UTF_8)));
// 断开连接
client.disconnect();
// 重连
client.reconnect();
// 停止
client.stop();
6.3 mica-mqtt server
// 注意:为了能接受更多链接(降低内存),请添加 jvm 参数 -Xss129k
MqttServer mqttServer = MqttServer.create()
// 默认:127.0.0.1
.ip("127.0.0.1")
// 默认:1883
.port(1883)
// 默认为: 8092(mqtt 默认最大消息大小),为了降低内存可以减小小此参数,如果消息过大 t-io 会尝试解析多次(建议根据实际业务情况而定)
.readBufferSize(512)
// 自定义认证
.authHandler((clientId, userName, password) -> true)
// 消息监听
.messageListener((clientId, topic, mqttQoS, payload) -> {
logger.info("clientId:{} topic:{} mqttQoS:{} message:{}", clientId, topic, mqttQoS, ByteBufferUtil.toString(payload));
})
// ssl 配置
.useSsl("", "", "")
// 自定义客户端上下线监听
.connectStatusListener(new IMqttConnectStatusListener() {
@Override
public void online(String clientId) {
}
@Override
public void offline(String clientId) {
}
})
// 自定义消息转发,可用 mq 广播实现集群化处理
.messageDispatcher(new IMqttMessageDispatcher() {
@Override
public void config(MqttServer mqttServer) {
}
@Override
public boolean send(Message message) {
return false;
}
@Override
public boolean send(String clientId, Message message) {
return false;
}
})
.debug() // 开启 t-io debug 信息日志
.start();
// 发送给某个客户端
mqttServer.publish("clientId","/test/123", ByteBuffer.wrap("mica最牛皮".getBytes()), MqttQoS.EXACTLY_ONCE);
// 发送给所有在线监听这个 topic 的客户端
mqttServer.publishAll("/test/123", ByteBuffer.wrap("mica最牛皮".getBytes()), MqttQoS.EXACTLY_ONCE);
// 停止服务
mqttServer.stop();
Seven, effect demonstration
8. Related documents
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。