【代码优化】IoT:移除 ServiceRegistry,使用 SpringUtils 替代
This commit is contained in:
parent
a85890d958
commit
3647fd3686
Binary file not shown.
|
@ -1,37 +0,0 @@
|
|||
package cn.iocoder.yudao.module.iot.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
// TODO 芋艿:纠结下
|
||||
/**
|
||||
* 服务注册表 - 插架模块使用,无法使用 Spring 注入
|
||||
*/
|
||||
public class ServiceRegistry {
|
||||
|
||||
private static final Map<Class<?>, Object> services = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 注册服务
|
||||
*
|
||||
* @param serviceClass 服务类
|
||||
* @param serviceImpl 服务实现
|
||||
* @param <T> 服务类
|
||||
*/
|
||||
public static <T> void registerService(Class<T> serviceClass, T serviceImpl) {
|
||||
services.put(serviceClass, serviceImpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得服务
|
||||
*
|
||||
* @param serviceClass 服务类
|
||||
* @param <T> 服务类
|
||||
* @return 服务实现
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getService(Class<T> serviceClass) {
|
||||
return (T) services.get(serviceClass);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package cn.iocoder.yudao.module.iot.mqttrpc.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
// TODO @芋艿:要不要加个 mqtt 值了的前缀
|
||||
/**
|
||||
* MQTT RPC 请求
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RpcRequest {
|
||||
|
||||
/**
|
||||
* 方法名
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
// TODO @haohao:object 对象会不会不好序列化?
|
||||
private Object[] params;
|
||||
|
||||
/**
|
||||
* 关联 ID
|
||||
*/
|
||||
private String correlationId;
|
||||
|
||||
/**
|
||||
* 回复地址
|
||||
*/
|
||||
private String replyTo;
|
||||
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package cn.iocoder.yudao.module.iot.mqttrpc.common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* MQTT RPC 响应
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RpcResponse {
|
||||
|
||||
/**
|
||||
* 关联 ID
|
||||
*/
|
||||
private String correlationId;
|
||||
|
||||
/**
|
||||
* 结果
|
||||
*/
|
||||
// TODO @haohao:object 对象会不会不好反序列化?
|
||||
private Object result;
|
||||
|
||||
/**
|
||||
* 错误
|
||||
*/
|
||||
private String error;
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package cn.iocoder.yudao.module.iot.mqttrpc.common;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
/**
|
||||
* 序列化工具类
|
||||
*
|
||||
*/
|
||||
public class SerializationUtils {
|
||||
|
||||
public static String serialize(Object obj) {
|
||||
return JSONUtil.toJsonStr(obj);
|
||||
}
|
||||
|
||||
public static <T> T deserialize(String json, Class<T> clazz) {
|
||||
return JSONUtil.toBean(json, clazz);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,22 +1,20 @@
|
|||
package cn.iocoder.yudao.module.iot.framework.plugin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum;
|
||||
import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Component
|
||||
@Slf4j
|
||||
public class PluginStart implements ApplicationRunner {
|
||||
|
|
|
@ -1,46 +1,35 @@
|
|||
package cn.iocoder.yudao.module.iot.framework.plugin;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
|
||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||
import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class UnifiedConfiguration {
|
||||
|
||||
private static final String SERVICE_REGISTRY_INITIALIZED_MARKER = "serviceRegistryInitializedMarker";
|
||||
|
||||
@Resource
|
||||
private DeviceDataApi deviceDataApi;
|
||||
@Value("${pf4j.pluginsDir:pluginsDir}")
|
||||
private String pluginsDir;
|
||||
|
||||
@Bean(SERVICE_REGISTRY_INITIALIZED_MARKER)
|
||||
public Object serviceRegistryInitializedMarker() {
|
||||
ServiceRegistry.registerService(DeviceDataApi.class, deviceDataApi);
|
||||
log.info("[init][将 DeviceDataApi 实例注册到 ServiceRegistry 中]");
|
||||
return new Object();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER)
|
||||
// @DependsOn("deviceDataApiImpl")
|
||||
public SpringPluginManager pluginManager() {
|
||||
log.info("[init][实例化 SpringPluginManager]");
|
||||
SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) {
|
||||
// SpringPluginManager springPluginManager = new SpringPluginManager() {
|
||||
|
||||
@Override
|
||||
public void startPlugins() {
|
||||
// 禁用插件启动,避免插件启动时,启动所有插件
|
||||
log.info("[init][禁用默认启动所有插件]");
|
||||
}
|
||||
|
||||
};
|
||||
springPluginManager.addPluginStateListener(new CustomPluginStateListener());
|
||||
return springPluginManager;
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.pf4j.PluginStateEvent;
|
|||
import org.pf4j.PluginStateListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CustomPluginStateListener implements PluginStateListener {
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package cn.iocoder.yudao.module.iot.plugin;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.pf4j.spring.SpringPlugin;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class HttpVertxPlugin extends SpringPlugin {
|
||||
|
||||
private static final int PORT = 8092;
|
||||
private Vertx vertx;
|
||||
|
||||
private DeviceDataApi deviceDataApi;
|
||||
|
||||
public HttpVertxPlugin(PluginWrapper wrapper) {
|
||||
|
@ -28,7 +28,7 @@ public class HttpVertxPlugin extends SpringPlugin {
|
|||
log.info("HttpVertxPlugin.start()");
|
||||
|
||||
// 获取 DeviceDataApi 实例
|
||||
deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class);
|
||||
deviceDataApi = SpringUtil.getBean(DeviceDataApi.class);
|
||||
if (deviceDataApi == null) {
|
||||
log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!");
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue