first commit

This commit is contained in:
zc
2025-12-08 10:40:43 +08:00
commit 871ae8be0a
410 changed files with 38212 additions and 0 deletions

View File

@@ -0,0 +1,267 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.server.MqttServer;
import org.dromara.mica.mqtt.core.server.model.ClientInfo;
import org.dromara.mica.mqtt.core.server.model.Subscribe;
import org.tio.core.ChannelContext;
import org.tio.core.stat.vo.StatVo;
import org.tio.utils.page.Page;
import org.tio.utils.timer.TimerTask;
import java.util.List;
import java.util.concurrent.Executor;
/**
* mqtt Server 模板
*
* @author wsq冷月宫主
* @author ChangJin Wei (魏昌进)
*/
@Getter
@RequiredArgsConstructor
public class MqttServerTemplate {
private final MqttServer mqttServer;
/**
* 发布消息
*
* @param clientId clientId
* @param topic topic
* @param payload 消息体
* @return 是否发送成功
*/
public boolean publish(String clientId, String topic, Object payload) {
return mqttServer.publish(clientId, topic, payload);
}
/**
* 发布消息
*
* @param clientId clientId
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @return 是否发送成功
*/
public boolean publish(String clientId, String topic, Object payload, MqttQoS qos) {
return mqttServer.publish(clientId, topic, payload, qos);
}
/**
* 发布消息
*
* @param clientId clientId
* @param topic topic
* @param payload 消息体
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publish(String clientId, String topic, Object payload, boolean retain) {
return mqttServer.publish(clientId, topic, payload, retain);
}
/**
* 发布消息
*
* @param clientId clientId
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publish(String clientId, String topic, Object payload, MqttQoS qos, boolean retain) {
return mqttServer.publish(clientId, topic, payload, qos, retain);
}
/**
* 发布消息给所以的在线设备
*
* @param topic topic
* @param payload 消息体
* @return 是否发送成功
*/
public boolean publishAll(String topic, Object payload) {
return mqttServer.publishAll(topic, payload);
}
/**
* 发布消息
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @return 是否发送成功
*/
public boolean publishAll(String topic, Object payload, MqttQoS qos) {
return mqttServer.publishAll(topic, payload, qos);
}
/**
* 发布消息给所以的在线设备
*
* @param topic topic
* @param payload 消息体
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publishAll(String topic, Object payload, boolean retain) {
return mqttServer.publishAll(topic, payload, retain);
}
/**
* 发布消息给所以的在线设备
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publishAll(String topic, Object payload, MqttQoS qos, boolean retain) {
return mqttServer.publishAll(topic, payload, qos, retain);
}
/**
* 获取客户端信息
*
* @param clientId clientId
* @return ClientInfo
*/
public ClientInfo getClientInfo(String clientId) {
return mqttServer.getClientInfo(clientId);
}
/**
* 获取客户端信息
*
* @param context ChannelContext
* @return ClientInfo
*/
public ClientInfo getClientInfo(ChannelContext context) {
return mqttServer.getClientInfo(context);
}
/**
* 获取所有的客户端
*
* @return 客户端列表
*/
public List<ClientInfo> getClients() {
return mqttServer.getClients();
}
/**
* 分页获取所有的客户端
*
* @param pageIndex pageIndex默认为 1
* @param pageSize pageSize默认为所有
* @return 分页
*/
public Page<ClientInfo> getClients(Integer pageIndex, Integer pageSize) {
return mqttServer.getClients(pageIndex, pageSize);
}
/**
* 获取统计数据
* @return StatVo
*/
public StatVo getStat() {
return mqttServer.getStat();
}
/**
* 获取客户端订阅情况
*
* @param clientId clientId
* @return 订阅集合
*/
public List<Subscribe> getSubscriptions(String clientId) {
return mqttServer.getSubscriptions(clientId);
}
/**
* 添加定时任务,注意:如果抛出异常,会终止后续任务,请自行处理异常
*
* @param command runnable
* @param delay delay
* @return TimerTask
*/
public TimerTask schedule(Runnable command, long delay) {
return mqttServer.schedule(command, delay);
}
/**
* 添加定时任务,注意:如果抛出异常,会终止后续任务,请自行处理异常
*
* @param command runnable
* @param delay delay
* @param executor 用于自定义线程池,处理耗时业务
* @return TimerTask
*/
public TimerTask schedule(Runnable command, long delay, Executor executor) {
return mqttServer.schedule(command, delay, executor);
}
/**
* 添加定时任务
*
* @param command runnable
* @param delay delay
* @return TimerTask
*/
public TimerTask scheduleOnce(Runnable command, long delay) {
return mqttServer.scheduleOnce(command, delay);
}
/**
* 添加定时任务
*
* @param command runnable
* @param delay delay
* @param executor 用于自定义线程池,处理耗时业务
* @return TimerTask
*/
public TimerTask scheduleOnce(Runnable command, long delay, Executor executor) {
return mqttServer.scheduleOnce(command, delay, executor);
}
/**
* 获取 ChannelContext
*
* @param clientId clientId
* @return ChannelContext
*/
public ChannelContext getChannelContext(String clientId) {
return mqttServer.getChannelContext(clientId);
}
/**
* 服务端主动断开连接
*
* @param clientId clientId
*/
public void close(String clientId) {
mqttServer.close(clientId);
}
}

View File

@@ -0,0 +1,195 @@
package org.dromara.mica.mqtt.server.solon.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.tio.utils.hutool.StrUtil;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* DataSize 兼容
*
* @author L.cm
*/
@Getter
@RequiredArgsConstructor
class DataSize {
/**
* Bytes per Kilobyte.
*/
private static final long BYTES_PER_KB = 1024;
/**
* Bytes per Megabyte.
*/
private static final long BYTES_PER_MB = BYTES_PER_KB * 1024;
/**
* Bytes per Gigabyte.
*/
private static final long BYTES_PER_GB = BYTES_PER_MB * 1024;
/**
* Bytes per Terabyte.
*/
private static final long BYTES_PER_TB = BYTES_PER_GB * 1024;
private final long bytes;
/**
* Obtain a {@link DataSize} representing the specified number of bytes.
* @param bytes the number of bytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofBytes(long bytes) {
return new DataSize(bytes);
}
/**
* Obtain a {@link DataSize} representing the specified number of kilobytes.
* @param kilobytes the number of kilobytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofKilobytes(long kilobytes) {
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
}
/**
* Obtain a {@link DataSize} representing the specified number of megabytes.
* @param megabytes the number of megabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofMegabytes(long megabytes) {
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
}
/**
* Obtain a {@link DataSize} representing the specified number of gigabytes.
* @param gigabytes the number of gigabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofGigabytes(long gigabytes) {
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
}
/**
* Obtain a {@link DataSize} representing the specified number of terabytes.
* @param terabytes the number of terabytes, positive or negative
* @return a {@link DataSize}
*/
public static DataSize ofTerabytes(long terabytes) {
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
}
/**
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
* @param amount the amount of the size, measured in terms of the unit,
* positive or negative
* @return a corresponding {@link DataSize}
*/
public static DataSize of(long amount, DataUnit unit) {
Objects.requireNonNull(unit, "Unit must not be null");
return new DataSize(Math.multiplyExact(amount, unit.getSize().getBytes()));
}
/**
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
* the specified default {@link DataUnit} if no unit is specified.
* <p>
* The string starts with a number followed optionally by a unit matching one of the
* supported {@linkplain DataUnit suffixes}.
* <p>
* Examples:
* <pre>
* "12KB" -- parses as "12 kilobytes"
* "5MB" -- parses as "5 megabytes"
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES})
* </pre>
* @param text the text to parse
* @return the parsed {@link DataSize}
*/
public static DataSize parse(String text) {
Objects.requireNonNull(text, "Text must not be null");
try {
Matcher matcher = DataSizeUtils.PATTERN.matcher(text.trim());
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid data size: " + text);
}
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2));
long amount = Long.parseLong(matcher.group(1));
return DataSize.of(amount, unit);
}
catch (Exception ex) {
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
}
}
/**
* Static nested class to support lazy loading of the {@link #PATTERN}.
* @since 5.3.21
*/
private static class DataSizeUtils {
/**
* The pattern for parsing.
*/
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
private static DataUnit determineDataUnit(String suffix) {
return (StrUtil.isNotBlank(suffix) ? DataUnit.fromSuffix(suffix) : DataUnit.BYTES);
}
}
@Getter
@RequiredArgsConstructor
public enum DataUnit {
/**
* Bytes, represented by suffix {@code B}.
*/
BYTES("B", DataSize.ofBytes(1)),
/**
* Kilobytes, represented by suffix {@code KB}.
*/
KILOBYTES("KB", DataSize.ofKilobytes(1)),
/**
* Megabytes, represented by suffix {@code MB}.
*/
MEGABYTES("MB", DataSize.ofMegabytes(1)),
/**
* Gigabytes, represented by suffix {@code GB}.
*/
GIGABYTES("GB", DataSize.ofGigabytes(1)),
/**
* Terabytes, represented by suffix {@code TB}.
*/
TERABYTES("TB", DataSize.ofTerabytes(1));
private final String suffix;
private final DataSize size;
/**
* Return the {@link DataUnit} matching the specified {@code suffix}.
* @param suffix one of the standard suffixes
* @return the {@link DataUnit} matching the specified {@code suffix}
* @throws IllegalArgumentException if the suffix does not match the suffix
* of any of this enum's constants
*/
public static DataUnit fromSuffix(String suffix) {
for (DataUnit candidate : values()) {
if (candidate.suffix.equals(suffix)) {
return candidate;
}
}
throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
}
}
}

View File

@@ -0,0 +1,119 @@
/* Copyright (c) 2022 Peigen.info. All rights reserved. */
package org.dromara.mica.mqtt.server.solon.config;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.deserialize.MqttJsonDeserializer;
import org.dromara.mica.mqtt.core.server.MqttServer;
import org.dromara.mica.mqtt.core.server.MqttServerCreator;
import org.dromara.mica.mqtt.core.server.event.IMqttConnectStatusListener;
import org.dromara.mica.mqtt.core.server.event.IMqttMessageListener;
import org.dromara.mica.mqtt.core.server.func.MqttFunctionManager;
import org.dromara.mica.mqtt.core.server.func.MqttFunctionMessageListener;
import org.dromara.mica.mqtt.server.solon.event.SolonEventMqttConnectStatusListener;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.tio.core.Node;
/**
* <b>(MqttServerConfiguration)</b>
*
* @author LiHai
* @version 1.0.0
* @since 2023/7/20
*/
@Configuration
public class MqttServerConfiguration {
@Bean
@Condition(onMissingBean = MqttDeserializer.class)
public MqttDeserializer mqttDeserializer() {
return new MqttJsonDeserializer();
}
@Bean
@Condition(onMissingBean = IMqttConnectStatusListener.class)
public IMqttConnectStatusListener connectStatusListener() {
return new SolonEventMqttConnectStatusListener();
}
@Bean
@Condition(onMissingBean = MqttFunctionManager.class)
public MqttFunctionManager mqttFunctionManager() {
return new MqttFunctionManager();
}
@Bean
@Condition(onMissingBean = IMqttMessageListener.class)
public IMqttMessageListener mqttFunctionMessageListener(MqttFunctionManager mqttFunctionManager) {
return new MqttFunctionMessageListener(mqttFunctionManager);
}
@Bean
public MqttServerCreator mqttServerCreator(MqttServerProperties properties) {
MqttServerCreator serverCreator = MqttServer.create()
.name(properties.getName())
.heartbeatTimeout(properties.getHeartbeatTimeout())
.keepaliveBackoff(properties.getKeepaliveBackoff())
.readBufferSize((int) DataSize.parse(properties.getReadBufferSize()).getBytes())
.maxBytesInMessage((int) DataSize.parse(properties.getMaxBytesInMessage()).getBytes())
.maxClientIdLength(properties.getMaxClientIdLength())
.nodeName(properties.getNodeName())
.statEnable(properties.isStatEnable())
.proxyProtocolEnable(properties.isProxyProtocolOn());
if (properties.isDebug()) {
serverCreator.debug();
}
// mqtt 协议
MqttServerProperties.Listener mqttListener = properties.getMqttListener();
if (mqttListener.isEnable()) {
serverCreator.enableMqtt(builder -> builder.serverNode(mqttListener.getServerNode()).build());
}
// mqtt ssl 协议
MqttServerProperties.SslListener mqttSslListener = properties.getMqttSslListener();
if (mqttSslListener.isEnable()) {
MqttServerProperties.Ssl ssl = mqttSslListener.getSsl();
serverCreator.enableMqttSsl(sslBuilder -> sslBuilder
.serverNode(mqttSslListener.getServerNode())
.useSsl(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getTruststorePass(), ssl.getClientAuth())
.build());
}
// mqtt websocket 协议
MqttServerProperties.Listener wsListener = properties.getWsListener();
if (wsListener.isEnable()) {
serverCreator.enableMqttWs(builder -> builder.serverNode(wsListener.getServerNode()).build());
}
MqttServerProperties.SslListener wssListener = properties.getWssListener();
if (mqttSslListener.isEnable()) {
MqttServerProperties.Ssl ssl = wssListener.getSsl();
serverCreator.enableMqttWss(sslBuilder -> sslBuilder
.serverNode(wssListener.getServerNode())
.useSsl(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getTruststorePass(), ssl.getClientAuth())
.build());
}
// mqtt http api
MqttServerProperties.HttpListener httpListener = properties.getHttpListener();
if (httpListener.isEnable()) {
Node serverNode = httpListener.getServerNode();
MqttServerProperties.HttpBasicAuth basicAuth = httpListener.getBasicAuth();
MqttServerProperties.McpServer mcpServer = httpListener.getMcpServer();
MqttServerProperties.HttpSsl ssl = httpListener.getSsl();
serverCreator.enableMqttHttpApi(builder -> {
builder.serverNode(serverNode);
if (basicAuth.isEnable()) {
builder.basicAuth(basicAuth.getUsername(), basicAuth.getPassword());
}
if (mcpServer.isEnable()) {
builder.mcpServer(mcpServer.getSseEndpoint(), mcpServer.getMessageEndpoint());
}
if (ssl.isEnable()) {
builder.useSsl(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getTruststorePass(), ssl.getClientAuth());
}
return builder.build();
});
}
return serverCreator;
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.config;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.tio.core.Tio;
import org.tio.server.ServerGroupStat;
import org.tio.server.TioServerConfig;
import java.util.Collections;
/**
* mica mqtt Metrics
*
* @author L.cm
*/
@Slf4j
@RequiredArgsConstructor
public class MqttServerMetrics {
/**
* Prefix used for all mica-mqtt metric names.
*/
public static final String MQTT_METRIC_NAME_PREFIX = "mqtt";
/**
* 连接统计
*/
private static final String MQTT_CONNECTIONS_ACCEPTED = MQTT_METRIC_NAME_PREFIX + ".connections.accepted";
private static final String MQTT_CONNECTIONS_SIZE = MQTT_METRIC_NAME_PREFIX + ".connections.size";
private static final String MQTT_CONNECTIONS_CLOSED = MQTT_METRIC_NAME_PREFIX + ".connections.closed";
/**
* 消息统计
*/
private static final String MQTT_MESSAGES_HANDLED_PACKETS = MQTT_METRIC_NAME_PREFIX + ".messages.handled.packets";
private static final String MQTT_MESSAGES_HANDLED_BYTES = MQTT_METRIC_NAME_PREFIX + ".messages.handled.bytes";
private static final String MQTT_MESSAGES_RECEIVED_PACKETS = MQTT_METRIC_NAME_PREFIX + ".messages.received.packets";
private static final String MQTT_MESSAGES_RECEIVED_BYTES = MQTT_METRIC_NAME_PREFIX + ".messages.received.bytes";
private static final String MQTT_MESSAGES_SEND_PACKETS = MQTT_METRIC_NAME_PREFIX + ".messages.send.packets";
private static final String MQTT_MESSAGES_SEND_BYTES = MQTT_METRIC_NAME_PREFIX + ".messages.send.bytes";
private final Iterable<Tag> tags;
public MqttServerMetrics() {
this(Collections.emptyList());
}
public void bindTo(MeterRegistry meterRegistry, TioServerConfig serverConfig) {
// 连接统计
Gauge.builder(MQTT_CONNECTIONS_ACCEPTED, serverConfig, (config) -> ((ServerGroupStat) config.getGroupStat()).accepted.sum())
.description("Mqtt server connections accepted")
.tags(tags)
.register(meterRegistry);
Gauge.builder(MQTT_CONNECTIONS_SIZE, serverConfig, (config) -> Tio.getAll(config).size())
.description("Mqtt server connections size")
.tags(tags)
.register(meterRegistry);
Gauge.builder(MQTT_CONNECTIONS_CLOSED, serverConfig, (config) -> config.getGroupStat().getClosed().sum())
.description("Mqtt server connections closed")
.tags(tags)
.register(meterRegistry);
// 消息统计
Gauge.builder(MQTT_MESSAGES_HANDLED_PACKETS, serverConfig, (config) -> config.getGroupStat().getHandledPackets().sum())
.description("Mqtt server handled packets")
.tags(tags)
.register(meterRegistry);
Gauge.builder(MQTT_MESSAGES_HANDLED_BYTES, serverConfig, (config) -> config.getGroupStat().getHandledBytes().sum())
.description("Mqtt server handled bytes")
.tags(tags)
.register(meterRegistry);
// 接收的消息
Gauge.builder(MQTT_MESSAGES_RECEIVED_PACKETS, serverConfig, (config) -> config.getGroupStat().getReceivedPackets().sum())
.description("Mqtt server received packets")
.tags(tags)
.register(meterRegistry);
Gauge.builder(MQTT_MESSAGES_RECEIVED_BYTES, serverConfig, (config) -> config.getGroupStat().getReceivedBytes().sum())
.description("Mqtt server received bytes")
.tags(tags)
.register(meterRegistry);
// 发送的消息
Gauge.builder(MQTT_MESSAGES_SEND_PACKETS, serverConfig, (config) -> config.getGroupStat().getSentPackets().sum())
.description("Mqtt server send packets")
.tags(tags)
.register(meterRegistry);
Gauge.builder(MQTT_MESSAGES_SEND_BYTES, serverConfig, (config) -> config.getGroupStat().getSentPackets().sum())
.description("Mqtt server send bytes")
.tags(tags)
.register(meterRegistry);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.config;
import io.micrometer.core.instrument.MeterRegistry;
import org.dromara.mica.mqtt.server.solon.MqttServerTemplate;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.event.AppLoadEndEvent;
/**
* mica mqtt Metrics
*
* @author L.cm
*/
@Configuration
@Condition(onClass = MeterRegistry.class)
public class MqttServerMetricsConfiguration {
@Bean
@Condition(onBean = MeterRegistry.class)
public MqttServerMetrics mqttServerMetrics(MeterRegistry registry, AppContext context) {
MqttServerMetrics metrics = new MqttServerMetrics();
// 应用加载完成事件
context.onEvent(AppLoadEndEvent.class, event -> {
MqttServerTemplate mqttServerTemplate = context.getBean(MqttServerTemplate.class);
metrics.bindTo(registry, mqttServerTemplate.getMqttServer().getServerConfig());
});
return metrics;
}
}

View File

@@ -0,0 +1,240 @@
/* Copyright (c) 2022 Peigen.info. All rights reserved. */
package org.dromara.mica.mqtt.server.solon.config;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.dromara.mica.mqtt.codec.MqttConstant;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.tio.core.Node;
import org.tio.core.ssl.ClientAuth;
/**
* <b>(MqttServerProperties)</b>
*
* @author Lihai
* @version 1.0.0
* @since 2023/7/19
*/
@Inject(value = "${" + MqttServerProperties.PREFIX + "}", required = false)
@Configuration
@Data
public class MqttServerProperties {
/**
* 配置前缀
*/
public static final String PREFIX = "mqtt.server";
/**
* 是否启用,默认:启用
*/
private boolean enabled = true;
/**
* 名称
*/
private String name = "Mica-Mqtt-Server";
/**
* mqtt 认证
*/
private MqttAuth auth = new MqttAuth();
/**
* 心跳超时时间(单位: 毫秒 默认: 1000 * 120)如果用户不希望框架层面做心跳相关工作请把此值设为0或负数
*/
private Long heartbeatTimeout;
/**
* MQTT 客户端 keepalive 系数,连接超时缺省为连接设置的 keepalive * keepaliveBackoff * 2默认0.75
* <p>
* 如果读者想对该值做一些调整,可以在此进行配置。比如设置为 0.75,则变为 keepalive * 1.5。但是该值不得小于 0.5,否则将小于 keepalive 设定的时间。
*/
private float keepaliveBackoff = 0.75F;
/**
* 接收数据的 buffer size默认8KB
*/
private String readBufferSize = "8KB";
/**
* 消息解析最大 bytes 长度默认10MB
*/
private String maxBytesInMessage = "10MB";
/**
* debug
*/
private boolean debug = false;
/**
* mqtt 3.1 会校验此参数为 23为了减少问题设置成了 64
*/
private int maxClientIdLength = MqttConstant.DEFAULT_MAX_CLIENT_ID_LENGTH;
/**
* 节点名称,用于处理集群
*/
private String nodeName;
/**
* 是否开启监控不开启可节省内存默认true
*/
private boolean statEnable = true;
/**
* 开启代理协议,支持 nginx proxy_protocol on;
*/
private boolean proxyProtocolOn = false;
/**
* mqtt tcp 监听器
*/
private Listener mqttListener = new Listener();
/**
* mqtt tcp ssl 监听器
*/
private SslListener mqttSslListener = new SslListener();
/**
* websocket mqtt 监听器
*/
private Listener wsListener = new Listener();
/**
* websocket ssl mqtt 监听器
*/
private SslListener wssListener = new SslListener();
/**
* http api 监听器
*/
private HttpListener httpListener = new HttpListener();
@Getter
@Setter
public static class MqttAuth {
/**
* 是否启用,默认:关闭
*/
private boolean enable = false;
/**
* http Basic 认证账号
*/
private String username;
/**
* http Basic 认证密码
*/
private String password;
}
@Getter
@Setter
public static class Listener {
/**
* 是否启用,默认:关闭
*/
private boolean enable = false;
/**
* 服务端 ip
*/
private String ip;
/**
* 端口
*/
private Integer port;
/**
* 获取服务节点
*
* @return ServerNode
*/
public Node getServerNode() {
if (this.ip == null && this.port == null) {
return null;
} else {
return new Node(this.ip, this.port);
}
}
}
@Getter
@Setter
public static class SslListener extends Listener {
/**
* ssl 配置
*/
private Ssl ssl = new Ssl();
}
@Getter
@Setter
public static class Ssl {
/**
* keystore 证书路径
*/
private String keystorePath;
/**
* keystore 密码
*/
private String keystorePass;
/**
* truststore 证书路径
*/
private String truststorePath;
/**
* truststore 密码
*/
private String truststorePass;
/**
* 认证类型
*/
private ClientAuth clientAuth = ClientAuth.NONE;
}
@Getter
@Setter
public static class HttpListener extends Listener {
/**
* basic 认证
*/
private HttpBasicAuth basicAuth = new HttpBasicAuth();
/**
* mcp 配置
*/
private McpServer mcpServer = new McpServer();
/**
* ssl 配置
*/
private HttpSsl ssl = new HttpSsl();
}
@Getter
@Setter
public static class HttpSsl extends Ssl {
/**
* 是否启用,默认:关闭
*/
private boolean enable = false;
}
@Getter
@Setter
public static class HttpBasicAuth {
/**
* 是否启用,默认:关闭
*/
private boolean enable = false;
/**
* http Basic 认证账号
*/
private String username;
/**
* http Basic 认证密码
*/
private String password;
}
@Getter
@Setter
public static class McpServer {
/**
* 是否启用,默认:关闭
*/
private boolean enable = false;
/**
* sse 端点
*/
private String sseEndpoint;
/**
* message 端点
*/
private String messageEndpoint;
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.event;
import lombok.Data;
import java.io.Serializable;
/**
* 客户端断开原因
*
* @author L.cm
*/
@Data
public class MqttClientOfflineEvent implements Serializable {
/**
* 客户端 Id
*/
private String clientId;
/**
* 用户名
*/
private String username;
/**
* 断开原因
*/
private String reason;
/**
* 时间戳
*/
private long ts;
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.event;
import lombok.Data;
import java.io.Serializable;
/**
* 客户端断开事件
*
* @author L.cm
*/
@Data
public class MqttClientOnlineEvent implements Serializable {
/**
* 客户端 id
*/
private String clientId;
/**
* 用户名
*/
private String username;
/**
* ip
*/
private String ipAddress;
/**
* 端口
*/
private int port;
/**
* keepalive
*/
private long keepalive;
/**
* 时间戳
*/
private long ts;
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.event;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.server.event.IMqttConnectStatusListener;
import org.noear.solon.core.event.EventBus;
import org.tio.core.ChannelContext;
import org.tio.core.Node;
import java.util.concurrent.TimeUnit;
/**
* spring event mqtt 连接状态
*
* @author L.cm
*/
@Slf4j
public class SolonEventMqttConnectStatusListener implements IMqttConnectStatusListener {
@Override
public void online(ChannelContext context, String clientId, String username) {
log.info("Mqtt clientId:{} username:{} online.", clientId, username);
MqttClientOnlineEvent onlineEvent = new MqttClientOnlineEvent();
onlineEvent.setClientId(clientId);
onlineEvent.setUsername(username);
// clientNode
Node clientNode = context.getClientNode();
onlineEvent.setIpAddress(clientNode.getIp());
onlineEvent.setPort(clientNode.getPort());
// keepalive
long keepalive = context.heartbeatTimeout == null ? 60L : TimeUnit.MILLISECONDS.toSeconds(context.heartbeatTimeout);
onlineEvent.setKeepalive(keepalive);
onlineEvent.setTs(context.stat.timeCreated);
EventBus.publish(onlineEvent);
}
@Override
public void offline(ChannelContext context, String clientId, String username, String reason) {
log.info("Mqtt clientId:{} username:{} offline reason:{}.", clientId, username, reason);
MqttClientOfflineEvent offlineEvent = new MqttClientOfflineEvent();
offlineEvent.setClientId(clientId);
offlineEvent.setUsername(username);
offlineEvent.setReason(reason);
offlineEvent.setTs(context.stat.timeClosed);
EventBus.publish(offlineEvent);
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.integration;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.function.ParamValueFunction;
import org.dromara.mica.mqtt.core.server.func.IMqttFunctionMessageListener;
import org.dromara.mica.mqtt.core.util.MethodParamUtil;
import org.tio.core.ChannelContext;
import org.tio.utils.mica.ExceptionUtils;
import java.lang.reflect.Method;
/**
* mqtt 服务端函数消息监听器
*
* @author L.cm
*/
class MqttServerFunctionListener implements IMqttFunctionMessageListener {
private final Object bean;
private final Method method;
private final ParamValueFunction[] paramValueFunctions;
MqttServerFunctionListener(Object bean, Method method, String[] topicTemplates, String[] topicFilters, MqttDeserializer deserializer) {
this.bean = bean;
this.method = method;
this.paramValueFunctions = MethodParamUtil.getParamValueFunctions(method, topicTemplates, topicFilters, deserializer);
}
@Override
public void onMessage(ChannelContext context, String clientId, String topic, MqttQoS qoS, MqttPublishMessage message) {
// 处理参数
Object[] methodParameters = getMethodParameters(context, topic, message, message.payload());
// 方法调用
try {
method.invoke(bean, methodParameters);
} catch (Throwable e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* 获取反射参数
*
* @param context context
* @param topic topic
* @param message message
* @param payload payload
* @return Object array
*/
protected Object[] getMethodParameters(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
int length = paramValueFunctions.length;
Object[] parameters = new Object[length];
for (int i = 0; i < length; i++) {
ParamValueFunction function = paramValueFunctions[i];
parameters[i] = function.getValue(context, topic, message, payload);
}
return parameters;
}
}

View File

@@ -0,0 +1,240 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.server.solon.integration;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
import org.dromara.mica.mqtt.core.server.MqttServer;
import org.dromara.mica.mqtt.core.server.MqttServerCreator;
import org.dromara.mica.mqtt.core.server.MqttServerCustomizer;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerAuthHandler;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerPublishPermission;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerSubscribeValidator;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerUniqueIdService;
import org.dromara.mica.mqtt.core.server.dispatcher.IMqttMessageDispatcher;
import org.dromara.mica.mqtt.core.server.event.IMqttConnectStatusListener;
import org.dromara.mica.mqtt.core.server.event.IMqttMessageListener;
import org.dromara.mica.mqtt.core.server.event.IMqttSessionListener;
import org.dromara.mica.mqtt.core.server.func.IMqttFunctionMessageListener;
import org.dromara.mica.mqtt.core.server.func.MqttFunctionManager;
import org.dromara.mica.mqtt.core.server.interceptor.IMqttMessageInterceptor;
import org.dromara.mica.mqtt.core.server.session.IMqttSessionManager;
import org.dromara.mica.mqtt.core.server.store.IMqttMessageStore;
import org.dromara.mica.mqtt.core.server.support.DefaultMqttServerAuthHandler;
import org.dromara.mica.mqtt.core.util.TopicUtil;
import org.dromara.mica.mqtt.core.annotation.MqttServerFunction;
import org.dromara.mica.mqtt.server.solon.MqttServerTemplate;
import org.dromara.mica.mqtt.server.solon.config.MqttServerConfiguration;
import org.dromara.mica.mqtt.server.solon.config.MqttServerMetricsConfiguration;
import org.dromara.mica.mqtt.server.solon.config.MqttServerProperties;
import org.noear.solon.Solon;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.core.Plugin;
import org.noear.solon.core.util.ClassUtil;
import java.lang.reflect.Method;
import java.util.*;
/**
* <b>(MqttServerPluginImpl)</b>
*
* @author LiHai
* @version 1.0.0
* @since 2023/7/20
*/
@Slf4j
public class MqttServerPluginImpl implements Plugin {
private final List<ExtractorClassTag<MqttServerFunction>> functionClassTags = new ArrayList<>();
private final List<ExtractorMethodTag<MqttServerFunction>> functionMethodTags = new ArrayList<>();
private volatile boolean running = false;
private AppContext context;
@Override
public void start(AppContext context) throws Throwable {
this.context = context; //todo: 去掉 Solon.context() 写法,可同时兼容 2.5 之前与之后的版本
// 查找类上的 MqttServerFunction 注解
context.beanBuilderAdd(MqttServerFunction.class, (clz, beanWrap, anno) -> {
functionClassTags.add(new ExtractorClassTag<>(clz, beanWrap, anno));
});
// 查找方法上的 MqttServerFunction 注解
context.beanExtractorAdd(MqttServerFunction.class, (bw, method, anno) -> {
functionMethodTags.add(new ExtractorMethodTag<>(bw, method, anno));
});
context.lifecycle(-9, () -> {
context.beanMake(MqttServerProperties.class);
context.beanMake(MqttServerConfiguration.class);
MqttServerProperties properties = context.getBean(MqttServerProperties.class);
MqttServerCreator serverCreator = context.getBean(MqttServerCreator.class);
IMqttServerAuthHandler authHandlerImpl = context.getBean(IMqttServerAuthHandler.class);
IMqttServerUniqueIdService uniqueIdService = context.getBean(IMqttServerUniqueIdService.class);
IMqttServerSubscribeValidator subscribeValidator = context.getBean(IMqttServerSubscribeValidator.class);
IMqttServerPublishPermission publishPermission = context.getBean(IMqttServerPublishPermission.class);
IMqttMessageDispatcher messageDispatcher = context.getBean(IMqttMessageDispatcher.class);
IMqttMessageStore messageStore = context.getBean(IMqttMessageStore.class);
IMqttSessionManager sessionManager = context.getBean(IMqttSessionManager.class);
IMqttSessionListener sessionListener = context.getBean(IMqttSessionListener.class);
IMqttMessageListener messageListener = context.getBean(IMqttMessageListener.class);
IMqttConnectStatusListener connectStatusListener = context.getBean(IMqttConnectStatusListener.class);
IMqttMessageInterceptor messageInterceptor = context.getBean(IMqttMessageInterceptor.class);
MqttServerCustomizer customizers = context.getBean(MqttServerCustomizer.class);
// 自定义消息监听
serverCreator.messageListener(messageListener);
// 认证处理器
MqttServerProperties.MqttAuth mqttAuth = properties.getAuth();
if (Objects.isNull(authHandlerImpl)) {
IMqttServerAuthHandler authHandler = mqttAuth.isEnable() ? new DefaultMqttServerAuthHandler(mqttAuth.getUsername(), mqttAuth.getPassword()) : null;
serverCreator.authHandler(authHandler);
} else {
serverCreator.authHandler(authHandlerImpl);
}
// mqtt 内唯一id
if (Objects.nonNull(uniqueIdService)) {
serverCreator.uniqueIdService(uniqueIdService);
}
// 订阅校验
if (Objects.nonNull(subscribeValidator)) {
serverCreator.subscribeValidator(subscribeValidator);
}
// 订阅权限校验
if (Objects.nonNull(publishPermission)) {
serverCreator.publishPermission(publishPermission);
}
// 消息转发
if (Objects.nonNull(messageDispatcher)) {
serverCreator.messageDispatcher(messageDispatcher);
}
// 消息存储
if (Objects.nonNull(messageStore)) {
serverCreator.messageStore(messageStore);
}
// session 管理
if (Objects.nonNull(sessionManager)) {
serverCreator.sessionManager(sessionManager);
}
// session 监听
if (Objects.nonNull(sessionListener)) {
serverCreator.sessionListener(sessionListener);
}
// 状态监听
if (Objects.nonNull(connectStatusListener)) {
serverCreator.connectStatusListener(connectStatusListener);
}
// 消息监听器
if (Objects.nonNull(messageInterceptor)) {
serverCreator.addInterceptor(messageInterceptor);
}
// 自定义处理
if (Objects.nonNull(customizers)) {
customizers.customize(serverCreator);
}
MqttServer mqttServer = serverCreator.build();
MqttServerTemplate mqttServerTemplate = new MqttServerTemplate(mqttServer);
context.wrapAndPut(MqttServerTemplate.class, mqttServerTemplate);
// Metrics
context.beanMake(MqttServerMetricsConfiguration.class);
// 添加启动时的函数处理
functionDetector();
// 启动
if (properties.isEnabled() && !running) {
running = mqttServerTemplate.getMqttServer().start();
log.info("mqtt server start...");
}
});
}
private void functionDetector() {
// functionManager
MqttFunctionManager functionManager = context.getBean(MqttFunctionManager.class);
// 类级别的注解订阅
functionClassTags.forEach(each -> {
MqttServerFunction anno = each.getAnno();
String[] topicFilters = getTopicFilters(anno.value());
IMqttFunctionMessageListener messageListener = each.getBeanWrap().get();
functionManager.register(topicFilters, messageListener);
});
// 方法级别的注解订阅
functionMethodTags.forEach(each -> {
MqttServerFunction anno = each.getAnno();
// topic 信息
String[] topicTemplates = anno.value();
String[] topicFilters = getTopicFilters(topicTemplates);
// 自定义的反序列化,支持 solon bean 或者 无参构造器初始化
Class<? extends MqttDeserializer> deserialized = anno.deserialize();
MqttDeserializer deserializer = getMqttDeserializer(deserialized);
// 构造监听器
Object bean = each.getBw().get();
Method method = each.getMethod();
// 注册监听器
MqttServerFunctionListener functionListener = new MqttServerFunctionListener(bean, method, topicTemplates, topicFilters, deserializer);
functionManager.register(topicFilters, functionListener);
});
}
/**
* 获取解码器
*
* @param deserializerType deserializerType
* @return 解码器
*/
private MqttDeserializer getMqttDeserializer(Class<?> deserializerType) {
BeanWrap beanWrap = context.getWrap(deserializerType);
if (beanWrap == null) {
return ClassUtil.newInstance(deserializerType);
}
return beanWrap.get();
}
private String[] getTopicFilters(String[] topicTemplates) {
// 1. 替换 solon cfg 变量
// 2. 替换订阅中的其他变量
return Arrays.stream(topicTemplates)
.map((x) -> Optional.ofNullable(Solon.cfg().getByTmpl(x)).orElse(x))
.map(TopicUtil::getTopicFilter)
.toArray(String[]::new);
}
@Override
public void stop() {
if (running) {
MqttServerTemplate mqttServerTemplate = context.getBean(MqttServerTemplate.class);
mqttServerTemplate.getMqttServer().stop();
log.info("mqtt server stop...");
}
}
@Data
@RequiredArgsConstructor
private static class ExtractorClassTag<T> {
private final Class<?> clz;
private final BeanWrap beanWrap;
private final T anno;
}
@Data
@RequiredArgsConstructor
private static class ExtractorMethodTag<T> {
private final BeanWrap bw;
private final Method method;
private final T anno;
}
}

View File

@@ -0,0 +1,9 @@
open module org.dromara.mica.mqtt.server.solon.plugin {
requires solon;
requires lombok;
requires transitive org.dromara.mica.mqtt.server;
exports org.dromara.mica.mqtt.server.noear;
exports org.dromara.mica.mqtt.server.solon.event;
exports org.dromara.mica.mqtt.server.solon.config;
provides org.noear.solon.core.Plugin with org.dromara.mica.mqtt.server.solon.integration.MqttServerPluginImpl;
}

View File

@@ -0,0 +1,269 @@
{
"properties": [
{
"name": "mqtt.server.auth.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.auth.password",
"type": "java.lang.String",
"description": "http Basic 认证密码"
},
{
"name": "mqtt.server.auth.username",
"type": "java.lang.String",
"description": "http Basic 认证账号"
},
{
"name": "mqtt.server.debug",
"type": "java.lang.Boolean",
"description": "debug",
"defaultValue": "false"
},
{
"name": "mqtt.server.enabled",
"type": "java.lang.Boolean",
"description": "是否启用,默认:启用",
"defaultValue": "true"
},
{
"name": "mqtt.server.heartbeat-timeout",
"type": "java.lang.Long",
"description": "心跳超时时间(单位: 毫秒 默认: 1000 * 120)如果用户不希望框架层面做心跳相关工作请把此值设为0或负数"
},
{
"name": "mqtt.server.http-listener.basic-auth.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.http-listener.basic-auth.password",
"type": "java.lang.String",
"description": "http Basic 认证密码"
},
{
"name": "mqtt.server.http-listener.basic-auth.username",
"type": "java.lang.String",
"description": "http Basic 认证账号"
},
{
"name": "mqtt.server.http-listener.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.http-listener.ip",
"type": "java.lang.String",
"description": "服务端 ip"
},
{
"name": "mqtt.server.http-listener.mcp-server.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.http-listener.mcp-server.message-endpoint",
"type": "java.lang.String",
"description": "message 端点"
},
{
"name": "mqtt.server.http-listener.mcp-server.sse-endpoint",
"type": "java.lang.String",
"description": "sse 端点"
},
{
"name": "mqtt.server.http-listener.port",
"type": "java.lang.Integer",
"description": "端口"
},
{
"name": "mqtt.server.http-listener.ssl.client-auth",
"type": "org.tio.core.ssl.ClientAuth",
"description": "认证类型"
},
{
"name": "mqtt.server.http-listener.ssl.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.http-listener.ssl.keystore-pass",
"type": "java.lang.String",
"description": "keystore 密码"
},
{
"name": "mqtt.server.http-listener.ssl.keystore-path",
"type": "java.lang.String",
"description": "keystore 证书路径"
},
{
"name": "mqtt.server.http-listener.ssl.truststore-pass",
"type": "java.lang.String",
"description": "truststore 密码"
},
{
"name": "mqtt.server.http-listener.ssl.truststore-path",
"type": "java.lang.String",
"description": "truststore 证书路径"
},
{
"name": "mqtt.server.keepalive-backoff",
"type": "java.lang.Float",
"description": "MQTT 客户端 keepalive 系数,连接超时缺省为连接设置的 keepalive * keepaliveBackoff * 2默认0.75 <p> 如果读者想对该值做一些调整,可以在此进行配置。比如设置为 0.75,则变为 keepalive * 1.5。但是该值不得小于 0.5,否则将小于 keepalive 设定的时间。"
},
{
"name": "mqtt.server.max-bytes-in-message",
"type": "java.lang.String",
"description": "消息解析最大 bytes 长度默认10M"
},
{
"name": "mqtt.server.max-client-id-length",
"type": "java.lang.Integer",
"description": "mqtt 3.1 会校验此参数为 23为了减少问题设置成了 64"
},
{
"name": "mqtt.server.mqtt-listener.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.mqtt-listener.ip",
"type": "java.lang.String",
"description": "服务端 ip"
},
{
"name": "mqtt.server.mqtt-listener.port",
"type": "java.lang.Integer",
"description": "端口"
},
{
"name": "mqtt.server.mqtt-ssl-listener.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭",
"defaultValue": "false"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ip",
"type": "java.lang.String",
"description": "服务端 ip"
},
{
"name": "mqtt.server.mqtt-ssl-listener.port",
"type": "java.lang.Integer",
"description": "端口"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ssl.client-auth",
"type": "org.tio.core.ssl.ClientAuth",
"description": "认证类型"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ssl.keystore-pass",
"type": "java.lang.String",
"description": "keystore 密码"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ssl.keystore-path",
"type": "java.lang.String",
"description": "keystore 证书路径"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ssl.truststore-pass",
"type": "java.lang.String",
"description": "truststore 密码"
},
{
"name": "mqtt.server.mqtt-ssl-listener.ssl.truststore-path",
"type": "java.lang.String",
"description": "truststore 证书路径"
},
{
"name": "mqtt.server.name",
"type": "java.lang.String",
"description": "名称"
},
{
"name": "mqtt.server.node-name",
"type": "java.lang.String",
"description": "节点名称,用于处理集群"
},
{
"name": "mqtt.server.proxy-protocol-on",
"type": "java.lang.Boolean",
"description": "开启代理协议,支持 nginx proxy_protocol on;",
"defaultValue": "false"
},
{
"name": "mqtt.server.read-buffer-size",
"type": "java.lang.String",
"description": "接收数据的 buffer size默认8k"
},
{
"name": "mqtt.server.stat-enable",
"type": "java.lang.Boolean",
"description": "是否开启监控不开启可节省内存默认true"
},
{
"name": "mqtt.server.ws-listener.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭"
},
{
"name": "mqtt.server.ws-listener.ip",
"type": "java.lang.String",
"description": "服务端 ip"
},
{
"name": "mqtt.server.ws-listener.port",
"type": "java.lang.Integer",
"description": "端口"
},
{
"name": "mqtt.server.wss-listener.enable",
"type": "java.lang.Boolean",
"description": "是否启用,默认:关闭"
},
{
"name": "mqtt.server.wss-listener.ip",
"type": "java.lang.String",
"description": "服务端 ip"
},
{
"name": "mqtt.server.wss-listener.port",
"type": "java.lang.Integer",
"description": "端口"
},
{
"name": "mqtt.server.wss-listener.ssl.client-auth",
"type": "org.tio.core.ssl.ClientAuth",
"description": "认证类型"
},
{
"name": "mqtt.server.wss-listener.ssl.keystore-pass",
"type": "java.lang.String",
"description": "keystore 密码"
},
{
"name": "mqtt.server.wss-listener.ssl.keystore-path",
"type": "java.lang.String",
"description": "keystore 证书路径"
},
{
"name": "mqtt.server.wss-listener.ssl.truststore-pass",
"type": "java.lang.String",
"description": "truststore 密码"
},
{
"name": "mqtt.server.wss-listener.ssl.truststore-path",
"type": "java.lang.String",
"description": "truststore 证书路径"
}
]
}

View File

@@ -0,0 +1,2 @@
solon.plugin=org.dromara.mica.mqtt.server.solon.integration.MqttServerPluginImpl
solon.plugin.priority=1

View File

@@ -0,0 +1,20 @@
package org.dromara.mica.mqtt.server.solon.test.task;
import org.noear.solon.Solon;
import org.noear.solon.scheduling.annotation.EnableScheduling;
/**
* <b>(ServerTest)</b>
*
* @author Peigen
* @version 1.0.0
* @since 2023/7/15
*/
@EnableScheduling
public class ServerTest {
public static void main(String[] args) {
Solon.start(ServerTest.class,args);
}
}

View File

@@ -0,0 +1,24 @@
package org.dromara.mica.mqtt.server.solon.test.task.task;
import org.dromara.mica.mqtt.server.solon.MqttServerTemplate;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.scheduling.annotation.Scheduled;
import java.nio.charset.StandardCharsets;
/**
* @author wsq
*/
@Component
public class PublishAllTask {
@Inject
private MqttServerTemplate mqttServerTemplate;
@Scheduled(fixedDelay = 1000)
public void run() {
boolean b = mqttServerTemplate.publishAll("/test/123", "mica最牛皮".getBytes(StandardCharsets.UTF_8));
System.out.println(b);
}
}

View File

@@ -0,0 +1,20 @@
server:
port: 30033
# mqtt 服务端配置
mqtt:
server:
enabled: true # 是否开启服务端默认true
name: Mica-Mqtt-Server # 名称默认Mica-Mqtt-Server
heartbeat-timeout: 120000 # 心跳超时,单位毫秒,默认: 1000 * 120
read-buffer-size: 8KB # 接收数据的 buffer size默认8k
max-bytes-in-message: 10MB # 消息解析最大 bytes 长度默认10M
auth:
enable: false # 是否开启 mqtt 认证
username: mica # mqtt 认证用户名
password: mica # mqtt 认证密码
debug: true # 如果开启 prometheus 指标收集建议关闭
stat-enable: true # 开启指标收集debug 和 prometheus 开启时需要打开,默认开启,关闭节省内存
mqtt-listener: # mqtt 监听器,还有 mqtt-ssl-listener
enable: true # 是否开启默认false
# ip: "0.0.0.0" # 服务端 ip 默认为空0.0.0.0,建议不要设置
port: 1883 # 端口默认1883