first commit
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.spring.server;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.core.annotation.MqttServerFunction;
|
||||
import org.dromara.mica.mqtt.core.deserialize.MqttDeserializer;
|
||||
import org.dromara.mica.mqtt.core.server.func.IMqttFunctionMessageListener;
|
||||
import org.dromara.mica.mqtt.core.server.func.MqttFunctionManager;
|
||||
import org.dromara.mica.mqtt.core.util.TopicUtil;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Mqtt 服务端消息处理
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class MqttServerFunctionDetector implements BeanPostProcessor {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(@NonNull Object bean, String beanName) throws BeansException {
|
||||
Class<?> userClass = ClassUtils.getUserClass(bean);
|
||||
// 1. 查找类上的 MqttServerFunction 注解
|
||||
if (bean instanceof IMqttFunctionMessageListener) {
|
||||
MqttServerFunction subscribe = AnnotationUtils.findAnnotation(userClass, MqttServerFunction.class);
|
||||
if (subscribe != null) {
|
||||
String[] topicFilters = getTopicFilters(applicationContext, subscribe.value());
|
||||
// 3. 注册监听器
|
||||
MqttFunctionManager functionManager = getFunctionManager();
|
||||
functionManager.register(topicFilters, (IMqttFunctionMessageListener) bean);
|
||||
}
|
||||
} else {
|
||||
// 2. 查找方法上的 MqttServerFunction 注解
|
||||
ReflectionUtils.doWithMethods(userClass, method -> {
|
||||
MqttServerFunction subscribe = AnnotationUtils.findAnnotation(method, MqttServerFunction.class);
|
||||
if (subscribe != null) {
|
||||
// 1. 校验必须为 public 和非 static 的方法
|
||||
int modifiers = method.getModifiers();
|
||||
if (Modifier.isStatic(modifiers)) {
|
||||
throw new IllegalArgumentException("@MqttServerFunction on method " + method + " must not static.");
|
||||
}
|
||||
if (!Modifier.isPublic(modifiers)) {
|
||||
throw new IllegalArgumentException("@MqttServerFunction on method " + method + " must public.");
|
||||
}
|
||||
// 2. 校验 method 入参数必须等于2
|
||||
int paramCount = method.getParameterCount();
|
||||
if (paramCount < 2 || paramCount > 6) {
|
||||
throw new IllegalArgumentException("@MqttServerFunction on method " + method + " parameter count must 2 ~ 6.");
|
||||
}
|
||||
// 3. topic 信息
|
||||
String[] topicTemplates = subscribe.value();
|
||||
String[] topicFilters = getTopicFilters(applicationContext, topicTemplates);
|
||||
// 4. 自定义的反序列化,支持 Spring bean 或者 无参构造器初始化
|
||||
Class<? extends MqttDeserializer> deserialized = subscribe.deserialize();
|
||||
@SuppressWarnings("unchecked")
|
||||
MqttDeserializer deserializer = getMqttDeserializer((Class<MqttDeserializer>) deserialized);
|
||||
// 5. 监听器
|
||||
MqttServerFunctionListener functionListener = new MqttServerFunctionListener(bean, method, topicTemplates, topicFilters, deserializer);
|
||||
// 6. 注册监听器
|
||||
MqttFunctionManager functionManager = getFunctionManager();
|
||||
functionManager.register(topicFilters, functionListener);
|
||||
}
|
||||
}, ReflectionUtils.USER_DECLARED_METHODS);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 MqttFunctionManager
|
||||
*
|
||||
* @return MqttFunctionManager
|
||||
*/
|
||||
protected MqttFunctionManager getFunctionManager() {
|
||||
return applicationContext.getBean(MqttFunctionManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取解码器
|
||||
*
|
||||
* @param deserializerType deserializerType
|
||||
* @return 解码器
|
||||
*/
|
||||
protected MqttDeserializer getMqttDeserializer(Class<MqttDeserializer> deserializerType) {
|
||||
return applicationContext.getBeanProvider(deserializerType)
|
||||
.getIfAvailable(() -> BeanUtils.instantiateClass(deserializerType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 Spring boot env 变量
|
||||
*
|
||||
* @param applicationContext ApplicationContext
|
||||
* @param values values
|
||||
* @return topic array
|
||||
*/
|
||||
private static String[] getTopicFilters(ApplicationContext applicationContext, String[] values) {
|
||||
// 1. 替换 Spring boot env 变量
|
||||
// 2. 替换订阅中的其他变量
|
||||
return Arrays.stream(values)
|
||||
.map(applicationContext.getEnvironment()::resolvePlaceholders)
|
||||
.map(TopicUtil::getTopicFilter)
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.spring.server;
|
||||
|
||||
import org.dromara.mica.mqtt.core.annotation.MqttServerFunction;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.LazyInitializationExcludeFilter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* mqtt 服务端函数延迟加载排除
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class MqttServerFunctionLazyFilter implements LazyInitializationExcludeFilter {
|
||||
|
||||
@Override
|
||||
public boolean isExcluded(String beanName, BeanDefinition beanDefinition, Class<?> beanType) {
|
||||
// 类上有注解的情况
|
||||
MqttServerFunction subscribe = AnnotationUtils.findAnnotation(beanType, MqttServerFunction.class);
|
||||
if (subscribe != null) {
|
||||
return true;
|
||||
}
|
||||
// 方法上的注解
|
||||
List<Method> methodList = new ArrayList<>();
|
||||
ReflectionUtils.doWithMethods(beanType, method -> {
|
||||
MqttServerFunction clientSubscribe = AnnotationUtils.findAnnotation(method, MqttServerFunction.class);
|
||||
if (clientSubscribe != null) {
|
||||
methodList.add(method);
|
||||
}
|
||||
}, ReflectionUtils.USER_DECLARED_METHODS);
|
||||
return !methodList.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.spring.server;
|
||||
|
||||
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.springframework.util.ReflectionUtils;
|
||||
import org.tio.core.ChannelContext;
|
||||
|
||||
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());
|
||||
// 反射调用
|
||||
ReflectionUtils.invokeMethod(method, bean, methodParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取反射参数
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.spring.server;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* 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.spring.server.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.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.MqttFunctionManager;
|
||||
import org.dromara.mica.mqtt.core.server.func.MqttFunctionMessageListener;
|
||||
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.spring.server.MqttServerFunctionDetector;
|
||||
import org.dromara.mica.mqtt.spring.server.MqttServerTemplate;
|
||||
import org.dromara.mica.mqtt.spring.server.event.SpringEventMqttConnectStatusListener;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.tio.core.Node;
|
||||
|
||||
/**
|
||||
* mqtt server 配置
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(
|
||||
prefix = MqttServerProperties.PREFIX,
|
||||
name = "enabled",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true
|
||||
)
|
||||
@EnableConfigurationProperties(MqttServerProperties.class)
|
||||
public class MqttServerConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MqttDeserializer mqttDeserializer() {
|
||||
return new MqttJsonDeserializer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IMqttConnectStatusListener springEventMqttConnectStatusListener(ApplicationEventPublisher eventPublisher) {
|
||||
return new SpringEventMqttConnectStatusListener(eventPublisher);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttServerCreator mqttServerCreator(MqttServerProperties properties,
|
||||
ObjectProvider<IMqttServerAuthHandler> authHandlerObjectProvider,
|
||||
ObjectProvider<IMqttServerUniqueIdService> uniqueIdServiceObjectProvider,
|
||||
ObjectProvider<IMqttServerSubscribeValidator> subscribeValidatorObjectProvider,
|
||||
ObjectProvider<IMqttServerPublishPermission> publishPermissionObjectProvider,
|
||||
ObjectProvider<IMqttMessageDispatcher> messageDispatcherObjectProvider,
|
||||
ObjectProvider<IMqttMessageStore> messageStoreObjectProvider,
|
||||
ObjectProvider<IMqttSessionManager> sessionManagerObjectProvider,
|
||||
ObjectProvider<IMqttSessionListener> sessionListenerObjectProvider,
|
||||
ObjectProvider<IMqttMessageListener> messageListenerObjectProvider,
|
||||
ObjectProvider<IMqttConnectStatusListener> connectStatusListenerObjectProvider,
|
||||
ObjectProvider<IMqttMessageInterceptor> messageInterceptorObjectProvider,
|
||||
ObjectProvider<MqttServerCustomizer> customizers) {
|
||||
MqttServerCreator serverCreator = MqttServer.create()
|
||||
.name(properties.getName())
|
||||
.heartbeatTimeout(properties.getHeartbeatTimeout())
|
||||
.keepaliveBackoff(properties.getKeepaliveBackoff())
|
||||
.readBufferSize((int) properties.getReadBufferSize().toBytes())
|
||||
.maxBytesInMessage((int) properties.getMaxBytesInMessage().toBytes())
|
||||
.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();
|
||||
});
|
||||
}
|
||||
// 自定义消息监听
|
||||
messageListenerObjectProvider.ifAvailable(serverCreator::messageListener);
|
||||
// 认证处理器
|
||||
IMqttServerAuthHandler authHandler = authHandlerObjectProvider.getIfAvailable(() -> {
|
||||
MqttServerProperties.MqttAuth mqttAuth = properties.getAuth();
|
||||
return mqttAuth.isEnable() ? new DefaultMqttServerAuthHandler(mqttAuth.getUsername(), mqttAuth.getPassword()) : null;
|
||||
});
|
||||
serverCreator.authHandler(authHandler);
|
||||
// mqtt 内唯一id
|
||||
uniqueIdServiceObjectProvider.ifAvailable(serverCreator::uniqueIdService);
|
||||
// 订阅校验
|
||||
subscribeValidatorObjectProvider.ifAvailable(serverCreator::subscribeValidator);
|
||||
// 订阅权限校验
|
||||
publishPermissionObjectProvider.ifAvailable(serverCreator::publishPermission);
|
||||
// 消息转发
|
||||
messageDispatcherObjectProvider.ifAvailable(serverCreator::messageDispatcher);
|
||||
// 消息存储
|
||||
messageStoreObjectProvider.ifAvailable(serverCreator::messageStore);
|
||||
// session 管理
|
||||
sessionManagerObjectProvider.ifAvailable(serverCreator::sessionManager);
|
||||
// session 监听
|
||||
sessionListenerObjectProvider.ifAvailable(serverCreator::sessionListener);
|
||||
// 状态监听
|
||||
connectStatusListenerObjectProvider.ifAvailable(serverCreator::connectStatusListener);
|
||||
// 消息监听器
|
||||
messageInterceptorObjectProvider.orderedStream().forEach(serverCreator::addInterceptor);
|
||||
// 自定义处理
|
||||
customizers.ifAvailable((customizer) -> customizer.customize(serverCreator));
|
||||
return serverCreator;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttServer mqttServer(MqttServerCreator mqttServerCreator) {
|
||||
return mqttServerCreator.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttServerLauncher mqttServerLauncher(MqttServer mqttServer) {
|
||||
return new MqttServerLauncher(mqttServer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttServerTemplate mqttServerTemplate(MqttServer mqttServer) {
|
||||
return new MqttServerTemplate(mqttServer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(MqttFunctionManager.class)
|
||||
public static MqttFunctionManager mqttFunctionManager() {
|
||||
return new MqttFunctionManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(IMqttMessageListener.class)
|
||||
public IMqttMessageListener mqttFunctionMessageListener(MqttFunctionManager mqttFunctionManager) {
|
||||
return new MqttFunctionMessageListener(mqttFunctionManager);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static MqttServerFunctionDetector mqttServerFunctionDetector(ApplicationContext applicationContext) {
|
||||
return new MqttServerFunctionDetector(applicationContext);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.spring.server.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.core.server.MqttServer;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* MqttServer 启动器
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class MqttServerLauncher implements SmartLifecycle, Ordered {
|
||||
private final MqttServer mqttServer;
|
||||
private volatile boolean running = false;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
running = mqttServer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
mqttServer.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
stop();
|
||||
callback.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return DEFAULT_PHASE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.spring.server.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.dromara.mica.mqtt.core.server.MqttServer;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
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 implements ApplicationListener<ApplicationStartedEvent> {
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
ApplicationContext applicationContext = event.getApplicationContext();
|
||||
MeterRegistry registry = getMeterRegistry(applicationContext);
|
||||
if (registry != null) {
|
||||
MqttServer mqttServer = applicationContext.getBean(MqttServer.class);
|
||||
TioServerConfig serverConfig = mqttServer.getServerConfig();
|
||||
bindTo(registry, serverConfig);
|
||||
}
|
||||
}
|
||||
|
||||
private MeterRegistry getMeterRegistry(ApplicationContext applicationContext) {
|
||||
try {
|
||||
return applicationContext.getBean(MeterRegistry.class);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
log.warn(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.spring.server.config;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
/**
|
||||
* mica mqtt Metrics 配置
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(
|
||||
prefix = MqttServerProperties.PREFIX,
|
||||
name = "enabled",
|
||||
havingValue = "true"
|
||||
)
|
||||
@ConditionalOnClass(MeterRegistry.class)
|
||||
@AutoConfigureAfter(MqttServerConfiguration.class)
|
||||
public class MqttServerMetricsConfiguration {
|
||||
|
||||
@Bean
|
||||
public MqttServerMetrics micaMqttMetrics() {
|
||||
return new MqttServerMetrics();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* 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.spring.server.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.dromara.mica.mqtt.codec.MqttConstant;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.tio.core.Node;
|
||||
import org.tio.core.ssl.ClientAuth;
|
||||
|
||||
/**
|
||||
* MqttServer 配置
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(MqttServerProperties.PREFIX)
|
||||
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,默认:8k
|
||||
*/
|
||||
private DataSize readBufferSize = DataSize.ofBytes(MqttConstant.DEFAULT_MAX_READ_BUFFER_SIZE);
|
||||
/**
|
||||
* 消息解析最大 bytes 长度,默认:10M
|
||||
*/
|
||||
private DataSize maxBytesInMessage = DataSize.ofBytes(MqttConstant.DEFAULT_MAX_BYTES_IN_MESSAGE);
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -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.spring.server.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;
|
||||
|
||||
}
|
||||
@@ -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.spring.server.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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.spring.server.event;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.mica.mqtt.core.server.event.IMqttConnectStatusListener;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.tio.core.ChannelContext;
|
||||
import org.tio.core.Node;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* spring event mqtt 连接状态
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SpringEventMqttConnectStatusListener implements IMqttConnectStatusListener {
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
@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);
|
||||
eventPublisher.publishEvent(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);
|
||||
eventPublisher.publishEvent(offlineEvent);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
open module org.dromara.mica.mqtt.server.spring.boot.starter {
|
||||
requires lombok;
|
||||
requires spring.core;
|
||||
requires spring.context;
|
||||
requires spring.boot;
|
||||
requires spring.boot.autoconfigure;
|
||||
requires transitive org.dromara.mica.mqtt.server;
|
||||
exports org.dromara.mica.mqtt.spring.server;
|
||||
exports org.dromara.mica.mqtt.spring.server.config;
|
||||
exports org.dromara.mica.mqtt.spring.server.event;
|
||||
}
|
||||
Reference in New Issue
Block a user