I. Introduction
After being connected to the liver for more than a month, mica-mqtt ushered in a relatively stable version. In this more than a month, 7 official versions have been published and more than 100 submissions have been made. I sincerely thank those students who are paying attention, star, using and giving feedback.
2. Introduction
mica-mqtt simple , low latency , high-performance open source mqtt IoT components based on t-io please refer to mica-mqtt gitee source code mica-mqtt-example module .
Three, function
- [x] Support MQTT v3.1, v3.1.1 and v5.0 protocols.
- [x] Support websocket mqtt sub-protocol (support mqtt.js).
- [x] Support http rest api, http api document see .
- [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 GraalVM to compile native executable programs.
- [x] Support Spring boot project quick access (mica-mqtt-spring-boot-starter).
- [x] mica-mqtt-spring-boot-starter supports Prometheus + Grafana.
Four, update records
- :sparkles: mqtt-server optimizes connection close log.
- :sparkles: mqtt-server optimizes the subscription, the same topicFilter subscription judges qos.
- :sparkles: Add try catch to mqtt-server listener to avoid disconnection due to business problems.
- :sparkles: mqtt-server optimizes topicFilters verification.
- :sparkles: mqtt-client optimized subscription reasonCodes judgment.
- :sparkles: add try catch to mqtt-client listener to avoid disconnection due to business problems.
- :sparkles: mqtt-client adds the session validity period.
- :sparkles: Code optimization to reduce problems on codacy.
- :bug: mqtt-server fixes the heartbeat time issue.
- :bug: Fix the problem of message duplication when multiple subscriptions of mqtt-server match at the same time.
- :bug: mqtt-client optimizes the logic of connection processing, and subscribes after mqtt connects.
- :bug: Fix a potential null pointer in MqttProperties.
Five, Spring boot quick access
5.1 Add dependency
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
5.2 Server configuration example
mqtt:
server:
enabled: true # 是否开启,默认:true
ip: 127.0.0.1 # 服务端 ip 默认:127.0.0.1
port: 5883 # 端口,默认:1883
name: Mica-Mqtt-Server # 名称,默认:Mica-Mqtt-Server
buffer-allocator: HEAP # 堆内存和堆外内存,默认:堆内存
heartbeat-timeout: 120000 # 心跳超时,单位毫秒,默认: 1000 * 120
read-buffer-size: 8092 # 接收数据的 buffer size,默认:8092
max-bytes-in-message: 8092 # 消息解析最大 bytes 长度,默认:8092
debug: true # 如果开启 prometheus 指标收集建议关闭
websocket-enable: true # 开启 websocket 子协议,默认开启
websocket-port: 8083 # websocket 端口,默认:8083
5.3 Server-side implementable interface (just register as Spring Bean)
interface | Do you have to | illustrate |
---|---|---|
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 |
AbstractMqttMessageDispatcher | Cluster yes, stand-alone no | Message forwarding, (will, retained message forwarding) |
IpStatListener | no | t-io ip status monitoring |
5.4 Server-side 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-----------------");
}
};
}
}
5.5 MqttServerTemplate usage example
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.spring.server.MqttServerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.nio.ByteBuffer;
/**
* @author wsq
*/
@Service
public class ServerService {
@Autowired
private MqttServerTemplate server;
public boolean publish(String body) {
server.publishAll("/test/123", ByteBuffer.wrap(body.getBytes()));
return true;
}
}
5.6 Cluster processing based on mq message broadcast
- Realize
IMqttConnectStatusListener
processing equipment state storage. - Implement
IMqttMessageListener
to forward the message to mq, and the business processes the mq message on demand. - Implement
IMqttMessageStore
store wills and retain messages. - Implement
AbstractMqttMessageDispatcher
send the message to mq, mq then broadcasts back to the mqtt cluster, and mqtt sends the message to the device. - Business messages are sent to mq, mq is broadcast to mqtt clusters, and mqtt sends messages to devices.
5.7 Prometheus + Grafana monitoring docking
Thanks to the t-io t-iostat , which monitors the indicators directly, currently supports the following indicators, and will continue to be improved in the future.
Supported indicators | illustrate |
---|---|
mqtt_connections_accepted | Total number of connections accepted |
mqtt_connections_closed | Number of closed connections |
mqtt_connections_size | Current connections |
mqtt_messages_handled_packets | Number of messages processed |
mqtt_messages_handled_bytes | Number of message bytes processed |
mqtt_messages_received_packets | Number of messages received |
mqtt_messages_received_bytes | Number of message bytes processed |
mqtt_messages_send_packets | Number of messages sent |
mqtt_messages_send_bytes | Number of message bytes sent |
For more information about mica-mqtt-spring-boot-starter , please see the document: https://gitee.com/596392912/mica-mqtt/tree/master/mica-mqtt-spring-boot-starter
Six, common java project access
6.1 maven dependency
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-core</artifactId>
<version>1.1.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()));
// 发送给所有在线监听这个 topic 的客户端
mqttServer.publishAll("/test/123", ByteBuffer.wrap("mica最牛皮".getBytes()));
// 停止服务
mqttServer.stop();
Seven, effect demonstration
8. Follow us
The above two-dimensional code scanning, more exciting content recommended every day!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。