parent
c48c05bdad
commit
ffa448bb3f
@ -0,0 +1,38 @@
|
||||
<?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>
|
||||
<!-- webflux -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<!-- mqtt -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,32 @@
|
||||
package org.dromara.common.mqtt.config;
|
||||
|
||||
import org.dromara.common.mqtt.server.MqttGateway;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* MQTT 自动配置类
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MqttProperties.class)
|
||||
// 只有当配置中设置了 mqtt.host 时才启用此自动配置
|
||||
@ConditionalOnProperty(prefix = "mqtt", name = "host")
|
||||
public class MqttAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MqttGateway mqttGateway(MqttProperties mqttProperties) {
|
||||
return new MqttGateway(mqttProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebClient webClient(MqttProperties mqttProperties) {
|
||||
String basicAuth = Base64.getEncoder().encodeToString((mqttProperties.getApiKey() + ":" + mqttProperties.getSecretKey()).getBytes());
|
||||
return WebClient.builder().baseUrl(mqttProperties.getServer()).defaultHeader("Authorization", "Basic " + basicAuth).build();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package org.dromara.common.mqtt.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "mqtt") // 配置文件前缀
|
||||
public class MqttProperties {
|
||||
private String server;
|
||||
|
||||
/** Broker 地址 (例如: tcp://broker.emqx.io:1883) */
|
||||
private String host;
|
||||
|
||||
/** 客户端 ID */
|
||||
private String clientId;
|
||||
|
||||
/** 用户名 */
|
||||
private String username;
|
||||
|
||||
/** 密码 */
|
||||
private String password;
|
||||
|
||||
/** 连接超时时间 (秒) */
|
||||
private int connectionTimeout = 30;
|
||||
|
||||
/** 心跳间隔 (秒) */
|
||||
private int keepAliveInterval = 60;
|
||||
|
||||
/** 是否清除会话 */
|
||||
private boolean cleanSession = true;
|
||||
|
||||
/** 断开重连是否启用 */
|
||||
private boolean automaticReconnect = true;
|
||||
|
||||
private String apiKey;
|
||||
private String secretKey;
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
org.dromara.common.mqtt.config.MqttAutoConfiguration
|
||||
Loading…
Reference in new issue