add 增加 ruoyi-common-mqtt 模块
This commit is contained in:
parent
145b903185
commit
5b82c12e17
@ -241,3 +241,43 @@ warm-flow:
|
||||
node-tooltip: true
|
||||
# 默认Authorization,如果有多个token,用逗号分隔
|
||||
token-name: ${sa-token.token-name},clientid
|
||||
|
||||
--- # mqtt 配置
|
||||
# 具体配置还需查看文档
|
||||
# https://gitee.com/dromara/mica-mqtt
|
||||
mqtt.client:
|
||||
# 是否开启客户端,默认:true
|
||||
enabled: false
|
||||
# 连接的服务端 ip ,默认:127.0.0.1
|
||||
ip: 127.0.0.1
|
||||
# 端口:默认:1883
|
||||
port: 1883
|
||||
# 客户端名称
|
||||
name: Mqtt-Client
|
||||
# 客户端Id(非常重要,一般为设备 sn,不可重复)
|
||||
client-id: 000001
|
||||
username: ruoyi
|
||||
password: 123456
|
||||
# 超时时间,单位:秒,默认:5秒
|
||||
timeout: 5
|
||||
# 重连时间,默认 5000 毫秒
|
||||
re-interval: 5000
|
||||
# mqtt 协议版本,可选 MQTT_3_1、mqtt_3_1_1、mqtt_5,默认:mqtt_3_1_1
|
||||
version: mqtt_3_1_1
|
||||
# 接收数据的 buffer size,默认:8k
|
||||
read-buffer-size: 8KB
|
||||
# 消息解析最大 bytes 长度,默认:10M
|
||||
max-bytes-in-message: 10MB
|
||||
# keep-alive 时间,单位:秒
|
||||
keep-alive-secs: 60
|
||||
# 开启保留 session 时,session 的有效期
|
||||
session-expiry-interval-secs: 0
|
||||
# 工作线程数,如果消息量比较大,例如做 emqx 的转发消息处理,可以调大此参数
|
||||
biz-thread-pool-size: 2
|
||||
# 是否开启 ssl
|
||||
ssl:
|
||||
enabled: false
|
||||
keystore-path:
|
||||
keystore-pass:
|
||||
truststore-path:
|
||||
truststore-pass:
|
||||
|
||||
@ -33,6 +33,7 @@
|
||||
<module>ruoyi-common-encrypt</module>
|
||||
<module>ruoyi-common-websocket</module>
|
||||
<module>ruoyi-common-sse</module>
|
||||
<module>ruoyi-common-mqtt</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
|
||||
@ -172,6 +172,12 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-mqtt</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
33
ruoyi-common/ruoyi-common-mqtt/pom.xml
Normal file
33
ruoyi-common/ruoyi-common-mqtt/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-mqtt</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-mqtt 模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.mica-mqtt</groupId>
|
||||
<artifactId>mica-mqtt-client-spring-boot-starter</artifactId>
|
||||
<version>2.5.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,29 @@
|
||||
package org.dromara.common.mqtt.config;
|
||||
|
||||
import org.dromara.common.mqtt.listener.MqttClientConnectListener;
|
||||
import org.dromara.common.mqtt.listener.MqttClientGlobalMessageListener;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* mqtt客户端配置初始化
|
||||
* <p>
|
||||
* 用法文档 <a href="https://gitee.com/dromara/mica-mqtt/blob/master/starter/mica-mqtt-client-spring-boot-starter/README.md">...</a>
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class MqttAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MqttClientConnectListener mqttClientConnectListener(MqttClientCreator mqttClientCreator) {
|
||||
return new MqttClientConnectListener(mqttClientCreator);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttClientGlobalMessageListener mqttClientGlobalMessageListener() {
|
||||
return new MqttClientGlobalMessageListener();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package org.dromara.common.mqtt.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.core.client.IMqttClientConnectListener;
|
||||
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
/**
|
||||
* 客户端连接状态监听
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class MqttClientConnectListener implements IMqttClientConnectListener {
|
||||
//
|
||||
private final MqttClientCreator mqttClientCreator;
|
||||
|
||||
public MqttClientConnectListener(MqttClientCreator mqttClientCreator) {
|
||||
this.mqttClientCreator = mqttClientCreator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(ChannelContext context, boolean isReconnect) {
|
||||
// 创建连接
|
||||
log.info("MqttConnectedEvent:{}", context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnect(ChannelContext context, Throwable throwable, String remark, boolean isRemove) {
|
||||
// 离线时更新重连
|
||||
log.info("MqttDisconnectEvent:{}", context, throwable);
|
||||
// 在断线时更新 clientId、username、password
|
||||
// mqttClientCreator.clientId("newClient" + System.currentTimeMillis())
|
||||
// .username("newUserName")
|
||||
// .password("newPassword");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package org.dromara.common.mqtt.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
|
||||
import org.dromara.mica.mqtt.core.client.IMqttClientGlobalMessageListener;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 全局消息监听,可以监听到所有订阅消息
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class MqttClientGlobalMessageListener implements IMqttClientGlobalMessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
|
||||
log.info("MqttGlobalMessageEvent => topic: {}, msg: {}", topic, new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
org.dromara.common.mqtt.config.MqttAutoConfiguration
|
||||
@ -98,6 +98,11 @@
|
||||
<artifactId>ruoyi-common-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-mqtt</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package org.dromara.demo.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.demo.domain.TestDemo;
|
||||
import org.dromara.mica.mqtt.codec.MqttQoS;
|
||||
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
|
||||
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
|
||||
import org.dromara.mica.mqtt.core.deserialize.MqttJsonDeserializer;
|
||||
import org.dromara.mica.mqtt.spring.client.MqttClientTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* mqtt 演示案例
|
||||
* <p>
|
||||
* 用法文档 <a href="https://gitee.com/dromara/mica-mqtt/blob/master/starter/mica-mqtt-client-spring-boot-starter/README.md">...</a>
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/demo/mqtt")
|
||||
@Slf4j
|
||||
public class MqttController {
|
||||
|
||||
private final MqttClientTemplate client;
|
||||
|
||||
@GetMapping("/send")
|
||||
public boolean send() {
|
||||
client.publish("/test/client", "测试测试".getBytes(StandardCharsets.UTF_8));
|
||||
return true;
|
||||
}
|
||||
|
||||
@MqttClientSubscribe("/test/#")
|
||||
public void subQos0(String topic, byte[] payload) {
|
||||
log.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.QOS1)
|
||||
public void subQos1(String topic, byte[] payload) {
|
||||
log.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
|
||||
public void thingSubRegister(String topic, byte[] payload) {
|
||||
// 1.3.8 开始支持,@MqttClientSubscribe 注解支持 ${} 变量替换,会默认替换成 +
|
||||
// 注意:mica-mqtt 会先从 Spring boot 配置中替换参数 ${},如果存在配置会优先被替换。
|
||||
log.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@MqttClientSubscribe(
|
||||
value = "/test/json",
|
||||
deserialize = MqttJsonDeserializer.class // 2.4.5 开始支持 自定义序列化,默认 json 序列化
|
||||
)
|
||||
public void testJson(String topic, MqttPublishMessage message, TestDemo data) {
|
||||
// 2.4.5 开始支持,支持 2 到 3 个参数,字段类型映射规则如下
|
||||
// String 字符串会默认映射到 topic,
|
||||
// MqttPublishMessage 会默认映射到 原始的消息,可以拿到 mqtt5 的 props 参数
|
||||
// byte[] 会映射到 mqtt 消息内容 payload
|
||||
// ByteBuffer 会映射到 mqtt 消息内容 payload
|
||||
// 其他类型会走序列化,确保消息能够序列化,默认为 json 序列化
|
||||
log.info("topic:{} json data:{}", topic, data);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user